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 | * |
Alexander Afanasyev | b78bc4d | 2014-04-09 21:20:52 -0700 | [diff] [blame] | 281 | * If the certificate to be deleted is current default system default, |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 282 | * the method will not delete the certificate and return immediately. |
Alexander Afanasyev | b78bc4d | 2014-04-09 21:20:52 -0700 | [diff] [blame] | 283 | * |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 284 | * @param certificateName The certificate to be deleted. |
| 285 | */ |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 286 | void |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 287 | deleteCertificate(const Name& certificateName); |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 288 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 289 | /** |
| 290 | * @brief delete a key. |
| 291 | * |
Alexander Afanasyev | b78bc4d | 2014-04-09 21:20:52 -0700 | [diff] [blame] | 292 | * If the key to be deleted is current default system default, |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 293 | * the method will not delete the key and return immediately. |
Alexander Afanasyev | b78bc4d | 2014-04-09 21:20:52 -0700 | [diff] [blame] | 294 | * |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 295 | * @param keyName The key to be deleted. |
| 296 | */ |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 297 | void |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 298 | deleteKey(const Name& keyName); |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 299 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 300 | /** |
| 301 | * @brief delete an identity. |
| 302 | * |
Alexander Afanasyev | b78bc4d | 2014-04-09 21:20:52 -0700 | [diff] [blame] | 303 | * If the identity to be deleted is current default system default, |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 304 | * the method will not delete the identity and return immediately. |
Alexander Afanasyev | b78bc4d | 2014-04-09 21:20:52 -0700 | [diff] [blame] | 305 | * |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 306 | * @param identity The identity to be deleted. |
| 307 | */ |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 308 | void |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 309 | deleteIdentity(const Name& identity); |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 310 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 311 | /** |
| 312 | * @brief export an identity. |
| 313 | * |
| 314 | * @param identity The identity to export. |
| 315 | * @param passwordStr The password to secure the private key. |
Yingdi Yu | 64c3fb4 | 2014-02-26 17:30:04 -0800 | [diff] [blame] | 316 | * @return The encoded export data. |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 317 | * @throws SecPublicInfo::Error if anything goes wrong in exporting. |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 318 | */ |
Yingdi Yu | 64c3fb4 | 2014-02-26 17:30:04 -0800 | [diff] [blame] | 319 | shared_ptr<SecuredBag> |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 320 | exportIdentity(const Name& identity, const std::string& passwordStr); |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 321 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 322 | /** |
| 323 | * @brief import an identity. |
| 324 | * |
Yingdi Yu | 64c3fb4 | 2014-02-26 17:30:04 -0800 | [diff] [blame] | 325 | * @param securedBag The encoded import data. |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 326 | * @param passwordStr The password to secure the private key. |
| 327 | */ |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 328 | void |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 329 | importIdentity(const SecuredBag& securedBag, const std::string& passwordStr); |
| 330 | |
| 331 | SecPublicInfo& |
| 332 | getPib() |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 333 | { |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 334 | return *m_pib; |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 335 | } |
| 336 | |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 337 | const SecPublicInfo& |
| 338 | getPib() const |
| 339 | { |
| 340 | return *m_pib; |
| 341 | } |
| 342 | |
| 343 | SecTpm& |
| 344 | getTpm() |
| 345 | { |
| 346 | return *m_tpm; |
| 347 | } |
| 348 | |
| 349 | const SecTpm& |
| 350 | getTpm() const |
| 351 | { |
| 352 | return *m_tpm; |
| 353 | } |
| 354 | |
| 355 | /******************************* |
| 356 | * Wrapper of SecPublicInfo * |
| 357 | *******************************/ |
| 358 | bool |
| 359 | doesIdentityExist(const Name& identityName) const |
| 360 | { |
| 361 | return m_pib->doesIdentityExist(identityName); |
| 362 | } |
| 363 | |
| 364 | void |
| 365 | addIdentity(const Name& identityName) |
| 366 | { |
| 367 | return m_pib->addIdentity(identityName); |
| 368 | } |
| 369 | |
| 370 | bool |
| 371 | doesPublicKeyExist(const Name& keyName) const |
| 372 | { |
| 373 | return m_pib->doesPublicKeyExist(keyName); |
| 374 | } |
| 375 | |
| 376 | void |
| 377 | addPublicKey(const Name& keyName, KeyType keyType, const PublicKey& publicKeyDer) |
| 378 | { |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame^] | 379 | return m_pib->addKey(keyName, publicKeyDer); |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 380 | } |
| 381 | |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 382 | void |
| 383 | addKey(const Name& keyName, const PublicKey& publicKeyDer) |
| 384 | { |
| 385 | return m_pib->addKey(keyName, publicKeyDer); |
| 386 | } |
| 387 | |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 388 | shared_ptr<PublicKey> |
| 389 | getPublicKey(const Name& keyName) const |
| 390 | { |
| 391 | return m_pib->getPublicKey(keyName); |
| 392 | } |
| 393 | |
| 394 | bool |
| 395 | doesCertificateExist(const Name& certificateName) const |
| 396 | { |
| 397 | return m_pib->doesCertificateExist(certificateName); |
| 398 | } |
| 399 | |
| 400 | void |
| 401 | addCertificate(const IdentityCertificate& certificate) |
| 402 | { |
| 403 | return m_pib->addCertificate(certificate); |
| 404 | } |
| 405 | |
| 406 | shared_ptr<IdentityCertificate> |
| 407 | getCertificate(const Name& certificateName) const |
| 408 | { |
| 409 | return m_pib->getCertificate(certificateName); |
| 410 | } |
| 411 | |
| 412 | Name |
| 413 | getDefaultIdentity() const |
| 414 | { |
| 415 | return m_pib->getDefaultIdentity(); |
| 416 | } |
| 417 | |
| 418 | Name |
| 419 | getDefaultKeyNameForIdentity(const Name& identityName) const |
| 420 | { |
| 421 | return m_pib->getDefaultKeyNameForIdentity(identityName); |
| 422 | } |
| 423 | |
| 424 | Name |
| 425 | getDefaultCertificateNameForKey(const Name& keyName) const |
| 426 | { |
| 427 | return m_pib->getDefaultCertificateNameForKey(keyName); |
| 428 | } |
| 429 | |
| 430 | void |
| 431 | getAllIdentities(std::vector<Name>& nameList, bool isDefault) const |
| 432 | { |
| 433 | return m_pib->getAllIdentities(nameList, isDefault); |
| 434 | } |
| 435 | |
| 436 | void |
| 437 | getAllKeyNames(std::vector<Name>& nameList, bool isDefault) const |
| 438 | { |
| 439 | return m_pib->getAllKeyNames(nameList, isDefault); |
| 440 | } |
| 441 | |
| 442 | void |
| 443 | getAllKeyNamesOfIdentity(const Name& identity, std::vector<Name>& nameList, bool isDefault) const |
| 444 | { |
| 445 | return m_pib->getAllKeyNamesOfIdentity(identity, nameList, isDefault); |
| 446 | } |
| 447 | |
| 448 | void |
| 449 | getAllCertificateNames(std::vector<Name>& nameList, bool isDefault) const |
| 450 | { |
| 451 | return m_pib->getAllCertificateNames(nameList, isDefault); |
| 452 | } |
| 453 | |
| 454 | void |
| 455 | getAllCertificateNamesOfKey(const Name& keyName, |
| 456 | std::vector<Name>& nameList, |
| 457 | bool isDefault) const |
| 458 | { |
| 459 | return m_pib->getAllCertificateNamesOfKey(keyName, nameList, isDefault); |
| 460 | } |
| 461 | |
| 462 | void |
| 463 | deleteCertificateInfo(const Name& certificateName) |
| 464 | { |
| 465 | return m_pib->deleteCertificateInfo(certificateName); |
| 466 | } |
| 467 | |
| 468 | void |
| 469 | deletePublicKeyInfo(const Name& keyName) |
| 470 | { |
| 471 | return m_pib->deletePublicKeyInfo(keyName); |
| 472 | } |
| 473 | |
| 474 | void |
| 475 | deleteIdentityInfo(const Name& identity) |
| 476 | { |
| 477 | return m_pib->deleteIdentityInfo(identity); |
| 478 | } |
| 479 | |
| 480 | void |
| 481 | setDefaultIdentity(const Name& identityName) |
| 482 | { |
| 483 | return m_pib->setDefaultIdentity(identityName); |
| 484 | } |
| 485 | |
| 486 | void |
| 487 | setDefaultKeyNameForIdentity(const Name& keyName) |
| 488 | { |
| 489 | return m_pib->setDefaultKeyNameForIdentity(keyName); |
| 490 | } |
| 491 | |
| 492 | void |
| 493 | setDefaultCertificateNameForKey(const Name& certificateName) |
| 494 | { |
| 495 | return m_pib->setDefaultCertificateNameForKey(certificateName); |
| 496 | } |
| 497 | |
| 498 | Name |
| 499 | getNewKeyName(const Name& identityName, bool useKsk) |
| 500 | { |
| 501 | return m_pib->getNewKeyName(identityName, useKsk); |
| 502 | } |
| 503 | |
| 504 | Name |
| 505 | getDefaultCertificateNameForIdentity(const Name& identityName) const |
| 506 | { |
| 507 | return m_pib->getDefaultCertificateNameForIdentity(identityName); |
| 508 | } |
| 509 | |
| 510 | Name |
| 511 | getDefaultCertificateName() const |
| 512 | { |
| 513 | return m_pib->getDefaultCertificateName(); |
| 514 | } |
| 515 | |
| 516 | void |
| 517 | addCertificateAsKeyDefault(const IdentityCertificate& certificate) |
| 518 | { |
| 519 | return m_pib->addCertificateAsKeyDefault(certificate); |
| 520 | } |
| 521 | |
| 522 | void |
| 523 | addCertificateAsIdentityDefault(const IdentityCertificate& certificate) |
| 524 | { |
| 525 | return m_pib->addCertificateAsIdentityDefault(certificate); |
| 526 | } |
| 527 | |
| 528 | void |
| 529 | addCertificateAsSystemDefault(const IdentityCertificate& certificate) |
| 530 | { |
| 531 | return m_pib->addCertificateAsSystemDefault(certificate); |
| 532 | } |
| 533 | |
| 534 | shared_ptr<IdentityCertificate> |
| 535 | getDefaultCertificate() const |
| 536 | { |
Alexander Afanasyev | aab7966 | 2014-07-07 17:35:34 -0700 | [diff] [blame] | 537 | if (!static_cast<bool>(m_pib->getDefaultCertificate())) |
| 538 | const_cast<KeyChain*>(this)->setDefaultCertificateInternal(); |
| 539 | |
| 540 | return m_pib->getDefaultCertificate(); |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 541 | } |
| 542 | |
| 543 | void |
| 544 | refreshDefaultCertificate() |
| 545 | { |
| 546 | return m_pib->refreshDefaultCertificate(); |
| 547 | } |
| 548 | |
| 549 | /******************************* |
| 550 | * Wrapper of SecTpm * |
| 551 | *******************************/ |
| 552 | |
| 553 | void |
| 554 | setTpmPassword(const uint8_t* password, size_t passwordLength) |
| 555 | { |
| 556 | return m_tpm->setTpmPassword(password, passwordLength); |
| 557 | } |
| 558 | |
| 559 | void |
| 560 | resetTpmPassword() |
| 561 | { |
| 562 | return m_tpm->resetTpmPassword(); |
| 563 | } |
| 564 | |
| 565 | void |
| 566 | setInTerminal(bool inTerminal) |
| 567 | { |
| 568 | return m_tpm->setInTerminal(inTerminal); |
| 569 | } |
| 570 | |
| 571 | bool |
| 572 | getInTerminal() const |
| 573 | { |
| 574 | return m_tpm->getInTerminal(); |
| 575 | } |
| 576 | |
| 577 | bool |
| 578 | isLocked() const |
| 579 | { |
| 580 | return m_tpm->isLocked(); |
| 581 | } |
| 582 | |
| 583 | bool |
| 584 | unlockTpm(const char* password, size_t passwordLength, bool usePassword) |
| 585 | { |
| 586 | return m_tpm->unlockTpm(password, passwordLength, usePassword); |
| 587 | } |
| 588 | |
| 589 | void |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 590 | generateKeyPairInTpm(const Name& keyName, const KeyParams& params) |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 591 | { |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 592 | return m_tpm->generateKeyPairInTpm(keyName, params); |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 593 | } |
| 594 | |
| 595 | void |
| 596 | deleteKeyPairInTpm(const Name& keyName) |
| 597 | { |
| 598 | return m_tpm->deleteKeyPairInTpm(keyName); |
| 599 | } |
| 600 | |
| 601 | shared_ptr<PublicKey> |
| 602 | getPublicKeyFromTpm(const Name& keyName) const |
| 603 | { |
| 604 | return m_tpm->getPublicKeyFromTpm(keyName); |
| 605 | } |
| 606 | |
| 607 | Block |
| 608 | signInTpm(const uint8_t* data, size_t dataLength, |
| 609 | const Name& keyName, |
| 610 | DigestAlgorithm digestAlgorithm) |
| 611 | { |
| 612 | return m_tpm->signInTpm(data, dataLength, keyName, digestAlgorithm); |
| 613 | } |
| 614 | |
| 615 | ConstBufferPtr |
| 616 | decryptInTpm(const uint8_t* data, size_t dataLength, const Name& keyName, bool isSymmetric) |
| 617 | { |
| 618 | return m_tpm->decryptInTpm(data, dataLength, keyName, isSymmetric); |
| 619 | } |
| 620 | |
| 621 | ConstBufferPtr |
| 622 | encryptInTpm(const uint8_t* data, size_t dataLength, const Name& keyName, bool isSymmetric) |
| 623 | { |
| 624 | return m_tpm->encryptInTpm(data, dataLength, keyName, isSymmetric); |
| 625 | } |
| 626 | |
| 627 | void |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 628 | generateSymmetricKeyInTpm(const Name& keyName, const KeyParams& params) |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 629 | { |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 630 | return m_tpm->generateSymmetricKeyInTpm(keyName, params); |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 631 | } |
| 632 | |
| 633 | bool |
| 634 | doesKeyExistInTpm(const Name& keyName, KeyClass keyClass) const |
| 635 | { |
| 636 | return m_tpm->doesKeyExistInTpm(keyName, keyClass); |
| 637 | } |
| 638 | |
| 639 | bool |
| 640 | generateRandomBlock(uint8_t* res, size_t size) const |
| 641 | { |
| 642 | return m_tpm->generateRandomBlock(res, size); |
| 643 | } |
| 644 | |
| 645 | void |
| 646 | addAppToAcl(const Name& keyName, KeyClass keyClass, const std::string& appPath, AclType acl) |
| 647 | { |
| 648 | return m_tpm->addAppToAcl(keyName, keyClass, appPath, acl); |
| 649 | } |
| 650 | |
| 651 | ConstBufferPtr |
| 652 | exportPrivateKeyPkcs5FromTpm(const Name& keyName, const std::string& password) |
| 653 | { |
| 654 | return m_tpm->exportPrivateKeyPkcs5FromTpm(keyName, password); |
| 655 | } |
| 656 | |
| 657 | bool |
| 658 | importPrivateKeyPkcs5IntoTpm(const Name& keyName, |
| 659 | const uint8_t* buf, size_t size, |
| 660 | const std::string& password) |
| 661 | { |
| 662 | return m_tpm->importPrivateKeyPkcs5IntoTpm(keyName, buf, size, password); |
| 663 | } |
Jeff Thompson | 8efe5ad | 2013-08-20 17:36:38 -0700 | [diff] [blame] | 664 | |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 665 | private: |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame^] | 666 | void |
| 667 | initialize(const std::string& pibLocator, |
| 668 | const std::string& tpmLocator, |
| 669 | bool needReset); |
| 670 | |
| 671 | void |
| 672 | initializeTpm(const std::string& locator); |
| 673 | |
| 674 | void |
| 675 | initializePib(const std::string& locator); |
| 676 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 677 | /** |
Yingdi Yu | c8f883c | 2014-06-20 23:25:22 -0700 | [diff] [blame] | 678 | * @brief Determine signature type |
| 679 | * |
| 680 | * An empty pointer will be returned if there is no valid signature. |
| 681 | */ |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 682 | shared_ptr<Signature> |
| 683 | determineSignatureWithPublicKey(const KeyLocator& keyLocator, |
| 684 | KeyType keyType, |
Yingdi Yu | c8f883c | 2014-06-20 23:25:22 -0700 | [diff] [blame] | 685 | DigestAlgorithm digestAlgorithm = DIGEST_ALGORITHM_SHA256); |
| 686 | |
| 687 | /** |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 688 | * @brief Set default certificate if it is not initialized |
| 689 | */ |
| 690 | void |
| 691 | setDefaultCertificateInternal(); |
| 692 | |
| 693 | /** |
| 694 | * @brief Sign a packet using a pariticular certificate. |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 695 | * |
| 696 | * @param packet The packet to be signed. |
| 697 | * @param certificate The signing certificate. |
| 698 | */ |
| 699 | template<typename T> |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 700 | void |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 701 | sign(T& packet, const IdentityCertificate& certificate); |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 702 | |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 703 | /** |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 704 | * @brief Generate a key pair for the specified identity. |
| 705 | * |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 706 | * @param identityName The name of the specified identity. |
| 707 | * @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] | 708 | * @param params The parameter of the key. |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 709 | * @return The name of the generated key. |
| 710 | */ |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 711 | Name |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 712 | generateKeyPair(const Name& identityName, bool isKsk = false, |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 713 | const KeyParams& params = DEFAULT_KEY_PARAMS); |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 714 | |
Yingdi Yu | 8726f65 | 2014-01-23 10:35:12 -0800 | [diff] [blame] | 715 | /** |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 716 | * @brief Sign the data using a particular key. |
| 717 | * |
| 718 | * @param data Reference to the data packet. |
| 719 | * @param signature Signature to be added. |
Yingdi Yu | 8726f65 | 2014-01-23 10:35:12 -0800 | [diff] [blame] | 720 | * @param keyName The name of the signing key. |
| 721 | * @param digestAlgorithm the digest algorithm. |
| 722 | * @throws Tpm::Error |
Alexander Afanasyev | b78bc4d | 2014-04-09 21:20:52 -0700 | [diff] [blame] | 723 | */ |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 724 | void |
Yingdi Yu | c8f883c | 2014-06-20 23:25:22 -0700 | [diff] [blame] | 725 | signPacketWrapper(Data& data, const Signature& signature, |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 726 | const Name& keyName, DigestAlgorithm digestAlgorithm); |
Yingdi Yu | 8726f65 | 2014-01-23 10:35:12 -0800 | [diff] [blame] | 727 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 728 | /** |
| 729 | * @brief Sign the interest using a particular key. |
| 730 | * |
| 731 | * @param interest Reference to the interest packet. |
| 732 | * @param signature Signature to be added. |
| 733 | * @param keyName The name of the signing key. |
| 734 | * @param digestAlgorithm the digest algorithm. |
| 735 | * @throws Tpm::Error |
Alexander Afanasyev | b78bc4d | 2014-04-09 21:20:52 -0700 | [diff] [blame] | 736 | */ |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 737 | void |
Yingdi Yu | c8f883c | 2014-06-20 23:25:22 -0700 | [diff] [blame] | 738 | signPacketWrapper(Interest& interest, const Signature& signature, |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 739 | const Name& keyName, DigestAlgorithm digestAlgorithm); |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 740 | |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame^] | 741 | public: |
| 742 | static const Name DEFAULT_PREFIX; |
| 743 | // RsaKeyParams is set to be default for backward compatibility. |
| 744 | static const RsaKeyParams DEFAULT_KEY_PARAMS; |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 745 | |
| 746 | private: |
| 747 | SecPublicInfo* m_pib; |
| 748 | SecTpm* m_tpm; |
Yingdi Yu | 0f5fb69 | 2014-06-10 12:07:28 -0700 | [diff] [blame] | 749 | time::milliseconds m_lastTimestamp; |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 750 | }; |
| 751 | |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 752 | template<typename T> |
| 753 | void |
| 754 | KeyChain::sign(T& packet) |
| 755 | { |
Alexander Afanasyev | aab7966 | 2014-07-07 17:35:34 -0700 | [diff] [blame] | 756 | if (!static_cast<bool>(m_pib->getDefaultCertificate())) |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 757 | setDefaultCertificateInternal(); |
| 758 | |
Alexander Afanasyev | aab7966 | 2014-07-07 17:35:34 -0700 | [diff] [blame] | 759 | sign(packet, *m_pib->getDefaultCertificate()); |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 760 | } |
| 761 | |
| 762 | template<typename T> |
| 763 | void |
| 764 | KeyChain::sign(T& packet, const Name& certificateName) |
| 765 | { |
Yingdi Yu | c8f883c | 2014-06-20 23:25:22 -0700 | [diff] [blame] | 766 | shared_ptr<IdentityCertificate> certificate = m_pib->getCertificate(certificateName); |
| 767 | sign(packet, *certificate); |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 768 | } |
| 769 | |
| 770 | template<typename T> |
| 771 | void |
| 772 | KeyChain::signByIdentity(T& packet, const Name& identityName) |
| 773 | { |
| 774 | Name signingCertificateName; |
| 775 | try |
| 776 | { |
| 777 | signingCertificateName = m_pib->getDefaultCertificateNameForIdentity(identityName); |
| 778 | } |
| 779 | catch (SecPublicInfo::Error& e) |
| 780 | { |
| 781 | signingCertificateName = createIdentity(identityName); |
| 782 | // Ideally, no exception will be thrown out, unless something goes wrong in the TPM, which |
| 783 | // is a fatal error. |
| 784 | } |
| 785 | |
| 786 | // We either get or create the signing certificate, sign packet! (no exception unless fatal |
| 787 | // error in TPM) |
| 788 | sign(packet, signingCertificateName); |
| 789 | } |
| 790 | |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 791 | template<typename T> |
| 792 | void |
| 793 | KeyChain::sign(T& packet, const IdentityCertificate& certificate) |
| 794 | { |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 795 | KeyLocator keyLocator(certificate.getName().getPrefix(-1)); |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 796 | |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 797 | shared_ptr<Signature> signature = |
| 798 | determineSignatureWithPublicKey(keyLocator, certificate.getPublicKeyInfo().getKeyType()); |
Yingdi Yu | c8f883c | 2014-06-20 23:25:22 -0700 | [diff] [blame] | 799 | |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 800 | if (!static_cast<bool>(signature)) |
| 801 | throw SecPublicInfo::Error("unknown key type!"); |
| 802 | |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 803 | signPacketWrapper(packet, *signature, |
| 804 | certificate.getPublicKeyName(), |
| 805 | DIGEST_ALGORITHM_SHA256); |
| 806 | |
| 807 | return; |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 808 | } |
| 809 | |
Yingdi Yu | 6ab6781 | 2014-11-27 15:00:34 -0800 | [diff] [blame] | 810 | } // namespace ndn |
Jeff Thompson | 47c93cf | 2013-08-09 00:38:48 -0700 | [diff] [blame] | 811 | |
Alexander Afanasyev | 766cea7 | 2014-04-24 19:16:42 -0700 | [diff] [blame] | 812 | #endif // NDN_SECURITY_KEY_CHAIN_HPP |