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