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