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 | /** |
Alexander Afanasyev | 34a3763 | 2015-01-16 17:37:36 -0800 | [diff] [blame] | 3 | * Copyright (c) 2013-2015 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 |
Yingdi Yu | c8f883c | 2014-06-20 23:25:22 -0700 | [diff] [blame] | 193 | generateRsaKeyPairAsDefault(const Name& identityName, bool isKsk = false, |
| 194 | uint32_t keySize = 2048); |
| 195 | |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 196 | /** |
| 197 | * @brief Generate a pair of ECDSA keys for the specified identity and set it as default key for |
| 198 | * the identity. |
| 199 | * |
| 200 | * @param identityName The name of the identity. |
| 201 | * @param isKsk true for generating a Key-Signing-Key (KSK), false for a Data-Signing-Key (KSK). |
| 202 | * @param keySize The size of the key. |
| 203 | * @return The generated key name. |
| 204 | * @see generateRsaKeyPair, generateEcdsaKeyPair, generateRsaKeyPairAsDefault |
| 205 | */ |
Yingdi Yu | c8f883c | 2014-06-20 23:25:22 -0700 | [diff] [blame] | 206 | Name |
| 207 | generateEcdsaKeyPairAsDefault(const Name& identityName, bool isKsk, uint32_t keySize = 256); |
Jeff Thompson | 79a2d5d | 2013-09-27 14:32:23 -0700 | [diff] [blame] | 208 | |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 209 | /** |
Yingdi Yu | c55680b | 2014-02-26 12:31:35 -0800 | [diff] [blame] | 210 | * @brief prepare an unsigned identity certificate |
| 211 | * |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 212 | * @param keyName Key name, e.g., `/<identity_name>/ksk-123456`. |
Yingdi Yu | c55680b | 2014-02-26 12:31:35 -0800 | [diff] [blame] | 213 | * @param signingIdentity The signing identity. |
| 214 | * @param notBefore Refer to IdentityCertificate. |
| 215 | * @param notAfter Refer to IdentityCertificate. |
Alexander Afanasyev | b78bc4d | 2014-04-09 21:20:52 -0700 | [diff] [blame] | 216 | * @param subjectDescription Refer to IdentityCertificate. |
Yingdi Yu | 0eb5d72 | 2014-06-10 15:06:25 -0700 | [diff] [blame] | 217 | * @param certPrefix Prefix before `KEY` component. By default, KeyChain will infer the |
| 218 | * certificate name according to the relation between the signingIdentity and |
| 219 | * the subject identity. If signingIdentity is a prefix of the subject identity, |
| 220 | * `KEY` will be inserted after the signingIdentity, otherwise `KEY` is inserted |
| 221 | * after subject identity (i.e., before `ksk-....`). |
Yingdi Yu | c55680b | 2014-02-26 12:31:35 -0800 | [diff] [blame] | 222 | * @return IdentityCertificate. |
| 223 | */ |
| 224 | shared_ptr<IdentityCertificate> |
| 225 | prepareUnsignedIdentityCertificate(const Name& keyName, |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 226 | const Name& signingIdentity, |
| 227 | const time::system_clock::TimePoint& notBefore, |
| 228 | const time::system_clock::TimePoint& notAfter, |
Yingdi Yu | 0eb5d72 | 2014-06-10 15:06:25 -0700 | [diff] [blame] | 229 | const std::vector<CertificateSubjectDescription>& subjectDescription, |
| 230 | const Name& certPrefix = DEFAULT_PREFIX); |
| 231 | |
| 232 | /** |
| 233 | * @brief prepare an unsigned identity certificate |
| 234 | * |
| 235 | * @param keyName Key name, e.g., `/<identity_name>/ksk-123456`. |
| 236 | * @param publicKey Public key to sign. |
| 237 | * @param signingIdentity The signing identity. |
| 238 | * @param notBefore Refer to IdentityCertificate. |
| 239 | * @param notAfter Refer to IdentityCertificate. |
| 240 | * @param subjectDescription Refer to IdentityCertificate. |
| 241 | * @param certPrefix Prefix before `KEY` component. By default, KeyChain will infer the |
| 242 | * certificate name according to the relation between the signingIdentity and |
| 243 | * the subject identity. If signingIdentity is a prefix of the subject identity, |
| 244 | * `KEY` will be inserted after the signingIdentity, otherwise `KEY` is inserted |
| 245 | * after subject identity (i.e., before `ksk-....`). |
| 246 | * @return IdentityCertificate. |
| 247 | */ |
| 248 | shared_ptr<IdentityCertificate> |
| 249 | prepareUnsignedIdentityCertificate(const Name& keyName, |
| 250 | const PublicKey& publicKey, |
| 251 | const Name& signingIdentity, |
| 252 | const time::system_clock::TimePoint& notBefore, |
| 253 | const time::system_clock::TimePoint& notAfter, |
| 254 | const std::vector<CertificateSubjectDescription>& subjectDescription, |
| 255 | const Name& certPrefix = DEFAULT_PREFIX); |
Yingdi Yu | c55680b | 2014-02-26 12:31:35 -0800 | [diff] [blame] | 256 | |
| 257 | /** |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 258 | * @brief Sign data according to the supplied signing information |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 259 | * |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 260 | * This method uses the supplied signing information @p params to create the SignatureInfo block: |
| 261 | * - it selects a private key and its certificate to sign the packet |
| 262 | * - sets the KeyLocator field with the certificate name, and |
| 263 | * - adds other requested information to the SignatureInfo block). |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 264 | * |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 265 | * After that, the method assigns the created SignatureInfo to the data packets, generate a |
| 266 | * signature and sets as part of the SignatureValue block. |
| 267 | * |
| 268 | * @param data The data to sign |
| 269 | * @param params The signing parameters. |
| 270 | * @throws Error if signing fails. |
| 271 | * @see SigningInfo |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 272 | */ |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 273 | void |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 274 | sign(Data& data, const SigningInfo& params = DEFAULT_SIGNING_INFO); |
Alexander Afanasyev | b78bc4d | 2014-04-09 21:20:52 -0700 | [diff] [blame] | 275 | |
Jeff Thompson | 47c93cf | 2013-08-09 00:38:48 -0700 | [diff] [blame] | 276 | /** |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 277 | * @brief Sign interest according to the supplied signing information |
| 278 | * |
| 279 | * This method uses the supplied signing information @p params to create the SignatureInfo block: |
| 280 | * - it selects a private key and its certificate to sign the packet |
| 281 | * - sets the KeyLocator field with the certificate name, and |
| 282 | * - adds other requested information to the SignatureInfo block). |
| 283 | * |
| 284 | * After that, the method appends the created SignatureInfo to the interest name, generate a |
| 285 | * signature and appends it as part of the SignatureValue block to the interest name. |
| 286 | * |
| 287 | * @param interest The interest to sign |
| 288 | * @param params The signing parameters. |
| 289 | * @throws Error if signing fails. |
| 290 | * @see SigningInfo |
| 291 | */ |
| 292 | void |
| 293 | sign(Interest& interest, const SigningInfo& params = DEFAULT_SIGNING_INFO); |
| 294 | |
| 295 | /** |
| 296 | * @brief Sign buffer according to the supplied signing information |
| 297 | * |
| 298 | * @param buffer The buffer to sign |
| 299 | * @param bufferLength The buffer size |
| 300 | * @param params The signing parameters. |
| 301 | * @return a SignatureValue TLV block |
| 302 | * @throws Error if signing fails. |
| 303 | * @see SigningInfo |
| 304 | */ |
| 305 | Block |
| 306 | sign(const uint8_t* buffer, size_t bufferLength, const SigningInfo& params); |
| 307 | |
| 308 | /** |
| 309 | * @deprecated use sign sign(T&, const SigningInfo&) |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 310 | * @brief Sign packet with a particular certificate. |
| 311 | * |
| 312 | * @param packet The packet to be signed. |
| 313 | * @param certificateName The certificate name of the key to use for signing. |
| 314 | * @throws SecPublicInfo::Error if certificate does not exist. |
Jeff Thompson | 3c73da4 | 2013-08-12 11:19:05 -0700 | [diff] [blame] | 315 | */ |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 316 | template<typename T> |
Alexander Afanasyev | b78bc4d | 2014-04-09 21:20:52 -0700 | [diff] [blame] | 317 | void |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 318 | sign(T& packet, const Name& certificateName); |
Alexander Afanasyev | b78bc4d | 2014-04-09 21:20:52 -0700 | [diff] [blame] | 319 | |
Jeff Thompson | 29ce310 | 2013-09-27 11:47:48 -0700 | [diff] [blame] | 320 | /** |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 321 | * @deprecated Use sign(const uint8_t*, size_t, const SigningInfo&) instead |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 322 | * @brief Sign the byte array using a particular certificate. |
| 323 | * |
Jeff Thompson | c01e178 | 2013-10-21 14:08:42 -0700 | [diff] [blame] | 324 | * @param buffer The byte array to be signed. |
| 325 | * @param bufferLength the length of buffer. |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 326 | * @param certificateName The certificate name of the signing key. |
Jeff Thompson | c01e178 | 2013-10-21 14:08:42 -0700 | [diff] [blame] | 327 | * @return The Signature. |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 328 | * @throws SecPublicInfo::Error if certificate does not exist. |
Jeff Thompson | c01e178 | 2013-10-21 14:08:42 -0700 | [diff] [blame] | 329 | */ |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 330 | Signature |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 331 | sign(const uint8_t* buffer, size_t bufferLength, const Name& certificateName); |
Jeff Thompson | c01e178 | 2013-10-21 14:08:42 -0700 | [diff] [blame] | 332 | |
| 333 | /** |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 334 | * @deprecated use sign sign(T&, const SigningInfo&) |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 335 | * @brief Sign packet using the default certificate of a particular identity. |
| 336 | * |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 337 | * If there is no default certificate of that identity, this method will create a self-signed |
| 338 | * certificate. |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 339 | * |
| 340 | * @param packet The packet to be signed. |
| 341 | * @param identityName The signing identity name. |
Jeff Thompson | 29ce310 | 2013-09-27 11:47:48 -0700 | [diff] [blame] | 342 | */ |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 343 | template<typename T> |
Alexander Afanasyev | b78bc4d | 2014-04-09 21:20:52 -0700 | [diff] [blame] | 344 | void |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 345 | signByIdentity(T& packet, const Name& identityName); |
Alexander Afanasyev | b78bc4d | 2014-04-09 21:20:52 -0700 | [diff] [blame] | 346 | |
Jeff Thompson | 3c73da4 | 2013-08-12 11:19:05 -0700 | [diff] [blame] | 347 | /** |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 348 | * @deprecated use sign(const uint8_t*, size_t, const SigningInfo&) instead |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 349 | * @brief Sign the byte array using the default certificate of a particular identity. |
| 350 | * |
Jeff Thompson | c01e178 | 2013-10-21 14:08:42 -0700 | [diff] [blame] | 351 | * @param buffer The byte array to be signed. |
| 352 | * @param bufferLength the length of buffer. |
| 353 | * @param identityName The identity name. |
| 354 | * @return The Signature. |
| 355 | */ |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 356 | Signature |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 357 | signByIdentity(const uint8_t* buffer, size_t bufferLength, const Name& identityName); |
Jeff Thompson | c01e178 | 2013-10-21 14:08:42 -0700 | [diff] [blame] | 358 | |
| 359 | /** |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 360 | * @deprecated use sign(Data&, SigningInfo(SigningInfo::SIGNER_TYPE_SHA256)) |
Yingdi Yu | 6ab6781 | 2014-11-27 15:00:34 -0800 | [diff] [blame] | 361 | * @brief Set Sha256 weak signature for @p data |
Yingdi Yu | 2115716 | 2014-02-28 13:02:34 -0800 | [diff] [blame] | 362 | */ |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 363 | void |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 364 | signWithSha256(Data& data); |
Yingdi Yu | 2115716 | 2014-02-28 13:02:34 -0800 | [diff] [blame] | 365 | |
| 366 | /** |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 367 | * @deprecated use sign(Interest&, SigningInfo(SigningInfo::SIGNER_TYPE_SHA256)) |
Yingdi Yu | 6ab6781 | 2014-11-27 15:00:34 -0800 | [diff] [blame] | 368 | * @brief Set Sha256 weak signature for @p interest |
| 369 | */ |
| 370 | void |
| 371 | signWithSha256(Interest& interest); |
| 372 | |
| 373 | /** |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 374 | * @brief Generate a self-signed certificate for a public key. |
| 375 | * |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 376 | * @param keyName The name of the public key |
| 377 | * @return The generated certificate, shared_ptr<IdentityCertificate>() if selfSign fails |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 378 | */ |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 379 | shared_ptr<IdentityCertificate> |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 380 | selfSign(const Name& keyName); |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 381 | |
| 382 | /** |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 383 | * @brief Self-sign the supplied identity certificate. |
| 384 | * |
| 385 | * @param cert The supplied cert. |
| 386 | * @throws SecTpm::Error if the private key does not exist. |
Jeff Thompson | 8efe5ad | 2013-08-20 17:36:38 -0700 | [diff] [blame] | 387 | */ |
Jeff Thompson | 2ce8f49 | 2013-09-17 18:01:25 -0700 | [diff] [blame] | 388 | void |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 389 | selfSign(IdentityCertificate& cert); |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 390 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 391 | /** |
| 392 | * @brief delete a certificate. |
| 393 | * |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 394 | * @param certificateName The certificate to be deleted. |
Yingdi Yu | 6147ef4 | 2014-12-08 17:48:32 -0800 | [diff] [blame] | 395 | * @throws KeyChain::Error if certificate cannot be deleted. |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 396 | */ |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 397 | void |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 398 | deleteCertificate(const Name& certificateName); |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 399 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 400 | /** |
| 401 | * @brief delete a key. |
| 402 | * |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 403 | * @param keyName The key to be deleted. |
Yingdi Yu | 6147ef4 | 2014-12-08 17:48:32 -0800 | [diff] [blame] | 404 | * @throws KeyChain::Error if key cannot be deleted. |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 405 | */ |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 406 | void |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 407 | deleteKey(const Name& keyName); |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 408 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 409 | /** |
| 410 | * @brief delete an identity. |
| 411 | * |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 412 | * @param identity The identity to be deleted. |
Yingdi Yu | 6147ef4 | 2014-12-08 17:48:32 -0800 | [diff] [blame] | 413 | * @throws KeyChain::Error if identity cannot be deleted. |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 414 | */ |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 415 | void |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 416 | deleteIdentity(const Name& identity); |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 417 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 418 | /** |
| 419 | * @brief export an identity. |
| 420 | * |
| 421 | * @param identity The identity to export. |
| 422 | * @param passwordStr The password to secure the private key. |
Yingdi Yu | 64c3fb4 | 2014-02-26 17:30:04 -0800 | [diff] [blame] | 423 | * @return The encoded export data. |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 424 | * @throws SecPublicInfo::Error if anything goes wrong in exporting. |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 425 | */ |
Yingdi Yu | 64c3fb4 | 2014-02-26 17:30:04 -0800 | [diff] [blame] | 426 | shared_ptr<SecuredBag> |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 427 | exportIdentity(const Name& identity, const std::string& passwordStr); |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 428 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 429 | /** |
| 430 | * @brief import an identity. |
| 431 | * |
Yingdi Yu | 64c3fb4 | 2014-02-26 17:30:04 -0800 | [diff] [blame] | 432 | * @param securedBag The encoded import data. |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 433 | * @param passwordStr The password to secure the private key. |
| 434 | */ |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 435 | void |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 436 | importIdentity(const SecuredBag& securedBag, const std::string& passwordStr); |
| 437 | |
| 438 | SecPublicInfo& |
| 439 | getPib() |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 440 | { |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 441 | return *m_pib; |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 442 | } |
| 443 | |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 444 | const SecPublicInfo& |
| 445 | getPib() const |
| 446 | { |
| 447 | return *m_pib; |
| 448 | } |
| 449 | |
| 450 | SecTpm& |
| 451 | getTpm() |
| 452 | { |
| 453 | return *m_tpm; |
| 454 | } |
| 455 | |
| 456 | const SecTpm& |
| 457 | getTpm() const |
| 458 | { |
| 459 | return *m_tpm; |
| 460 | } |
| 461 | |
| 462 | /******************************* |
| 463 | * Wrapper of SecPublicInfo * |
| 464 | *******************************/ |
| 465 | bool |
| 466 | doesIdentityExist(const Name& identityName) const |
| 467 | { |
| 468 | return m_pib->doesIdentityExist(identityName); |
| 469 | } |
| 470 | |
| 471 | void |
| 472 | addIdentity(const Name& identityName) |
| 473 | { |
| 474 | return m_pib->addIdentity(identityName); |
| 475 | } |
| 476 | |
| 477 | bool |
| 478 | doesPublicKeyExist(const Name& keyName) const |
| 479 | { |
| 480 | return m_pib->doesPublicKeyExist(keyName); |
| 481 | } |
| 482 | |
| 483 | void |
| 484 | addPublicKey(const Name& keyName, KeyType keyType, const PublicKey& publicKeyDer) |
| 485 | { |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 486 | return m_pib->addKey(keyName, publicKeyDer); |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 487 | } |
| 488 | |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 489 | void |
| 490 | addKey(const Name& keyName, const PublicKey& publicKeyDer) |
| 491 | { |
| 492 | return m_pib->addKey(keyName, publicKeyDer); |
| 493 | } |
| 494 | |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 495 | shared_ptr<PublicKey> |
| 496 | getPublicKey(const Name& keyName) const |
| 497 | { |
| 498 | return m_pib->getPublicKey(keyName); |
| 499 | } |
| 500 | |
| 501 | bool |
| 502 | doesCertificateExist(const Name& certificateName) const |
| 503 | { |
| 504 | return m_pib->doesCertificateExist(certificateName); |
| 505 | } |
| 506 | |
| 507 | void |
| 508 | addCertificate(const IdentityCertificate& certificate) |
| 509 | { |
| 510 | return m_pib->addCertificate(certificate); |
| 511 | } |
| 512 | |
| 513 | shared_ptr<IdentityCertificate> |
| 514 | getCertificate(const Name& certificateName) const |
| 515 | { |
| 516 | return m_pib->getCertificate(certificateName); |
| 517 | } |
| 518 | |
| 519 | Name |
| 520 | getDefaultIdentity() const |
| 521 | { |
| 522 | return m_pib->getDefaultIdentity(); |
| 523 | } |
| 524 | |
| 525 | Name |
| 526 | getDefaultKeyNameForIdentity(const Name& identityName) const |
| 527 | { |
| 528 | return m_pib->getDefaultKeyNameForIdentity(identityName); |
| 529 | } |
| 530 | |
| 531 | Name |
| 532 | getDefaultCertificateNameForKey(const Name& keyName) const |
| 533 | { |
| 534 | return m_pib->getDefaultCertificateNameForKey(keyName); |
| 535 | } |
| 536 | |
| 537 | void |
| 538 | getAllIdentities(std::vector<Name>& nameList, bool isDefault) const |
| 539 | { |
| 540 | return m_pib->getAllIdentities(nameList, isDefault); |
| 541 | } |
| 542 | |
| 543 | void |
| 544 | getAllKeyNames(std::vector<Name>& nameList, bool isDefault) const |
| 545 | { |
| 546 | return m_pib->getAllKeyNames(nameList, isDefault); |
| 547 | } |
| 548 | |
| 549 | void |
| 550 | getAllKeyNamesOfIdentity(const Name& identity, std::vector<Name>& nameList, bool isDefault) const |
| 551 | { |
| 552 | return m_pib->getAllKeyNamesOfIdentity(identity, nameList, isDefault); |
| 553 | } |
| 554 | |
| 555 | void |
| 556 | getAllCertificateNames(std::vector<Name>& nameList, bool isDefault) const |
| 557 | { |
| 558 | return m_pib->getAllCertificateNames(nameList, isDefault); |
| 559 | } |
| 560 | |
| 561 | void |
| 562 | getAllCertificateNamesOfKey(const Name& keyName, |
| 563 | std::vector<Name>& nameList, |
| 564 | bool isDefault) const |
| 565 | { |
| 566 | return m_pib->getAllCertificateNamesOfKey(keyName, nameList, isDefault); |
| 567 | } |
| 568 | |
| 569 | void |
| 570 | deleteCertificateInfo(const Name& certificateName) |
| 571 | { |
| 572 | return m_pib->deleteCertificateInfo(certificateName); |
| 573 | } |
| 574 | |
| 575 | void |
| 576 | deletePublicKeyInfo(const Name& keyName) |
| 577 | { |
| 578 | return m_pib->deletePublicKeyInfo(keyName); |
| 579 | } |
| 580 | |
| 581 | void |
| 582 | deleteIdentityInfo(const Name& identity) |
| 583 | { |
| 584 | return m_pib->deleteIdentityInfo(identity); |
| 585 | } |
| 586 | |
| 587 | void |
| 588 | setDefaultIdentity(const Name& identityName) |
| 589 | { |
| 590 | return m_pib->setDefaultIdentity(identityName); |
| 591 | } |
| 592 | |
| 593 | void |
| 594 | setDefaultKeyNameForIdentity(const Name& keyName) |
| 595 | { |
| 596 | return m_pib->setDefaultKeyNameForIdentity(keyName); |
| 597 | } |
| 598 | |
| 599 | void |
| 600 | setDefaultCertificateNameForKey(const Name& certificateName) |
| 601 | { |
| 602 | return m_pib->setDefaultCertificateNameForKey(certificateName); |
| 603 | } |
| 604 | |
| 605 | Name |
| 606 | getNewKeyName(const Name& identityName, bool useKsk) |
| 607 | { |
| 608 | return m_pib->getNewKeyName(identityName, useKsk); |
| 609 | } |
| 610 | |
| 611 | Name |
| 612 | getDefaultCertificateNameForIdentity(const Name& identityName) const |
| 613 | { |
| 614 | return m_pib->getDefaultCertificateNameForIdentity(identityName); |
| 615 | } |
| 616 | |
| 617 | Name |
| 618 | getDefaultCertificateName() const |
| 619 | { |
| 620 | return m_pib->getDefaultCertificateName(); |
| 621 | } |
| 622 | |
| 623 | void |
| 624 | addCertificateAsKeyDefault(const IdentityCertificate& certificate) |
| 625 | { |
| 626 | return m_pib->addCertificateAsKeyDefault(certificate); |
| 627 | } |
| 628 | |
| 629 | void |
| 630 | addCertificateAsIdentityDefault(const IdentityCertificate& certificate) |
| 631 | { |
| 632 | return m_pib->addCertificateAsIdentityDefault(certificate); |
| 633 | } |
| 634 | |
| 635 | void |
| 636 | addCertificateAsSystemDefault(const IdentityCertificate& certificate) |
| 637 | { |
| 638 | return m_pib->addCertificateAsSystemDefault(certificate); |
| 639 | } |
| 640 | |
| 641 | shared_ptr<IdentityCertificate> |
| 642 | getDefaultCertificate() const |
| 643 | { |
Alexander Afanasyev | aab7966 | 2014-07-07 17:35:34 -0700 | [diff] [blame] | 644 | if (!static_cast<bool>(m_pib->getDefaultCertificate())) |
| 645 | const_cast<KeyChain*>(this)->setDefaultCertificateInternal(); |
| 646 | |
| 647 | return m_pib->getDefaultCertificate(); |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 648 | } |
| 649 | |
| 650 | void |
| 651 | refreshDefaultCertificate() |
| 652 | { |
| 653 | return m_pib->refreshDefaultCertificate(); |
| 654 | } |
| 655 | |
| 656 | /******************************* |
| 657 | * Wrapper of SecTpm * |
| 658 | *******************************/ |
| 659 | |
| 660 | void |
| 661 | setTpmPassword(const uint8_t* password, size_t passwordLength) |
| 662 | { |
| 663 | return m_tpm->setTpmPassword(password, passwordLength); |
| 664 | } |
| 665 | |
| 666 | void |
| 667 | resetTpmPassword() |
| 668 | { |
| 669 | return m_tpm->resetTpmPassword(); |
| 670 | } |
| 671 | |
| 672 | void |
| 673 | setInTerminal(bool inTerminal) |
| 674 | { |
| 675 | return m_tpm->setInTerminal(inTerminal); |
| 676 | } |
| 677 | |
| 678 | bool |
| 679 | getInTerminal() const |
| 680 | { |
| 681 | return m_tpm->getInTerminal(); |
| 682 | } |
| 683 | |
| 684 | bool |
| 685 | isLocked() const |
| 686 | { |
| 687 | return m_tpm->isLocked(); |
| 688 | } |
| 689 | |
| 690 | bool |
| 691 | unlockTpm(const char* password, size_t passwordLength, bool usePassword) |
| 692 | { |
| 693 | return m_tpm->unlockTpm(password, passwordLength, usePassword); |
| 694 | } |
| 695 | |
| 696 | void |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 697 | generateKeyPairInTpm(const Name& keyName, const KeyParams& params) |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 698 | { |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 699 | return m_tpm->generateKeyPairInTpm(keyName, params); |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 700 | } |
| 701 | |
| 702 | void |
| 703 | deleteKeyPairInTpm(const Name& keyName) |
| 704 | { |
| 705 | return m_tpm->deleteKeyPairInTpm(keyName); |
| 706 | } |
| 707 | |
| 708 | shared_ptr<PublicKey> |
| 709 | getPublicKeyFromTpm(const Name& keyName) const |
| 710 | { |
| 711 | return m_tpm->getPublicKeyFromTpm(keyName); |
| 712 | } |
| 713 | |
| 714 | Block |
| 715 | signInTpm(const uint8_t* data, size_t dataLength, |
| 716 | const Name& keyName, |
| 717 | DigestAlgorithm digestAlgorithm) |
| 718 | { |
| 719 | return m_tpm->signInTpm(data, dataLength, keyName, digestAlgorithm); |
| 720 | } |
| 721 | |
| 722 | ConstBufferPtr |
| 723 | decryptInTpm(const uint8_t* data, size_t dataLength, const Name& keyName, bool isSymmetric) |
| 724 | { |
| 725 | return m_tpm->decryptInTpm(data, dataLength, keyName, isSymmetric); |
| 726 | } |
| 727 | |
| 728 | ConstBufferPtr |
| 729 | encryptInTpm(const uint8_t* data, size_t dataLength, const Name& keyName, bool isSymmetric) |
| 730 | { |
| 731 | return m_tpm->encryptInTpm(data, dataLength, keyName, isSymmetric); |
| 732 | } |
| 733 | |
| 734 | void |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 735 | generateSymmetricKeyInTpm(const Name& keyName, const KeyParams& params) |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 736 | { |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 737 | return m_tpm->generateSymmetricKeyInTpm(keyName, params); |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 738 | } |
| 739 | |
| 740 | bool |
| 741 | doesKeyExistInTpm(const Name& keyName, KeyClass keyClass) const |
| 742 | { |
| 743 | return m_tpm->doesKeyExistInTpm(keyName, keyClass); |
| 744 | } |
| 745 | |
| 746 | bool |
| 747 | generateRandomBlock(uint8_t* res, size_t size) const |
| 748 | { |
| 749 | return m_tpm->generateRandomBlock(res, size); |
| 750 | } |
| 751 | |
| 752 | void |
| 753 | addAppToAcl(const Name& keyName, KeyClass keyClass, const std::string& appPath, AclType acl) |
| 754 | { |
| 755 | return m_tpm->addAppToAcl(keyName, keyClass, appPath, acl); |
| 756 | } |
| 757 | |
| 758 | ConstBufferPtr |
| 759 | exportPrivateKeyPkcs5FromTpm(const Name& keyName, const std::string& password) |
| 760 | { |
| 761 | return m_tpm->exportPrivateKeyPkcs5FromTpm(keyName, password); |
| 762 | } |
| 763 | |
| 764 | bool |
| 765 | importPrivateKeyPkcs5IntoTpm(const Name& keyName, |
| 766 | const uint8_t* buf, size_t size, |
| 767 | const std::string& password) |
| 768 | { |
| 769 | return m_tpm->importPrivateKeyPkcs5IntoTpm(keyName, buf, size, password); |
| 770 | } |
Jeff Thompson | 8efe5ad | 2013-08-20 17:36:38 -0700 | [diff] [blame] | 771 | |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 772 | private: |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 773 | void |
Alexander Afanasyev | 0711380 | 2015-01-15 19:14:36 -0800 | [diff] [blame] | 774 | initialize(const std::string& pibLocatorUri, |
| 775 | const std::string& tpmLocatorUri, |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 776 | bool needReset); |
| 777 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 778 | /** |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 779 | * @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] | 780 | * |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 781 | * @param params The signing parameters. |
| 782 | * @return The signing key name and prepared SignatureInfo. |
| 783 | * @throw Error when the requested signing method cannot be satisfied. |
Yingdi Yu | c8f883c | 2014-06-20 23:25:22 -0700 | [diff] [blame] | 784 | */ |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 785 | std::tuple<Name, SignatureInfo> |
| 786 | prepareSignatureInfo(const SigningInfo& params); |
| 787 | |
| 788 | /** |
| 789 | * @brief Internal abstraction of packet signing. |
| 790 | * |
| 791 | * @param packet The packet to sign |
| 792 | * @param params The signing parameters. |
| 793 | * @throw Error when the signing fails. |
| 794 | */ |
| 795 | template<typename T> |
| 796 | void |
| 797 | signImpl(T& packet, const SigningInfo& params); |
Yingdi Yu | c8f883c | 2014-06-20 23:25:22 -0700 | [diff] [blame] | 798 | |
| 799 | /** |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 800 | * @brief Set default certificate if it is not initialized |
| 801 | */ |
| 802 | void |
| 803 | setDefaultCertificateInternal(); |
| 804 | |
| 805 | /** |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 806 | * @brief Generate a key pair for the specified identity. |
| 807 | * |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 808 | * @param identityName The name of the specified identity. |
| 809 | * @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] | 810 | * @param params The parameter of the key. |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 811 | * @return The name of the generated key. |
| 812 | */ |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 813 | Name |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 814 | generateKeyPair(const Name& identityName, bool isKsk = false, |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 815 | const KeyParams& params = DEFAULT_KEY_PARAMS); |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 816 | |
Yingdi Yu | 8726f65 | 2014-01-23 10:35:12 -0800 | [diff] [blame] | 817 | /** |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 818 | * @brief Sign the data using a particular key. |
| 819 | * |
| 820 | * @param data Reference to the data packet. |
| 821 | * @param signature Signature to be added. |
Yingdi Yu | 8726f65 | 2014-01-23 10:35:12 -0800 | [diff] [blame] | 822 | * @param keyName The name of the signing key. |
| 823 | * @param digestAlgorithm the digest algorithm. |
| 824 | * @throws Tpm::Error |
Alexander Afanasyev | b78bc4d | 2014-04-09 21:20:52 -0700 | [diff] [blame] | 825 | */ |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 826 | void |
Yingdi Yu | c8f883c | 2014-06-20 23:25:22 -0700 | [diff] [blame] | 827 | signPacketWrapper(Data& data, const Signature& signature, |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 828 | const Name& keyName, DigestAlgorithm digestAlgorithm); |
Yingdi Yu | 8726f65 | 2014-01-23 10:35:12 -0800 | [diff] [blame] | 829 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 830 | /** |
| 831 | * @brief Sign the interest using a particular key. |
| 832 | * |
| 833 | * @param interest Reference to the interest packet. |
| 834 | * @param signature Signature to be added. |
| 835 | * @param keyName The name of the signing key. |
| 836 | * @param digestAlgorithm the digest algorithm. |
| 837 | * @throws Tpm::Error |
Alexander Afanasyev | b78bc4d | 2014-04-09 21:20:52 -0700 | [diff] [blame] | 838 | */ |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 839 | void |
Yingdi Yu | c8f883c | 2014-06-20 23:25:22 -0700 | [diff] [blame] | 840 | signPacketWrapper(Interest& interest, const Signature& signature, |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 841 | const Name& keyName, DigestAlgorithm digestAlgorithm); |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 842 | |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 843 | /** |
| 844 | * @brief Generate a SignatureValue block for a buffer @p buf with size @p size using |
| 845 | * a key with name @p keyName and digest algorithm @p digestAlgorithm. |
| 846 | */ |
| 847 | Block |
| 848 | pureSign(const uint8_t* buf, size_t size, const Name& keyName, DigestAlgorithm digestAlgorithm) const; |
| 849 | |
Alexander Afanasyev | 0711380 | 2015-01-15 19:14:36 -0800 | [diff] [blame] | 850 | static void |
Alexander Afanasyev | 34a3763 | 2015-01-16 17:37:36 -0800 | [diff] [blame] | 851 | registerPibImpl(const std::string& canonicalName, |
| 852 | std::initializer_list<std::string> aliases, PibCreateFunc createFunc); |
Alexander Afanasyev | 0711380 | 2015-01-15 19:14:36 -0800 | [diff] [blame] | 853 | |
| 854 | static void |
Alexander Afanasyev | 34a3763 | 2015-01-16 17:37:36 -0800 | [diff] [blame] | 855 | registerTpmImpl(const std::string& canonicalName, |
| 856 | std::initializer_list<std::string> aliases, TpmCreateFunc createFunc); |
Alexander Afanasyev | 0711380 | 2015-01-15 19:14:36 -0800 | [diff] [blame] | 857 | |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 858 | public: |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 859 | static tlv::SignatureTypeValue |
| 860 | getSignatureType(KeyType keyType, DigestAlgorithm digestAlgorithm); |
| 861 | |
| 862 | public: |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 863 | static const Name DEFAULT_PREFIX; |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 864 | static const SigningInfo DEFAULT_SIGNING_INFO; |
| 865 | |
| 866 | /** |
| 867 | * @brief A localhost identity which indicates that signature is generated using SHA-256. |
| 868 | * @todo Passing this as identity is not implemented. |
| 869 | */ |
| 870 | static const Name DIGEST_SHA256_IDENTITY; |
| 871 | |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 872 | // RsaKeyParams is set to be default for backward compatibility. |
| 873 | static const RsaKeyParams DEFAULT_KEY_PARAMS; |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 874 | |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 875 | typedef std::map<std::string, Block> SignParams; |
| 876 | |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 877 | private: |
Alexander Afanasyev | 0711380 | 2015-01-15 19:14:36 -0800 | [diff] [blame] | 878 | std::unique_ptr<SecPublicInfo> m_pib; |
| 879 | std::unique_ptr<SecTpm> m_tpm; |
Yingdi Yu | 0f5fb69 | 2014-06-10 12:07:28 -0700 | [diff] [blame] | 880 | time::milliseconds m_lastTimestamp; |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 881 | }; |
| 882 | |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 883 | template<typename T> |
| 884 | void |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 885 | KeyChain::signImpl(T& packet, const SigningInfo& params) |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 886 | { |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 887 | Name keyName; |
| 888 | SignatureInfo sigInfo; |
| 889 | std::tie(keyName, sigInfo) = prepareSignatureInfo(params); |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 890 | |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 891 | signPacketWrapper(packet, Signature(sigInfo), |
| 892 | keyName, params.getDigestAlgorithm()); |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 893 | } |
| 894 | |
| 895 | template<typename T> |
| 896 | void |
| 897 | KeyChain::sign(T& packet, const Name& certificateName) |
| 898 | { |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 899 | signImpl(packet, SigningInfo(SigningInfo::SIGNER_TYPE_CERT, certificateName)); |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 900 | } |
| 901 | |
| 902 | template<typename T> |
| 903 | void |
| 904 | KeyChain::signByIdentity(T& packet, const Name& identityName) |
| 905 | { |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 906 | signImpl(packet, SigningInfo(SigningInfo::SIGNER_TYPE_ID, identityName)); |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 907 | } |
| 908 | |
Alexander Afanasyev | 0711380 | 2015-01-15 19:14:36 -0800 | [diff] [blame] | 909 | template<class PibType> |
| 910 | inline void |
Alexander Afanasyev | 34a3763 | 2015-01-16 17:37:36 -0800 | [diff] [blame] | 911 | KeyChain::registerPib(std::initializer_list<std::string> aliases) |
Alexander Afanasyev | 0711380 | 2015-01-15 19:14:36 -0800 | [diff] [blame] | 912 | { |
Alexander Afanasyev | 34a3763 | 2015-01-16 17:37:36 -0800 | [diff] [blame] | 913 | registerPibImpl(*aliases.begin(), aliases, [] (const std::string& locator) { |
Alexander Afanasyev | 0711380 | 2015-01-15 19:14:36 -0800 | [diff] [blame] | 914 | return unique_ptr<SecPublicInfo>(new PibType(locator)); |
| 915 | }); |
| 916 | } |
| 917 | |
| 918 | template<class TpmType> |
| 919 | inline void |
Alexander Afanasyev | 34a3763 | 2015-01-16 17:37:36 -0800 | [diff] [blame] | 920 | KeyChain::registerTpm(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 | registerTpmImpl(*aliases.begin(), aliases, [] (const std::string& locator) { |
Alexander Afanasyev | 0711380 | 2015-01-15 19:14:36 -0800 | [diff] [blame] | 923 | return unique_ptr<SecTpm>(new TpmType(locator)); |
| 924 | }); |
| 925 | } |
| 926 | |
| 927 | /** |
| 928 | * \brief Register SecPib class in ndn-cxx KeyChain |
| 929 | * |
| 930 | * This macro should be placed once in the implementation file of the |
| 931 | * SecPib type within the namespace where the type is declared. |
| 932 | */ |
Alexander Afanasyev | 34a3763 | 2015-01-16 17:37:36 -0800 | [diff] [blame] | 933 | #define NDN_CXX_KEYCHAIN_REGISTER_PIB(PibType, ...) \ |
Alexander Afanasyev | 0711380 | 2015-01-15 19:14:36 -0800 | [diff] [blame] | 934 | static class NdnCxxAuto ## PibType ## PibRegistrationClass \ |
| 935 | { \ |
| 936 | public: \ |
| 937 | NdnCxxAuto ## PibType ## PibRegistrationClass() \ |
| 938 | { \ |
| 939 | ::ndn::KeyChain::registerPib<PibType>({__VA_ARGS__}); \ |
| 940 | } \ |
| 941 | } ndnCxxAuto ## PibType ## PibRegistrationVariable |
| 942 | |
| 943 | /** |
| 944 | * \brief Register SecTpm class in ndn-cxx KeyChain |
| 945 | * |
| 946 | * This macro should be placed once in the implementation file of the |
| 947 | * SecTpm type within the namespace where the type is declared. |
| 948 | */ |
Alexander Afanasyev | 34a3763 | 2015-01-16 17:37:36 -0800 | [diff] [blame] | 949 | #define NDN_CXX_KEYCHAIN_REGISTER_TPM(TpmType, ...) \ |
Alexander Afanasyev | 0711380 | 2015-01-15 19:14:36 -0800 | [diff] [blame] | 950 | static class NdnCxxAuto ## TpmType ## TpmRegistrationClass \ |
| 951 | { \ |
| 952 | public: \ |
| 953 | NdnCxxAuto ## TpmType ## TpmRegistrationClass() \ |
| 954 | { \ |
| 955 | ::ndn::KeyChain::registerTpm<TpmType>({__VA_ARGS__}); \ |
| 956 | } \ |
| 957 | } ndnCxxAuto ## TpmType ## TpmRegistrationVariable |
| 958 | |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 959 | } // namespace security |
| 960 | |
| 961 | using security::KeyChain; |
| 962 | |
Yingdi Yu | 6ab6781 | 2014-11-27 15:00:34 -0800 | [diff] [blame] | 963 | } // namespace ndn |
Jeff Thompson | 47c93cf | 2013-08-09 00:38:48 -0700 | [diff] [blame] | 964 | |
Alexander Afanasyev | 766cea7 | 2014-04-24 19:16:42 -0700 | [diff] [blame] | 965 | #endif // NDN_SECURITY_KEY_CHAIN_HPP |