Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Jeff Thompson | 47c93cf | 2013-08-09 00:38:48 -0700 | [diff] [blame] | 2 | /** |
José Quevedo | 641de4c | 2016-01-29 00:11:24 +0000 | [diff] [blame] | 3 | * Copyright (c) 2013-2016 Regents of the University of California. |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 6 | * |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 7 | * ndn-cxx library is free software: you can redistribute it and/or modify it under the |
| 8 | * terms of the GNU Lesser General Public License as published by the Free Software |
| 9 | * Foundation, either version 3 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received copies of the GNU General Public License and GNU Lesser |
| 16 | * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see |
| 17 | * <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 20 | * |
| 21 | * @author Yingdi Yu <http://irl.cs.ucla.edu/~yingdi/> |
Jeff Thompson | 47c93cf | 2013-08-09 00:38:48 -0700 | [diff] [blame] | 22 | */ |
| 23 | |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 24 | #ifndef NDN_SECURITY_KEY_CHAIN_HPP |
| 25 | #define NDN_SECURITY_KEY_CHAIN_HPP |
Jeff Thompson | 47c93cf | 2013-08-09 00:38:48 -0700 | [diff] [blame] | 26 | |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 27 | #include "sec-public-info.hpp" |
| 28 | #include "sec-tpm.hpp" |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 29 | #include "key-params.hpp" |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 30 | #include "secured-bag.hpp" |
Yingdi Yu | 4f32463 | 2014-01-15 18:10:03 -0800 | [diff] [blame] | 31 | #include "signature-sha256-with-rsa.hpp" |
Yingdi Yu | c8f883c | 2014-06-20 23:25:22 -0700 | [diff] [blame] | 32 | #include "signature-sha256-with-ecdsa.hpp" |
Yingdi Yu | bf6a281 | 2014-06-17 15:32:11 -0700 | [diff] [blame] | 33 | #include "digest-sha256.hpp" |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 34 | #include "signing-info.hpp" |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 35 | |
Yingdi Yu | 4270f20 | 2014-01-28 14:19:16 -0800 | [diff] [blame] | 36 | #include "../interest.hpp" |
Yingdi Yu | 2115716 | 2014-02-28 13:02:34 -0800 | [diff] [blame] | 37 | #include "../util/crypto.hpp" |
Yingdi Yu | 0f5fb69 | 2014-06-10 12:07:28 -0700 | [diff] [blame] | 38 | #include "../util/random.hpp" |
Alexander Afanasyev | 0711380 | 2015-01-15 19:14:36 -0800 | [diff] [blame] | 39 | #include <initializer_list> |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 40 | |
Jeff Thompson | 47c93cf | 2013-08-09 00:38:48 -0700 | [diff] [blame] | 41 | |
| 42 | namespace ndn { |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 43 | namespace security { |
Jeff Thompson | 47c93cf | 2013-08-09 00:38:48 -0700 | [diff] [blame] | 44 | |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 45 | /** |
| 46 | * @brief The packet signing interface. |
| 47 | */ |
Alexander Afanasyev | 45a3713 | 2014-04-28 16:54:57 -0700 | [diff] [blame] | 48 | class KeyChain : noncopyable |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 49 | { |
| 50 | public: |
| 51 | class Error : public std::runtime_error |
| 52 | { |
| 53 | public: |
| 54 | explicit |
| 55 | Error(const std::string& what) |
| 56 | : std::runtime_error(what) |
| 57 | { |
| 58 | } |
| 59 | }; |
| 60 | |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 61 | /** |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 62 | * @brief Error thrown when the supplied TPM locator to KeyChain constructor does not match |
| 63 | * the locator stored in PIB |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 64 | */ |
| 65 | class MismatchError : public Error |
| 66 | { |
| 67 | public: |
| 68 | explicit |
| 69 | MismatchError(const std::string& what) |
| 70 | : Error(what) |
| 71 | { |
| 72 | } |
| 73 | }; |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 74 | |
Alexander Afanasyev | 0711380 | 2015-01-15 19:14:36 -0800 | [diff] [blame] | 75 | typedef function<unique_ptr<SecPublicInfo> (const std::string&)> PibCreateFunc; |
| 76 | typedef function<unique_ptr<SecTpm>(const std::string&)> TpmCreateFunc; |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 77 | |
Alexander Afanasyev | 0711380 | 2015-01-15 19:14:36 -0800 | [diff] [blame] | 78 | /** |
| 79 | * @brief Register a new PIB |
Alexander Afanasyev | 34a3763 | 2015-01-16 17:37:36 -0800 | [diff] [blame] | 80 | * @param aliases List of schemes with which this PIB will be associated. |
| 81 | * The first alias in the list is considered a canonical name of the PIB instance. |
Alexander Afanasyev | 0711380 | 2015-01-15 19:14:36 -0800 | [diff] [blame] | 82 | */ |
| 83 | template<class PibType> |
| 84 | static void |
Alexander Afanasyev | 34a3763 | 2015-01-16 17:37:36 -0800 | [diff] [blame] | 85 | registerPib(std::initializer_list<std::string> aliases); |
Alexander Afanasyev | 0711380 | 2015-01-15 19:14:36 -0800 | [diff] [blame] | 86 | |
| 87 | /** |
| 88 | * @brief Register a new TPM |
Alexander Afanasyev | 34a3763 | 2015-01-16 17:37:36 -0800 | [diff] [blame] | 89 | * @param aliases List of schemes with which this TPM will be associated |
| 90 | * The first alias in the list is considered a canonical name of the TPM instance. |
Alexander Afanasyev | 0711380 | 2015-01-15 19:14:36 -0800 | [diff] [blame] | 91 | */ |
| 92 | template<class TpmType> |
| 93 | static void |
Alexander Afanasyev | 34a3763 | 2015-01-16 17:37:36 -0800 | [diff] [blame] | 94 | registerTpm(std::initializer_list<std::string> aliases); |
Alexander Afanasyev | 0711380 | 2015-01-15 19:14:36 -0800 | [diff] [blame] | 95 | |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 96 | /** |
| 97 | * @brief Get default PIB locator |
| 98 | */ |
Yingdi Yu | 281689a | 2015-06-13 14:32:32 -0700 | [diff] [blame] | 99 | static std::string |
| 100 | getDefaultPibLocator(); |
| 101 | |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 102 | /** |
| 103 | * @brief Create a PIB according to @p pibLocator |
| 104 | */ |
Yingdi Yu | 281689a | 2015-06-13 14:32:32 -0700 | [diff] [blame] | 105 | static unique_ptr<SecPublicInfo> |
| 106 | createPib(const std::string& pibLocator); |
| 107 | |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 108 | /** |
| 109 | * @brief Get default TPM locator |
| 110 | */ |
Yingdi Yu | 281689a | 2015-06-13 14:32:32 -0700 | [diff] [blame] | 111 | static std::string |
| 112 | getDefaultTpmLocator(); |
| 113 | |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 114 | /** |
| 115 | * @brief Create a TPM according to @p tpmLocator |
| 116 | */ |
Yingdi Yu | 281689a | 2015-06-13 14:32:32 -0700 | [diff] [blame] | 117 | static unique_ptr<SecTpm> |
| 118 | createTpm(const std::string& tpmLocator); |
| 119 | |
Alexander Afanasyev | 0711380 | 2015-01-15 19:14:36 -0800 | [diff] [blame] | 120 | /** |
| 121 | * @brief Constructor to create KeyChain with default PIB and TPM |
| 122 | * |
| 123 | * Default PIB and TPM are platform-dependent and can be overriden system-wide or on |
| 124 | * per-use basis. |
| 125 | * |
| 126 | * @todo Add detailed description about config file behavior here |
| 127 | */ |
| 128 | KeyChain(); |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 129 | |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 130 | /** |
| 131 | * @brief KeyChain constructor |
| 132 | * |
| 133 | * @sa http://redmine.named-data.net/issues/2260 |
| 134 | * |
Davide Pesavento | 18cf81b | 2015-09-12 23:36:43 +0200 | [diff] [blame] | 135 | * @param pibLocator PIB locator |
| 136 | * @param tpmLocator TPM locator |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 137 | * @param allowReset if true, the PIB will be reset when the supplied tpmLocator |
| 138 | * mismatches the one in PIB |
| 139 | */ |
| 140 | KeyChain(const std::string& pibLocator, |
| 141 | const std::string& tpmLocator, |
| 142 | bool allowReset = false); |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 143 | |
| 144 | virtual |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 145 | ~KeyChain(); |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 146 | |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 147 | /** |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 148 | * @brief Create an identity by creating a pair of Key-Signing-Key (KSK) for this identity and a |
| 149 | * self-signed certificate of the KSK. |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 150 | * |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 151 | * @param identityName The name of the identity. |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 152 | * @param params The key parameter if a key needs to be generated for the identity. |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 153 | * @return The name of the default certificate of the identity. |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 154 | */ |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 155 | Name |
| 156 | createIdentity(const Name& identityName, const KeyParams& params = DEFAULT_KEY_PARAMS); |
Alexander Afanasyev | b78bc4d | 2014-04-09 21:20:52 -0700 | [diff] [blame] | 157 | |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 158 | /** |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 159 | * @brief Generate a pair of RSA keys for the specified identity. |
| 160 | * |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 161 | * @param identityName The name of the identity. |
| 162 | * @param isKsk true for generating a Key-Signing-Key (KSK), false for a Data-Signing-Key (KSK). |
| 163 | * @param keySize The size of the key. |
| 164 | * @return The generated key name. |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 165 | * @see generateEcdsaKeyPair |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 166 | */ |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 167 | Name |
Yingdi Yu | c8f883c | 2014-06-20 23:25:22 -0700 | [diff] [blame] | 168 | generateRsaKeyPair(const Name& identityName, bool isKsk = false, uint32_t keySize = 2048); |
Alexander Afanasyev | b78bc4d | 2014-04-09 21:20:52 -0700 | [diff] [blame] | 169 | |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 170 | /** |
| 171 | * @brief Generate a pair of ECDSA keys for the specified identity. |
| 172 | * |
| 173 | * @param identityName The name of the identity. |
| 174 | * @param isKsk true for generating a Key-Signing-Key (KSK), false for a Data-Signing-Key (KSK). |
| 175 | * @param keySize The size of the key. |
| 176 | * @return The generated key name. |
| 177 | * @see generateRsaKeyPair |
| 178 | */ |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 179 | Name |
Yingdi Yu | c8f883c | 2014-06-20 23:25:22 -0700 | [diff] [blame] | 180 | generateEcdsaKeyPair(const Name& identityName, bool isKsk = false, uint32_t keySize = 256); |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 181 | |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 182 | /** |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 183 | * @brief Generate a pair of RSA keys for the specified identity and set it as default key for |
| 184 | * the identity. |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 185 | * |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 186 | * @param identityName The name of the identity. |
| 187 | * @param isKsk true for generating a Key-Signing-Key (KSK), false for a Data-Signing-Key (KSK). |
| 188 | * @param keySize The size of the key. |
| 189 | * @return The generated key name. |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 190 | * @see generateRsaKeyPair, generateEcdsaKeyPair, generateEcdsaKeyPairAsDefault |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 191 | */ |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 192 | Name |
José Quevedo | 699d2ea | 2016-01-29 00:13:00 +0000 | [diff] [blame] | 193 | generateRsaKeyPairAsDefault(const Name& identityName, bool isKsk = false, uint32_t keySize = 2048); |
Yingdi Yu | c8f883c | 2014-06-20 23:25:22 -0700 | [diff] [blame] | 194 | |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 195 | /** |
| 196 | * @brief Generate a pair of ECDSA keys for the specified identity and set it as default key for |
| 197 | * the identity. |
| 198 | * |
| 199 | * @param identityName The name of the identity. |
| 200 | * @param isKsk true for generating a Key-Signing-Key (KSK), false for a Data-Signing-Key (KSK). |
| 201 | * @param keySize The size of the key. |
| 202 | * @return The generated key name. |
| 203 | * @see generateRsaKeyPair, generateEcdsaKeyPair, generateRsaKeyPairAsDefault |
| 204 | */ |
Yingdi Yu | c8f883c | 2014-06-20 23:25:22 -0700 | [diff] [blame] | 205 | Name |
José Quevedo | 699d2ea | 2016-01-29 00:13:00 +0000 | [diff] [blame] | 206 | generateEcdsaKeyPairAsDefault(const Name& identityName, bool isKsk = false, uint32_t keySize = 256); |
Jeff Thompson | 79a2d5d | 2013-09-27 14:32:23 -0700 | [diff] [blame] | 207 | |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 208 | /** |
Yingdi Yu | c55680b | 2014-02-26 12:31:35 -0800 | [diff] [blame] | 209 | * @brief prepare an unsigned identity certificate |
| 210 | * |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 211 | * @param keyName Key name, e.g., `/<identity_name>/ksk-123456`. |
Yingdi Yu | c55680b | 2014-02-26 12:31:35 -0800 | [diff] [blame] | 212 | * @param signingIdentity The signing identity. |
| 213 | * @param notBefore Refer to IdentityCertificate. |
| 214 | * @param notAfter Refer to IdentityCertificate. |
Alexander Afanasyev | b78bc4d | 2014-04-09 21:20:52 -0700 | [diff] [blame] | 215 | * @param subjectDescription Refer to IdentityCertificate. |
Yingdi Yu | 0eb5d72 | 2014-06-10 15:06:25 -0700 | [diff] [blame] | 216 | * @param certPrefix Prefix before `KEY` component. By default, KeyChain will infer the |
| 217 | * certificate name according to the relation between the signingIdentity and |
| 218 | * the subject identity. If signingIdentity is a prefix of the subject identity, |
| 219 | * `KEY` will be inserted after the signingIdentity, otherwise `KEY` is inserted |
| 220 | * after subject identity (i.e., before `ksk-....`). |
Yingdi Yu | c55680b | 2014-02-26 12:31:35 -0800 | [diff] [blame] | 221 | * @return IdentityCertificate. |
| 222 | */ |
| 223 | shared_ptr<IdentityCertificate> |
| 224 | prepareUnsignedIdentityCertificate(const Name& keyName, |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 225 | const Name& signingIdentity, |
| 226 | const time::system_clock::TimePoint& notBefore, |
| 227 | const time::system_clock::TimePoint& notAfter, |
Yingdi Yu | 0eb5d72 | 2014-06-10 15:06:25 -0700 | [diff] [blame] | 228 | const std::vector<CertificateSubjectDescription>& subjectDescription, |
| 229 | const Name& certPrefix = DEFAULT_PREFIX); |
| 230 | |
| 231 | /** |
| 232 | * @brief prepare an unsigned identity certificate |
| 233 | * |
| 234 | * @param keyName Key name, e.g., `/<identity_name>/ksk-123456`. |
| 235 | * @param publicKey Public key to sign. |
| 236 | * @param signingIdentity The signing identity. |
| 237 | * @param notBefore Refer to IdentityCertificate. |
| 238 | * @param notAfter Refer to IdentityCertificate. |
| 239 | * @param subjectDescription Refer to IdentityCertificate. |
| 240 | * @param certPrefix Prefix before `KEY` component. By default, KeyChain will infer the |
| 241 | * certificate name according to the relation between the signingIdentity and |
| 242 | * the subject identity. If signingIdentity is a prefix of the subject identity, |
| 243 | * `KEY` will be inserted after the signingIdentity, otherwise `KEY` is inserted |
| 244 | * after subject identity (i.e., before `ksk-....`). |
| 245 | * @return IdentityCertificate. |
| 246 | */ |
| 247 | shared_ptr<IdentityCertificate> |
| 248 | prepareUnsignedIdentityCertificate(const Name& keyName, |
| 249 | const PublicKey& publicKey, |
| 250 | const Name& signingIdentity, |
| 251 | const time::system_clock::TimePoint& notBefore, |
| 252 | const time::system_clock::TimePoint& notAfter, |
| 253 | const std::vector<CertificateSubjectDescription>& subjectDescription, |
| 254 | const Name& certPrefix = DEFAULT_PREFIX); |
Yingdi Yu | c55680b | 2014-02-26 12:31:35 -0800 | [diff] [blame] | 255 | |
| 256 | /** |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 257 | * @brief Sign data according to the supplied signing information |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 258 | * |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 259 | * This method uses the supplied signing information @p params to create the SignatureInfo block: |
| 260 | * - it selects a private key and its certificate to sign the packet |
| 261 | * - sets the KeyLocator field with the certificate name, and |
| 262 | * - adds other requested information to the SignatureInfo block). |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 263 | * |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 264 | * After that, the method assigns the created SignatureInfo to the data packets, generate a |
| 265 | * signature and sets as part of the SignatureValue block. |
| 266 | * |
| 267 | * @param data The data to sign |
| 268 | * @param params The signing parameters. |
| 269 | * @throws Error if signing fails. |
| 270 | * @see SigningInfo |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 271 | */ |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 272 | void |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 273 | sign(Data& data, const SigningInfo& params = DEFAULT_SIGNING_INFO); |
Alexander Afanasyev | b78bc4d | 2014-04-09 21:20:52 -0700 | [diff] [blame] | 274 | |
Jeff Thompson | 47c93cf | 2013-08-09 00:38:48 -0700 | [diff] [blame] | 275 | /** |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 276 | * @brief Sign interest according to the supplied signing information |
| 277 | * |
| 278 | * This method uses the supplied signing information @p params to create the SignatureInfo block: |
| 279 | * - it selects a private key and its certificate to sign the packet |
| 280 | * - sets the KeyLocator field with the certificate name, and |
| 281 | * - adds other requested information to the SignatureInfo block). |
| 282 | * |
| 283 | * After that, the method appends the created SignatureInfo to the interest name, generate a |
| 284 | * signature and appends it as part of the SignatureValue block to the interest name. |
| 285 | * |
| 286 | * @param interest The interest to sign |
| 287 | * @param params The signing parameters. |
| 288 | * @throws Error if signing fails. |
| 289 | * @see SigningInfo |
| 290 | */ |
| 291 | void |
| 292 | sign(Interest& interest, const SigningInfo& params = DEFAULT_SIGNING_INFO); |
| 293 | |
| 294 | /** |
| 295 | * @brief Sign buffer according to the supplied signing information |
| 296 | * |
| 297 | * @param buffer The buffer to sign |
| 298 | * @param bufferLength The buffer size |
| 299 | * @param params The signing parameters. |
| 300 | * @return a SignatureValue TLV block |
| 301 | * @throws Error if signing fails. |
| 302 | * @see SigningInfo |
| 303 | */ |
| 304 | Block |
| 305 | sign(const uint8_t* buffer, size_t bufferLength, const SigningInfo& params); |
| 306 | |
| 307 | /** |
| 308 | * @deprecated use sign sign(T&, const SigningInfo&) |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 309 | * @brief Sign packet with a particular certificate. |
| 310 | * |
| 311 | * @param packet The packet to be signed. |
| 312 | * @param certificateName The certificate name of the key to use for signing. |
| 313 | * @throws SecPublicInfo::Error if certificate does not exist. |
Jeff Thompson | 3c73da4 | 2013-08-12 11:19:05 -0700 | [diff] [blame] | 314 | */ |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 315 | template<typename T> |
Alexander Afanasyev | b78bc4d | 2014-04-09 21:20:52 -0700 | [diff] [blame] | 316 | void |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 317 | sign(T& packet, const Name& certificateName); |
Alexander Afanasyev | b78bc4d | 2014-04-09 21:20:52 -0700 | [diff] [blame] | 318 | |
Jeff Thompson | 29ce310 | 2013-09-27 11:47:48 -0700 | [diff] [blame] | 319 | /** |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 320 | * @deprecated Use sign(const uint8_t*, size_t, const SigningInfo&) instead |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 321 | * @brief Sign the byte array using a particular certificate. |
| 322 | * |
Jeff Thompson | c01e178 | 2013-10-21 14:08:42 -0700 | [diff] [blame] | 323 | * @param buffer The byte array to be signed. |
| 324 | * @param bufferLength the length of buffer. |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 325 | * @param certificateName The certificate name of the signing key. |
Jeff Thompson | c01e178 | 2013-10-21 14:08:42 -0700 | [diff] [blame] | 326 | * @return The Signature. |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 327 | * @throws SecPublicInfo::Error if certificate does not exist. |
Jeff Thompson | c01e178 | 2013-10-21 14:08:42 -0700 | [diff] [blame] | 328 | */ |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 329 | Signature |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 330 | sign(const uint8_t* buffer, size_t bufferLength, const Name& certificateName); |
Jeff Thompson | c01e178 | 2013-10-21 14:08:42 -0700 | [diff] [blame] | 331 | |
| 332 | /** |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 333 | * @deprecated use sign sign(T&, const SigningInfo&) |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 334 | * @brief Sign packet using the default certificate of a particular identity. |
| 335 | * |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 336 | * If there is no default certificate of that identity, this method will create a self-signed |
| 337 | * certificate. |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 338 | * |
| 339 | * @param packet The packet to be signed. |
| 340 | * @param identityName The signing identity name. |
Jeff Thompson | 29ce310 | 2013-09-27 11:47:48 -0700 | [diff] [blame] | 341 | */ |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 342 | template<typename T> |
Alexander Afanasyev | b78bc4d | 2014-04-09 21:20:52 -0700 | [diff] [blame] | 343 | void |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 344 | signByIdentity(T& packet, const Name& identityName); |
Alexander Afanasyev | b78bc4d | 2014-04-09 21:20:52 -0700 | [diff] [blame] | 345 | |
Jeff Thompson | 3c73da4 | 2013-08-12 11:19:05 -0700 | [diff] [blame] | 346 | /** |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 347 | * @deprecated use sign(const uint8_t*, size_t, const SigningInfo&) instead |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 348 | * @brief Sign the byte array using the default certificate of a particular identity. |
| 349 | * |
Jeff Thompson | c01e178 | 2013-10-21 14:08:42 -0700 | [diff] [blame] | 350 | * @param buffer The byte array to be signed. |
| 351 | * @param bufferLength the length of buffer. |
| 352 | * @param identityName The identity name. |
| 353 | * @return The Signature. |
| 354 | */ |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 355 | Signature |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 356 | signByIdentity(const uint8_t* buffer, size_t bufferLength, const Name& identityName); |
Jeff Thompson | c01e178 | 2013-10-21 14:08:42 -0700 | [diff] [blame] | 357 | |
| 358 | /** |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 359 | * @deprecated use sign(Data&, SigningInfo(SigningInfo::SIGNER_TYPE_SHA256)) |
Yingdi Yu | 6ab6781 | 2014-11-27 15:00:34 -0800 | [diff] [blame] | 360 | * @brief Set Sha256 weak signature for @p data |
Yingdi Yu | 2115716 | 2014-02-28 13:02:34 -0800 | [diff] [blame] | 361 | */ |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 362 | void |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 363 | signWithSha256(Data& data); |
Yingdi Yu | 2115716 | 2014-02-28 13:02:34 -0800 | [diff] [blame] | 364 | |
| 365 | /** |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 366 | * @deprecated use sign(Interest&, SigningInfo(SigningInfo::SIGNER_TYPE_SHA256)) |
Yingdi Yu | 6ab6781 | 2014-11-27 15:00:34 -0800 | [diff] [blame] | 367 | * @brief Set Sha256 weak signature for @p interest |
| 368 | */ |
| 369 | void |
| 370 | signWithSha256(Interest& interest); |
| 371 | |
| 372 | /** |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 373 | * @brief Generate a self-signed certificate for a public key. |
| 374 | * |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 375 | * @param keyName The name of the public key |
| 376 | * @return The generated certificate, shared_ptr<IdentityCertificate>() if selfSign fails |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 377 | */ |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 378 | shared_ptr<IdentityCertificate> |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 379 | selfSign(const Name& keyName); |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 380 | |
| 381 | /** |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 382 | * @brief Self-sign the supplied identity certificate. |
| 383 | * |
| 384 | * @param cert The supplied cert. |
| 385 | * @throws SecTpm::Error if the private key does not exist. |
Jeff Thompson | 8efe5ad | 2013-08-20 17:36:38 -0700 | [diff] [blame] | 386 | */ |
Jeff Thompson | 2ce8f49 | 2013-09-17 18:01:25 -0700 | [diff] [blame] | 387 | void |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 388 | selfSign(IdentityCertificate& cert); |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 389 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 390 | /** |
| 391 | * @brief delete a certificate. |
| 392 | * |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 393 | * @param certificateName The certificate to be deleted. |
Yingdi Yu | 6147ef4 | 2014-12-08 17:48:32 -0800 | [diff] [blame] | 394 | * @throws KeyChain::Error if certificate cannot be deleted. |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 395 | */ |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 396 | void |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 397 | deleteCertificate(const Name& certificateName); |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 398 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 399 | /** |
| 400 | * @brief delete a key. |
| 401 | * |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 402 | * @param keyName The key to be deleted. |
Yingdi Yu | 6147ef4 | 2014-12-08 17:48:32 -0800 | [diff] [blame] | 403 | * @throws KeyChain::Error if key cannot be deleted. |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 404 | */ |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 405 | void |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 406 | deleteKey(const Name& keyName); |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 407 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 408 | /** |
| 409 | * @brief delete an identity. |
| 410 | * |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 411 | * @param identity The identity to be deleted. |
Yingdi Yu | 6147ef4 | 2014-12-08 17:48:32 -0800 | [diff] [blame] | 412 | * @throws KeyChain::Error if identity cannot be deleted. |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 413 | */ |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 414 | void |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 415 | deleteIdentity(const Name& identity); |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 416 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 417 | /** |
| 418 | * @brief export an identity. |
| 419 | * |
| 420 | * @param identity The identity to export. |
| 421 | * @param passwordStr The password to secure the private key. |
Yingdi Yu | 64c3fb4 | 2014-02-26 17:30:04 -0800 | [diff] [blame] | 422 | * @return The encoded export data. |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 423 | * @throws SecPublicInfo::Error if anything goes wrong in exporting. |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 424 | */ |
Yingdi Yu | 64c3fb4 | 2014-02-26 17:30:04 -0800 | [diff] [blame] | 425 | shared_ptr<SecuredBag> |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 426 | exportIdentity(const Name& identity, const std::string& passwordStr); |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 427 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 428 | /** |
| 429 | * @brief import an identity. |
| 430 | * |
Yingdi Yu | 64c3fb4 | 2014-02-26 17:30:04 -0800 | [diff] [blame] | 431 | * @param securedBag The encoded import data. |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 432 | * @param passwordStr The password to secure the private key. |
| 433 | */ |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 434 | void |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 435 | importIdentity(const SecuredBag& securedBag, const std::string& passwordStr); |
| 436 | |
| 437 | SecPublicInfo& |
| 438 | getPib() |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 439 | { |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 440 | return *m_pib; |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 441 | } |
| 442 | |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 443 | const SecPublicInfo& |
| 444 | getPib() const |
| 445 | { |
| 446 | return *m_pib; |
| 447 | } |
| 448 | |
| 449 | SecTpm& |
| 450 | getTpm() |
| 451 | { |
| 452 | return *m_tpm; |
| 453 | } |
| 454 | |
| 455 | const SecTpm& |
| 456 | getTpm() const |
| 457 | { |
| 458 | return *m_tpm; |
| 459 | } |
| 460 | |
| 461 | /******************************* |
| 462 | * Wrapper of SecPublicInfo * |
| 463 | *******************************/ |
| 464 | bool |
| 465 | doesIdentityExist(const Name& identityName) const |
| 466 | { |
| 467 | return m_pib->doesIdentityExist(identityName); |
| 468 | } |
| 469 | |
| 470 | void |
| 471 | addIdentity(const Name& identityName) |
| 472 | { |
| 473 | return m_pib->addIdentity(identityName); |
| 474 | } |
| 475 | |
| 476 | bool |
| 477 | doesPublicKeyExist(const Name& keyName) const |
| 478 | { |
| 479 | return m_pib->doesPublicKeyExist(keyName); |
| 480 | } |
| 481 | |
| 482 | void |
| 483 | addPublicKey(const Name& keyName, KeyType keyType, const PublicKey& publicKeyDer) |
| 484 | { |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 485 | return m_pib->addKey(keyName, publicKeyDer); |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 486 | } |
| 487 | |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 488 | void |
| 489 | addKey(const Name& keyName, const PublicKey& publicKeyDer) |
| 490 | { |
| 491 | return m_pib->addKey(keyName, publicKeyDer); |
| 492 | } |
| 493 | |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 494 | shared_ptr<PublicKey> |
| 495 | getPublicKey(const Name& keyName) const |
| 496 | { |
| 497 | return m_pib->getPublicKey(keyName); |
| 498 | } |
| 499 | |
| 500 | bool |
| 501 | doesCertificateExist(const Name& certificateName) const |
| 502 | { |
| 503 | return m_pib->doesCertificateExist(certificateName); |
| 504 | } |
| 505 | |
| 506 | void |
| 507 | addCertificate(const IdentityCertificate& certificate) |
| 508 | { |
| 509 | return m_pib->addCertificate(certificate); |
| 510 | } |
| 511 | |
| 512 | shared_ptr<IdentityCertificate> |
| 513 | getCertificate(const Name& certificateName) const |
| 514 | { |
| 515 | return m_pib->getCertificate(certificateName); |
| 516 | } |
| 517 | |
| 518 | Name |
| 519 | getDefaultIdentity() const |
| 520 | { |
| 521 | return m_pib->getDefaultIdentity(); |
| 522 | } |
| 523 | |
| 524 | Name |
| 525 | getDefaultKeyNameForIdentity(const Name& identityName) const |
| 526 | { |
| 527 | return m_pib->getDefaultKeyNameForIdentity(identityName); |
| 528 | } |
| 529 | |
José Quevedo | 641de4c | 2016-01-29 00:11:24 +0000 | [diff] [blame] | 530 | /** |
| 531 | * @brief Get default key parameters for the specified identity |
| 532 | * |
| 533 | * If identity has a previously generated key, the returned parameters |
| 534 | * will include the same type of the key. If there are no existing |
| 535 | * keys, DEFAULT_KEY_PARAMS is used. |
| 536 | */ |
| 537 | const KeyParams& |
| 538 | getDefaultKeyParamsForIdentity(const Name& identityName) const; |
| 539 | |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 540 | Name |
| 541 | getDefaultCertificateNameForKey(const Name& keyName) const |
| 542 | { |
| 543 | return m_pib->getDefaultCertificateNameForKey(keyName); |
| 544 | } |
| 545 | |
| 546 | void |
| 547 | getAllIdentities(std::vector<Name>& nameList, bool isDefault) const |
| 548 | { |
| 549 | return m_pib->getAllIdentities(nameList, isDefault); |
| 550 | } |
| 551 | |
| 552 | void |
| 553 | getAllKeyNames(std::vector<Name>& nameList, bool isDefault) const |
| 554 | { |
| 555 | return m_pib->getAllKeyNames(nameList, isDefault); |
| 556 | } |
| 557 | |
| 558 | void |
| 559 | getAllKeyNamesOfIdentity(const Name& identity, std::vector<Name>& nameList, bool isDefault) const |
| 560 | { |
| 561 | return m_pib->getAllKeyNamesOfIdentity(identity, nameList, isDefault); |
| 562 | } |
| 563 | |
| 564 | void |
| 565 | getAllCertificateNames(std::vector<Name>& nameList, bool isDefault) const |
| 566 | { |
| 567 | return m_pib->getAllCertificateNames(nameList, isDefault); |
| 568 | } |
| 569 | |
| 570 | void |
| 571 | getAllCertificateNamesOfKey(const Name& keyName, |
| 572 | std::vector<Name>& nameList, |
| 573 | bool isDefault) const |
| 574 | { |
| 575 | return m_pib->getAllCertificateNamesOfKey(keyName, nameList, isDefault); |
| 576 | } |
| 577 | |
| 578 | void |
| 579 | deleteCertificateInfo(const Name& certificateName) |
| 580 | { |
| 581 | return m_pib->deleteCertificateInfo(certificateName); |
| 582 | } |
| 583 | |
| 584 | void |
| 585 | deletePublicKeyInfo(const Name& keyName) |
| 586 | { |
| 587 | return m_pib->deletePublicKeyInfo(keyName); |
| 588 | } |
| 589 | |
| 590 | void |
| 591 | deleteIdentityInfo(const Name& identity) |
| 592 | { |
| 593 | return m_pib->deleteIdentityInfo(identity); |
| 594 | } |
| 595 | |
| 596 | void |
| 597 | setDefaultIdentity(const Name& identityName) |
| 598 | { |
| 599 | return m_pib->setDefaultIdentity(identityName); |
| 600 | } |
| 601 | |
| 602 | void |
| 603 | setDefaultKeyNameForIdentity(const Name& keyName) |
| 604 | { |
| 605 | return m_pib->setDefaultKeyNameForIdentity(keyName); |
| 606 | } |
| 607 | |
| 608 | void |
| 609 | setDefaultCertificateNameForKey(const Name& certificateName) |
| 610 | { |
| 611 | return m_pib->setDefaultCertificateNameForKey(certificateName); |
| 612 | } |
| 613 | |
| 614 | Name |
| 615 | getNewKeyName(const Name& identityName, bool useKsk) |
| 616 | { |
| 617 | return m_pib->getNewKeyName(identityName, useKsk); |
| 618 | } |
| 619 | |
| 620 | Name |
| 621 | getDefaultCertificateNameForIdentity(const Name& identityName) const |
| 622 | { |
| 623 | return m_pib->getDefaultCertificateNameForIdentity(identityName); |
| 624 | } |
| 625 | |
| 626 | Name |
| 627 | getDefaultCertificateName() const |
| 628 | { |
| 629 | return m_pib->getDefaultCertificateName(); |
| 630 | } |
| 631 | |
| 632 | void |
| 633 | addCertificateAsKeyDefault(const IdentityCertificate& certificate) |
| 634 | { |
| 635 | return m_pib->addCertificateAsKeyDefault(certificate); |
| 636 | } |
| 637 | |
| 638 | void |
| 639 | addCertificateAsIdentityDefault(const IdentityCertificate& certificate) |
| 640 | { |
| 641 | return m_pib->addCertificateAsIdentityDefault(certificate); |
| 642 | } |
| 643 | |
| 644 | void |
| 645 | addCertificateAsSystemDefault(const IdentityCertificate& certificate) |
| 646 | { |
| 647 | return m_pib->addCertificateAsSystemDefault(certificate); |
| 648 | } |
| 649 | |
| 650 | shared_ptr<IdentityCertificate> |
| 651 | getDefaultCertificate() const |
| 652 | { |
Alexander Afanasyev | aab7966 | 2014-07-07 17:35:34 -0700 | [diff] [blame] | 653 | if (!static_cast<bool>(m_pib->getDefaultCertificate())) |
| 654 | const_cast<KeyChain*>(this)->setDefaultCertificateInternal(); |
| 655 | |
| 656 | return m_pib->getDefaultCertificate(); |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 657 | } |
| 658 | |
| 659 | void |
| 660 | refreshDefaultCertificate() |
| 661 | { |
| 662 | return m_pib->refreshDefaultCertificate(); |
| 663 | } |
| 664 | |
| 665 | /******************************* |
| 666 | * Wrapper of SecTpm * |
| 667 | *******************************/ |
| 668 | |
| 669 | void |
| 670 | setTpmPassword(const uint8_t* password, size_t passwordLength) |
| 671 | { |
| 672 | return m_tpm->setTpmPassword(password, passwordLength); |
| 673 | } |
| 674 | |
| 675 | void |
| 676 | resetTpmPassword() |
| 677 | { |
| 678 | return m_tpm->resetTpmPassword(); |
| 679 | } |
| 680 | |
| 681 | void |
| 682 | setInTerminal(bool inTerminal) |
| 683 | { |
| 684 | return m_tpm->setInTerminal(inTerminal); |
| 685 | } |
| 686 | |
| 687 | bool |
| 688 | getInTerminal() const |
| 689 | { |
| 690 | return m_tpm->getInTerminal(); |
| 691 | } |
| 692 | |
| 693 | bool |
| 694 | isLocked() const |
| 695 | { |
| 696 | return m_tpm->isLocked(); |
| 697 | } |
| 698 | |
| 699 | bool |
| 700 | unlockTpm(const char* password, size_t passwordLength, bool usePassword) |
| 701 | { |
| 702 | return m_tpm->unlockTpm(password, passwordLength, usePassword); |
| 703 | } |
| 704 | |
| 705 | void |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 706 | generateKeyPairInTpm(const Name& keyName, const KeyParams& params) |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 707 | { |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 708 | return m_tpm->generateKeyPairInTpm(keyName, params); |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 709 | } |
| 710 | |
| 711 | void |
| 712 | deleteKeyPairInTpm(const Name& keyName) |
| 713 | { |
| 714 | return m_tpm->deleteKeyPairInTpm(keyName); |
| 715 | } |
| 716 | |
| 717 | shared_ptr<PublicKey> |
| 718 | getPublicKeyFromTpm(const Name& keyName) const |
| 719 | { |
| 720 | return m_tpm->getPublicKeyFromTpm(keyName); |
| 721 | } |
| 722 | |
| 723 | Block |
| 724 | signInTpm(const uint8_t* data, size_t dataLength, |
| 725 | const Name& keyName, |
| 726 | DigestAlgorithm digestAlgorithm) |
| 727 | { |
| 728 | return m_tpm->signInTpm(data, dataLength, keyName, digestAlgorithm); |
| 729 | } |
| 730 | |
| 731 | ConstBufferPtr |
| 732 | decryptInTpm(const uint8_t* data, size_t dataLength, const Name& keyName, bool isSymmetric) |
| 733 | { |
| 734 | return m_tpm->decryptInTpm(data, dataLength, keyName, isSymmetric); |
| 735 | } |
| 736 | |
| 737 | ConstBufferPtr |
| 738 | encryptInTpm(const uint8_t* data, size_t dataLength, const Name& keyName, bool isSymmetric) |
| 739 | { |
| 740 | return m_tpm->encryptInTpm(data, dataLength, keyName, isSymmetric); |
| 741 | } |
| 742 | |
| 743 | void |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 744 | generateSymmetricKeyInTpm(const Name& keyName, const KeyParams& params) |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 745 | { |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 746 | return m_tpm->generateSymmetricKeyInTpm(keyName, params); |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 747 | } |
| 748 | |
| 749 | bool |
| 750 | doesKeyExistInTpm(const Name& keyName, KeyClass keyClass) const |
| 751 | { |
| 752 | return m_tpm->doesKeyExistInTpm(keyName, keyClass); |
| 753 | } |
| 754 | |
| 755 | bool |
| 756 | generateRandomBlock(uint8_t* res, size_t size) const |
| 757 | { |
| 758 | return m_tpm->generateRandomBlock(res, size); |
| 759 | } |
| 760 | |
| 761 | void |
| 762 | addAppToAcl(const Name& keyName, KeyClass keyClass, const std::string& appPath, AclType acl) |
| 763 | { |
| 764 | return m_tpm->addAppToAcl(keyName, keyClass, appPath, acl); |
| 765 | } |
| 766 | |
| 767 | ConstBufferPtr |
| 768 | exportPrivateKeyPkcs5FromTpm(const Name& keyName, const std::string& password) |
| 769 | { |
| 770 | return m_tpm->exportPrivateKeyPkcs5FromTpm(keyName, password); |
| 771 | } |
| 772 | |
| 773 | bool |
| 774 | importPrivateKeyPkcs5IntoTpm(const Name& keyName, |
| 775 | const uint8_t* buf, size_t size, |
| 776 | const std::string& password) |
| 777 | { |
| 778 | return m_tpm->importPrivateKeyPkcs5IntoTpm(keyName, buf, size, password); |
| 779 | } |
Jeff Thompson | 8efe5ad | 2013-08-20 17:36:38 -0700 | [diff] [blame] | 780 | |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 781 | private: |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 782 | void |
Alexander Afanasyev | 0711380 | 2015-01-15 19:14:36 -0800 | [diff] [blame] | 783 | initialize(const std::string& pibLocatorUri, |
| 784 | const std::string& tpmLocatorUri, |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 785 | bool needReset); |
| 786 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 787 | /** |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 788 | * @brief Prepare a SignatureInfo TLV according to signing information and return the signing key name |
Yingdi Yu | c8f883c | 2014-06-20 23:25:22 -0700 | [diff] [blame] | 789 | * |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 790 | * @param params The signing parameters. |
| 791 | * @return The signing key name and prepared SignatureInfo. |
| 792 | * @throw Error when the requested signing method cannot be satisfied. |
Yingdi Yu | c8f883c | 2014-06-20 23:25:22 -0700 | [diff] [blame] | 793 | */ |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 794 | std::tuple<Name, SignatureInfo> |
| 795 | prepareSignatureInfo(const SigningInfo& params); |
| 796 | |
| 797 | /** |
| 798 | * @brief Internal abstraction of packet signing. |
| 799 | * |
| 800 | * @param packet The packet to sign |
| 801 | * @param params The signing parameters. |
| 802 | * @throw Error when the signing fails. |
| 803 | */ |
| 804 | template<typename T> |
| 805 | void |
| 806 | signImpl(T& packet, const SigningInfo& params); |
Yingdi Yu | c8f883c | 2014-06-20 23:25:22 -0700 | [diff] [blame] | 807 | |
| 808 | /** |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 809 | * @brief Set default certificate if it is not initialized |
| 810 | */ |
| 811 | void |
| 812 | setDefaultCertificateInternal(); |
| 813 | |
| 814 | /** |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 815 | * @brief Generate a key pair for the specified identity. |
| 816 | * |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 817 | * @param identityName The name of the specified identity. |
| 818 | * @param isKsk true for generating a Key-Signing-Key (KSK), false for a Data-Signing-Key (KSK). |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 819 | * @param params The parameter of the key. |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 820 | * @return The name of the generated key. |
| 821 | */ |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 822 | Name |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 823 | generateKeyPair(const Name& identityName, bool isKsk = false, |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 824 | const KeyParams& params = DEFAULT_KEY_PARAMS); |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 825 | |
Yingdi Yu | 8726f65 | 2014-01-23 10:35:12 -0800 | [diff] [blame] | 826 | /** |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 827 | * @brief Sign the data using a particular key. |
| 828 | * |
| 829 | * @param data Reference to the data packet. |
| 830 | * @param signature Signature to be added. |
Yingdi Yu | 8726f65 | 2014-01-23 10:35:12 -0800 | [diff] [blame] | 831 | * @param keyName The name of the signing key. |
| 832 | * @param digestAlgorithm the digest algorithm. |
| 833 | * @throws Tpm::Error |
Alexander Afanasyev | b78bc4d | 2014-04-09 21:20:52 -0700 | [diff] [blame] | 834 | */ |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 835 | void |
Yingdi Yu | c8f883c | 2014-06-20 23:25:22 -0700 | [diff] [blame] | 836 | signPacketWrapper(Data& data, const Signature& signature, |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 837 | const Name& keyName, DigestAlgorithm digestAlgorithm); |
Yingdi Yu | 8726f65 | 2014-01-23 10:35:12 -0800 | [diff] [blame] | 838 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 839 | /** |
| 840 | * @brief Sign the interest using a particular key. |
| 841 | * |
| 842 | * @param interest Reference to the interest packet. |
| 843 | * @param signature Signature to be added. |
| 844 | * @param keyName The name of the signing key. |
| 845 | * @param digestAlgorithm the digest algorithm. |
| 846 | * @throws Tpm::Error |
Alexander Afanasyev | b78bc4d | 2014-04-09 21:20:52 -0700 | [diff] [blame] | 847 | */ |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 848 | void |
Yingdi Yu | c8f883c | 2014-06-20 23:25:22 -0700 | [diff] [blame] | 849 | signPacketWrapper(Interest& interest, const Signature& signature, |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 850 | const Name& keyName, DigestAlgorithm digestAlgorithm); |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 851 | |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 852 | /** |
| 853 | * @brief Generate a SignatureValue block for a buffer @p buf with size @p size using |
| 854 | * a key with name @p keyName and digest algorithm @p digestAlgorithm. |
| 855 | */ |
| 856 | Block |
| 857 | pureSign(const uint8_t* buf, size_t size, const Name& keyName, DigestAlgorithm digestAlgorithm) const; |
| 858 | |
Alexander Afanasyev | 0711380 | 2015-01-15 19:14:36 -0800 | [diff] [blame] | 859 | static void |
Alexander Afanasyev | 34a3763 | 2015-01-16 17:37:36 -0800 | [diff] [blame] | 860 | registerPibImpl(const std::string& canonicalName, |
| 861 | std::initializer_list<std::string> aliases, PibCreateFunc createFunc); |
Alexander Afanasyev | 0711380 | 2015-01-15 19:14:36 -0800 | [diff] [blame] | 862 | |
| 863 | static void |
Alexander Afanasyev | 34a3763 | 2015-01-16 17:37:36 -0800 | [diff] [blame] | 864 | registerTpmImpl(const std::string& canonicalName, |
| 865 | std::initializer_list<std::string> aliases, TpmCreateFunc createFunc); |
Alexander Afanasyev | 0711380 | 2015-01-15 19:14:36 -0800 | [diff] [blame] | 866 | |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 867 | public: |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 868 | static tlv::SignatureTypeValue |
| 869 | getSignatureType(KeyType keyType, DigestAlgorithm digestAlgorithm); |
| 870 | |
| 871 | public: |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 872 | static const Name DEFAULT_PREFIX; |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 873 | static const SigningInfo DEFAULT_SIGNING_INFO; |
| 874 | |
| 875 | /** |
| 876 | * @brief A localhost identity which indicates that signature is generated using SHA-256. |
| 877 | * @todo Passing this as identity is not implemented. |
| 878 | */ |
| 879 | static const Name DIGEST_SHA256_IDENTITY; |
| 880 | |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 881 | // RsaKeyParams is set to be default for backward compatibility. |
| 882 | static const RsaKeyParams DEFAULT_KEY_PARAMS; |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 883 | |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 884 | typedef std::map<std::string, Block> SignParams; |
| 885 | |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 886 | private: |
Alexander Afanasyev | 0711380 | 2015-01-15 19:14:36 -0800 | [diff] [blame] | 887 | std::unique_ptr<SecPublicInfo> m_pib; |
| 888 | std::unique_ptr<SecTpm> m_tpm; |
Yingdi Yu | 0f5fb69 | 2014-06-10 12:07:28 -0700 | [diff] [blame] | 889 | time::milliseconds m_lastTimestamp; |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 890 | }; |
| 891 | |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 892 | template<typename T> |
| 893 | void |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 894 | KeyChain::signImpl(T& packet, const SigningInfo& params) |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 895 | { |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 896 | Name keyName; |
| 897 | SignatureInfo sigInfo; |
| 898 | std::tie(keyName, sigInfo) = prepareSignatureInfo(params); |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 899 | |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 900 | signPacketWrapper(packet, Signature(sigInfo), |
| 901 | keyName, params.getDigestAlgorithm()); |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 902 | } |
| 903 | |
| 904 | template<typename T> |
| 905 | void |
| 906 | KeyChain::sign(T& packet, const Name& certificateName) |
| 907 | { |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 908 | signImpl(packet, SigningInfo(SigningInfo::SIGNER_TYPE_CERT, certificateName)); |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 909 | } |
| 910 | |
| 911 | template<typename T> |
| 912 | void |
| 913 | KeyChain::signByIdentity(T& packet, const Name& identityName) |
| 914 | { |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 915 | signImpl(packet, SigningInfo(SigningInfo::SIGNER_TYPE_ID, identityName)); |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 916 | } |
| 917 | |
Alexander Afanasyev | 0711380 | 2015-01-15 19:14:36 -0800 | [diff] [blame] | 918 | template<class PibType> |
| 919 | inline void |
Alexander Afanasyev | 34a3763 | 2015-01-16 17:37:36 -0800 | [diff] [blame] | 920 | KeyChain::registerPib(std::initializer_list<std::string> aliases) |
Alexander Afanasyev | 0711380 | 2015-01-15 19:14:36 -0800 | [diff] [blame] | 921 | { |
Alexander Afanasyev | 34a3763 | 2015-01-16 17:37:36 -0800 | [diff] [blame] | 922 | registerPibImpl(*aliases.begin(), aliases, [] (const std::string& locator) { |
Davide Pesavento | 409cc20 | 2015-09-19 14:13:16 +0200 | [diff] [blame] | 923 | return make_unique<PibType>(locator); |
Alexander Afanasyev | 0711380 | 2015-01-15 19:14:36 -0800 | [diff] [blame] | 924 | }); |
| 925 | } |
| 926 | |
| 927 | template<class TpmType> |
| 928 | inline void |
Alexander Afanasyev | 34a3763 | 2015-01-16 17:37:36 -0800 | [diff] [blame] | 929 | KeyChain::registerTpm(std::initializer_list<std::string> aliases) |
Alexander Afanasyev | 0711380 | 2015-01-15 19:14:36 -0800 | [diff] [blame] | 930 | { |
Alexander Afanasyev | 34a3763 | 2015-01-16 17:37:36 -0800 | [diff] [blame] | 931 | registerTpmImpl(*aliases.begin(), aliases, [] (const std::string& locator) { |
Davide Pesavento | 409cc20 | 2015-09-19 14:13:16 +0200 | [diff] [blame] | 932 | return make_unique<TpmType>(locator); |
Alexander Afanasyev | 0711380 | 2015-01-15 19:14:36 -0800 | [diff] [blame] | 933 | }); |
| 934 | } |
| 935 | |
| 936 | /** |
| 937 | * \brief Register SecPib class in ndn-cxx KeyChain |
| 938 | * |
| 939 | * This macro should be placed once in the implementation file of the |
| 940 | * SecPib type within the namespace where the type is declared. |
| 941 | */ |
Alexander Afanasyev | 34a3763 | 2015-01-16 17:37:36 -0800 | [diff] [blame] | 942 | #define NDN_CXX_KEYCHAIN_REGISTER_PIB(PibType, ...) \ |
Alexander Afanasyev | 0711380 | 2015-01-15 19:14:36 -0800 | [diff] [blame] | 943 | static class NdnCxxAuto ## PibType ## PibRegistrationClass \ |
| 944 | { \ |
| 945 | public: \ |
| 946 | NdnCxxAuto ## PibType ## PibRegistrationClass() \ |
| 947 | { \ |
| 948 | ::ndn::KeyChain::registerPib<PibType>({__VA_ARGS__}); \ |
| 949 | } \ |
| 950 | } ndnCxxAuto ## PibType ## PibRegistrationVariable |
| 951 | |
| 952 | /** |
| 953 | * \brief Register SecTpm class in ndn-cxx KeyChain |
| 954 | * |
| 955 | * This macro should be placed once in the implementation file of the |
| 956 | * SecTpm type within the namespace where the type is declared. |
| 957 | */ |
Alexander Afanasyev | 34a3763 | 2015-01-16 17:37:36 -0800 | [diff] [blame] | 958 | #define NDN_CXX_KEYCHAIN_REGISTER_TPM(TpmType, ...) \ |
Alexander Afanasyev | 0711380 | 2015-01-15 19:14:36 -0800 | [diff] [blame] | 959 | static class NdnCxxAuto ## TpmType ## TpmRegistrationClass \ |
| 960 | { \ |
| 961 | public: \ |
| 962 | NdnCxxAuto ## TpmType ## TpmRegistrationClass() \ |
| 963 | { \ |
| 964 | ::ndn::KeyChain::registerTpm<TpmType>({__VA_ARGS__}); \ |
| 965 | } \ |
| 966 | } ndnCxxAuto ## TpmType ## TpmRegistrationVariable |
| 967 | |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 968 | } // namespace security |
| 969 | |
| 970 | using security::KeyChain; |
| 971 | |
Yingdi Yu | 6ab6781 | 2014-11-27 15:00:34 -0800 | [diff] [blame] | 972 | } // namespace ndn |
Jeff Thompson | 47c93cf | 2013-08-09 00:38:48 -0700 | [diff] [blame] | 973 | |
Alexander Afanasyev | 766cea7 | 2014-04-24 19:16:42 -0700 | [diff] [blame] | 974 | #endif // NDN_SECURITY_KEY_CHAIN_HPP |