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