Jeff Thompson | 25b4e61 | 2013-10-10 16:03:24 -0700 | [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 | /** |
Jeff Thompson | 7687dc0 | 2013-09-13 11:54:07 -0700 | [diff] [blame] | 3 | * Copyright (C) 2013 Regents of the University of California. |
Jeff Thompson | ba16b8f | 2013-12-16 13:11:47 -0800 | [diff] [blame] | 4 | * @author: Yingdi Yu <yingdi@cs.ucla.edu> |
Jeff Thompson | 7687dc0 | 2013-09-13 11:54:07 -0700 | [diff] [blame] | 5 | * @author: Jeff Thompson <jefft0@remap.ucla.edu> |
Jeff Thompson | 47c93cf | 2013-08-09 00:38:48 -0700 | [diff] [blame] | 6 | * See COPYING for copyright and distribution information. |
| 7 | */ |
| 8 | |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 9 | #ifndef NDN_SECURITY_KEY_CHAIN_HPP |
| 10 | #define NDN_SECURITY_KEY_CHAIN_HPP |
Jeff Thompson | 47c93cf | 2013-08-09 00:38:48 -0700 | [diff] [blame] | 11 | |
Yingdi Yu | 4f32463 | 2014-01-15 18:10:03 -0800 | [diff] [blame] | 12 | #include "identity-certificate.hpp" |
| 13 | #include "public-key.hpp" |
| 14 | #include "signature-sha256-with-rsa.hpp" |
Yingdi Yu | 4270f20 | 2014-01-28 14:19:16 -0800 | [diff] [blame] | 15 | #include "../interest.hpp" |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 16 | #include "../encoding/tlv-security.hpp" |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 17 | #include "../util/random.hpp" |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 18 | |
Yingdi Yu | 0402092 | 2014-01-22 12:46:53 -0800 | [diff] [blame] | 19 | //PublicInfo |
Yingdi Yu | 4f32463 | 2014-01-15 18:10:03 -0800 | [diff] [blame] | 20 | #include "sec-public-info-sqlite3.hpp" |
| 21 | #include "sec-public-info-memory.hpp" |
Yingdi Yu | 0402092 | 2014-01-22 12:46:53 -0800 | [diff] [blame] | 22 | //TPM |
| 23 | #include "sec-tpm-file.hpp" |
Yingdi Yu | 4f32463 | 2014-01-15 18:10:03 -0800 | [diff] [blame] | 24 | #include "sec-tpm-memory.hpp" |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 25 | |
Yingdi Yu | 0402092 | 2014-01-22 12:46:53 -0800 | [diff] [blame] | 26 | #ifdef NDN_CPP_HAVE_OSX_SECURITY |
| 27 | #include "sec-tpm-osx.hpp" |
| 28 | #endif |
| 29 | |
Jeff Thompson | 47c93cf | 2013-08-09 00:38:48 -0700 | [diff] [blame] | 30 | |
| 31 | namespace ndn { |
| 32 | |
Jeff Thompson | 2ce8f49 | 2013-09-17 18:01:25 -0700 | [diff] [blame] | 33 | /** |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 34 | * @brief KeyChain is one of the main classes of the security library. |
Jeff Thompson | ffa36f9 | 2013-09-20 08:42:41 -0700 | [diff] [blame] | 35 | * |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 36 | * The KeyChain class provides a set of interfaces of identity management and private key related operations. |
Jeff Thompson | ffa36f9 | 2013-09-20 08:42:41 -0700 | [diff] [blame] | 37 | */ |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 38 | template<class Info, class Tpm> |
| 39 | class KeyChainImpl : public Info, public Tpm |
| 40 | { |
Yingdi Yu | f8fc8de | 2014-02-25 15:45:39 -0800 | [diff] [blame] | 41 | typedef SecPublicInfo::Error InfoError; |
| 42 | typedef SecTpm::Error TpmError; |
Jeff Thompson | 47c93cf | 2013-08-09 00:38:48 -0700 | [diff] [blame] | 43 | public: |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 44 | /** |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 45 | * @brief Create an identity by creating a pair of Key-Signing-Key (KSK) for this identity and a self-signed certificate of the KSK. |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 46 | * |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 47 | * @param identityName The name of the identity. |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 48 | * @return The name of the default certificate of the identity. |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 49 | */ |
| 50 | Name |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 51 | createIdentity(const Name& identityName) |
| 52 | { |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 53 | Info::addIdentity(identityName); |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 54 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 55 | Name keyName; |
| 56 | try |
| 57 | { |
| 58 | keyName = Info::getDefaultKeyNameForIdentity(identityName); |
| 59 | } |
| 60 | catch(InfoError& e) |
| 61 | { |
| 62 | keyName = generateRSAKeyPairAsDefault(identityName, true); |
| 63 | } |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 64 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 65 | Name certName; |
| 66 | try |
| 67 | { |
| 68 | certName = Info::getDefaultCertificateNameForKey(keyName); |
| 69 | } |
| 70 | catch(InfoError& e) |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 71 | { |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 72 | shared_ptr<IdentityCertificate> selfCert = selfSign(keyName); |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 73 | Info::addCertificateAsIdentityDefault(*selfCert); |
| 74 | certName = selfCert->getName(); |
| 75 | } |
| 76 | |
| 77 | return certName; |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 78 | } |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 79 | |
| 80 | /** |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 81 | * @brief Generate a pair of RSA keys for the specified identity. |
| 82 | * |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 83 | * @param identityName The name of the identity. |
| 84 | * @param isKsk true for generating a Key-Signing-Key (KSK), false for a Data-Signing-Key (KSK). |
| 85 | * @param keySize The size of the key. |
| 86 | * @return The generated key name. |
| 87 | */ |
| 88 | Name |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 89 | generateRSAKeyPair(const Name& identityName, bool isKsk = false, int keySize = 2048) |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 90 | { |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 91 | return generateKeyPair(identityName, isKsk, KEY_TYPE_RSA, keySize); |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 92 | } |
Alexander Afanasyev | 64a3d81 | 2014-01-05 23:35:05 -0800 | [diff] [blame] | 93 | |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 94 | /** |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 95 | * @brief Generate a pair of RSA keys for the specified identity and set it as default key for the identity. |
| 96 | * |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 97 | * @param identityName The name of the identity. |
| 98 | * @param isKsk true for generating a Key-Signing-Key (KSK), false for a Data-Signing-Key (KSK). |
| 99 | * @param keySize The size of the key. |
| 100 | * @return The generated key name. |
| 101 | */ |
| 102 | Name |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 103 | generateRSAKeyPairAsDefault(const Name& identityName, bool isKsk = false, int keySize = 2048) |
| 104 | { |
| 105 | Name keyName = generateKeyPair(identityName, isKsk, KEY_TYPE_RSA, keySize); |
| 106 | |
| 107 | Info::setDefaultKeyNameForIdentity(keyName); |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 108 | |
| 109 | return keyName; |
| 110 | } |
Jeff Thompson | 79a2d5d | 2013-09-27 14:32:23 -0700 | [diff] [blame] | 111 | |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 112 | /** |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 113 | * @brief Sign packet with default identity |
| 114 | * |
| 115 | * on return signatureInfo and signatureValue in the packet are set. |
| 116 | * |
| 117 | * @param packet The packet to be signed |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 118 | */ |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 119 | template<typename T> |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 120 | void |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 121 | sign(T& packet) |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 122 | { |
| 123 | if (!Info::defaultCertificate()) |
| 124 | { |
| 125 | Info::refreshDefaultCertificate(); |
| 126 | |
| 127 | if(!Info::defaultCertificate()) |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 128 | { |
| 129 | Name defaultIdentity; |
| 130 | try |
| 131 | { |
| 132 | defaultIdentity = Info::getDefaultIdentity(); |
| 133 | } |
| 134 | catch(InfoError& e) |
| 135 | { |
| 136 | uint32_t random = random::generateWord32(); |
| 137 | defaultIdentity.append("tmp-identity").append(reinterpret_cast<uint8_t*>(&random), 4); |
| 138 | } |
| 139 | createIdentity(defaultIdentity); |
| 140 | Info::refreshDefaultCertificate(); |
| 141 | } |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 142 | } |
| 143 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 144 | sign(packet, *Info::defaultCertificate()); |
Yingdi Yu | 4270f20 | 2014-01-28 14:19:16 -0800 | [diff] [blame] | 145 | } |
Alexander Afanasyev | e64788e | 2014-01-05 22:38:21 -0800 | [diff] [blame] | 146 | |
Jeff Thompson | 47c93cf | 2013-08-09 00:38:48 -0700 | [diff] [blame] | 147 | /** |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 148 | * @brief Sign packet with a particular certificate. |
| 149 | * |
| 150 | * @param packet The packet to be signed. |
| 151 | * @param certificateName The certificate name of the key to use for signing. |
| 152 | * @throws SecPublicInfo::Error if certificate does not exist. |
Jeff Thompson | 3c73da4 | 2013-08-12 11:19:05 -0700 | [diff] [blame] | 153 | */ |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 154 | template<typename T> |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 155 | void |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 156 | sign(T& packet, const Name& certificateName) |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 157 | { |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 158 | if (!Info::doesCertificateExist(certificateName)) |
Yingdi Yu | 4270f20 | 2014-01-28 14:19:16 -0800 | [diff] [blame] | 159 | throw InfoError("Requested certificate [" + certificateName.toUri() + "] doesn't exist"); |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 160 | |
| 161 | SignatureSha256WithRsa signature; |
| 162 | signature.setKeyLocator(certificateName.getPrefix(-1)); // implicit conversion should take care |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 163 | |
| 164 | // For temporary usage, we support RSA + SHA256 only, but will support more. |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 165 | signPacketWrapper(packet, signature, |
| 166 | IdentityCertificate::certificateNameToPublicKeyName(certificateName), |
| 167 | DIGEST_ALGORITHM_SHA256); |
Yingdi Yu | 4270f20 | 2014-01-28 14:19:16 -0800 | [diff] [blame] | 168 | } |
Jeff Thompson | 79a2d5d | 2013-09-27 14:32:23 -0700 | [diff] [blame] | 169 | |
Jeff Thompson | 29ce310 | 2013-09-27 11:47:48 -0700 | [diff] [blame] | 170 | /** |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 171 | * @brief Sign the byte array using a particular certificate. |
| 172 | * |
Jeff Thompson | c01e178 | 2013-10-21 14:08:42 -0700 | [diff] [blame] | 173 | * @param buffer The byte array to be signed. |
| 174 | * @param bufferLength the length of buffer. |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 175 | * @param certificateName The certificate name of the signing key. |
Jeff Thompson | c01e178 | 2013-10-21 14:08:42 -0700 | [diff] [blame] | 176 | * @return The Signature. |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 177 | * @throws SecPublicInfo::Error if certificate does not exist. |
Jeff Thompson | c01e178 | 2013-10-21 14:08:42 -0700 | [diff] [blame] | 178 | */ |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 179 | Signature |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 180 | sign(const uint8_t* buffer, size_t bufferLength, const Name& certificateName) |
| 181 | { |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 182 | if (!Info::doesCertificateExist(certificateName)) |
Yingdi Yu | 4270f20 | 2014-01-28 14:19:16 -0800 | [diff] [blame] | 183 | throw InfoError("Requested certificate [" + certificateName.toUri() + "] doesn't exist"); |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 184 | |
| 185 | SignatureSha256WithRsa signature; |
| 186 | signature.setKeyLocator(certificateName.getPrefix(-1)); // implicit conversion should take care |
| 187 | |
| 188 | // For temporary usage, we support RSA + SHA256 only, but will support more. |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 189 | signature.setValue(Tpm::signInTpm(buffer, bufferLength, |
| 190 | IdentityCertificate::certificateNameToPublicKeyName(certificateName), |
| 191 | DIGEST_ALGORITHM_SHA256)); |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 192 | return signature; |
| 193 | } |
Jeff Thompson | c01e178 | 2013-10-21 14:08:42 -0700 | [diff] [blame] | 194 | |
| 195 | /** |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 196 | * @brief Sign packet using the default certificate of a particular identity. |
| 197 | * |
| 198 | * If there is no default certificate of that identity, this method will create a self-signed certificate. |
| 199 | * |
| 200 | * @param packet The packet to be signed. |
| 201 | * @param identityName The signing identity name. |
Jeff Thompson | 29ce310 | 2013-09-27 11:47:48 -0700 | [diff] [blame] | 202 | */ |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 203 | template<typename T> |
Jeff Thompson | 29ce310 | 2013-09-27 11:47:48 -0700 | [diff] [blame] | 204 | void |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 205 | signByIdentity(T& packet, const Name& identityName) |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 206 | { |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 207 | Name signingCertificateName; |
| 208 | try |
| 209 | { |
| 210 | signingCertificateName = Info::getDefaultCertificateNameForIdentity(identityName); |
| 211 | } |
| 212 | catch(InfoError& e) |
| 213 | { |
| 214 | signingCertificateName = createIdentity(identityName); |
| 215 | // Ideally, no exception will be thrown out, unless something goes wrong in the TPM, which is a fatal error. |
| 216 | } |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 217 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 218 | // We either get or create the signing certificate, sign packet! (no exception unless fatal error in TPM) |
| 219 | sign(packet, signingCertificateName); |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 220 | } |
Jeff Thompson | 3c73da4 | 2013-08-12 11:19:05 -0700 | [diff] [blame] | 221 | |
| 222 | /** |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 223 | * @brief Sign the byte array using the default certificate of a particular identity. |
| 224 | * |
Jeff Thompson | c01e178 | 2013-10-21 14:08:42 -0700 | [diff] [blame] | 225 | * @param buffer The byte array to be signed. |
| 226 | * @param bufferLength the length of buffer. |
| 227 | * @param identityName The identity name. |
| 228 | * @return The Signature. |
| 229 | */ |
Alexander Afanasyev | 64a3d81 | 2014-01-05 23:35:05 -0800 | [diff] [blame] | 230 | Signature |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 231 | signByIdentity(const uint8_t* buffer, size_t bufferLength, const Name& identityName) |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 232 | { |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 233 | Name signingCertificateName; |
| 234 | try |
| 235 | { |
| 236 | signingCertificateName = Info::getDefaultCertificateNameForIdentity(identityName); |
| 237 | } |
| 238 | catch(InfoError& e) |
| 239 | { |
| 240 | signingCertificateName = createIdentity(identityName); |
| 241 | // Ideally, no exception will be thrown out, unless something goes wrong in the TPM, which is a fatal error. |
| 242 | } |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 243 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 244 | // We either get or create the signing certificate, sign data! (no exception unless fatal error in TPM) |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 245 | return sign(buffer, bufferLength, signingCertificateName); |
| 246 | } |
Jeff Thompson | c01e178 | 2013-10-21 14:08:42 -0700 | [diff] [blame] | 247 | |
| 248 | /** |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 249 | * @brief Generate a self-signed certificate for a public key. |
| 250 | * |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 251 | * @param keyName The name of the public key. |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 252 | * @return The generated certificate, NULL if selfSign fails. |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 253 | */ |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 254 | shared_ptr<IdentityCertificate> |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 255 | selfSign(const Name& keyName) |
| 256 | { |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 257 | shared_ptr<PublicKey> pubKey; |
| 258 | try |
| 259 | { |
| 260 | pubKey = Info::getPublicKey(keyName); // may throw an exception. |
| 261 | } |
| 262 | catch(InfoError& e) |
| 263 | { |
| 264 | return shared_ptr<IdentityCertificate>(); |
| 265 | } |
Yingdi Yu | 7ea6950 | 2014-01-15 17:21:29 -0800 | [diff] [blame] | 266 | |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 267 | shared_ptr<IdentityCertificate> certificate = make_shared<IdentityCertificate>(); |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 268 | |
| 269 | Name certificateName = keyName.getPrefix(-1); |
| 270 | certificateName.append("KEY").append(keyName.get(-1)).append("ID-CERT").appendVersion(); |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 271 | |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 272 | certificate->setName(certificateName); |
| 273 | certificate->setNotBefore(getNow()); |
| 274 | certificate->setNotAfter(getNow() + 630720000 /* 20 years*/); |
| 275 | certificate->setPublicKeyInfo(*pubKey); |
| 276 | certificate->addSubjectDescription(CertificateSubjectDescription("2.5.4.41", keyName.toUri())); |
| 277 | certificate->encode(); |
| 278 | |
| 279 | selfSign(*certificate); |
| 280 | return certificate; |
| 281 | } |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 282 | |
| 283 | /** |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 284 | * @brief Self-sign the supplied identity certificate. |
| 285 | * |
| 286 | * @param cert The supplied cert. |
| 287 | * @throws SecTpm::Error if the private key does not exist. |
Jeff Thompson | 8efe5ad | 2013-08-20 17:36:38 -0700 | [diff] [blame] | 288 | */ |
Jeff Thompson | 2ce8f49 | 2013-09-17 18:01:25 -0700 | [diff] [blame] | 289 | void |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 290 | selfSign (IdentityCertificate& cert) |
| 291 | { |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 292 | Name keyName = IdentityCertificate::certificateNameToPublicKeyName(cert.getName()); |
| 293 | if(!Tpm::doesKeyExistInTpm(keyName, KEY_CLASS_PRIVATE)) |
| 294 | throw TpmError("private key does not exist!"); |
| 295 | |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 296 | SignatureSha256WithRsa signature; |
| 297 | signature.setKeyLocator(cert.getName().getPrefix(-1)); // implicit conversion should take care |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 298 | |
| 299 | // For temporary usage, we support RSA + SHA256 only, but will support more. |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 300 | signPacketWrapper(cert, signature, keyName, DIGEST_ALGORITHM_SHA256); |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 301 | } |
| 302 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 303 | /** |
| 304 | * @brief delete a certificate. |
| 305 | * |
| 306 | * If the certificate to be deleted is current default system default, |
| 307 | * the method will not delete the certificate and return immediately. |
| 308 | * |
| 309 | * @param certificateName The certificate to be deleted. |
| 310 | */ |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 311 | void |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 312 | deleteCertificate (const Name& certificateName) |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 313 | { |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 314 | try |
| 315 | { |
| 316 | if(Info::getDefaultCertificateName() == certificateName) |
| 317 | return; |
| 318 | } |
| 319 | catch(InfoError& e) |
| 320 | { |
| 321 | // Not a real error, just try to delete the certificate |
| 322 | } |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 323 | |
| 324 | Info::deleteCertificateInfo(certificateName); |
| 325 | } |
| 326 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 327 | /** |
| 328 | * @brief delete a key. |
| 329 | * |
| 330 | * If the key to be deleted is current default system default, |
| 331 | * the method will not delete the key and return immediately. |
| 332 | * |
| 333 | * @param keyName The key to be deleted. |
| 334 | */ |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 335 | void |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 336 | deleteKey (const Name& keyName) |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 337 | { |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 338 | try |
| 339 | { |
| 340 | if(Info::getDefaultKeyNameForIdentity(Info::getDefaultIdentity()) == keyName) |
| 341 | return; |
| 342 | } |
| 343 | catch(InfoError& e) |
| 344 | { |
| 345 | // Not a real error, just try to delete the key |
| 346 | } |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 347 | |
| 348 | Info::deletePublicKeyInfo(keyName); |
| 349 | Tpm::deleteKeyPairInTpm(keyName); |
| 350 | } |
| 351 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 352 | /** |
| 353 | * @brief delete an identity. |
| 354 | * |
| 355 | * If the identity to be deleted is current default system default, |
| 356 | * the method will not delete the identity and return immediately. |
| 357 | * |
| 358 | * @param identity The identity to be deleted. |
| 359 | */ |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 360 | void |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 361 | deleteIdentity (const Name& identity) |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 362 | { |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 363 | try |
| 364 | { |
| 365 | if(Info::getDefaultIdentity() == identity) |
| 366 | return; |
| 367 | } |
| 368 | catch(InfoError& e) |
| 369 | { |
| 370 | // Not a real error, just try to delete the identity |
| 371 | } |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 372 | |
| 373 | std::vector<Name> nameList; |
| 374 | Info::getAllKeyNamesOfIdentity(identity, nameList, true); |
| 375 | Info::getAllKeyNamesOfIdentity(identity, nameList, false); |
| 376 | |
| 377 | Info::deleteIdentityInfo(identity); |
| 378 | |
| 379 | std::vector<Name>::const_iterator it = nameList.begin(); |
| 380 | for(; it != nameList.end(); it++) |
| 381 | Tpm::deleteKeyPairInTpm(*it); |
| 382 | } |
| 383 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 384 | /** |
| 385 | * @brief export an identity. |
| 386 | * |
| 387 | * @param identity The identity to export. |
| 388 | * @param passwordStr The password to secure the private key. |
| 389 | * @param The encoded export data. |
| 390 | * @throws InfoError if anything goes wrong in exporting. |
| 391 | */ |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 392 | Block |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 393 | exportIdentity(const Name& identity, const std::string& passwordStr) |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 394 | { |
| 395 | if (!Info::doesIdentityExist(identity)) |
| 396 | throw InfoError("Identity does not exist!"); |
| 397 | |
| 398 | Name keyName = Info::getDefaultKeyNameForIdentity(identity); |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 399 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 400 | ConstBufferPtr pkcs8; |
| 401 | try |
| 402 | { |
| 403 | pkcs8 = Tpm::exportPrivateKeyPkcs8FromTpm(keyName, passwordStr); |
| 404 | } |
| 405 | catch(TpmError& e) |
| 406 | { |
| 407 | throw InfoError("Fail to export PKCS8 of private key"); |
| 408 | } |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 409 | Block wireKey(tlv::security::KeyPackage, pkcs8); |
| 410 | |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 411 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 412 | shared_ptr<IdentityCertificate> cert; |
| 413 | try |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 414 | { |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 415 | cert = Info::getCertificate(Info::getDefaultCertificateNameForKey(keyName)); |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 416 | } |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 417 | catch(InfoError& e) |
| 418 | { |
| 419 | cert = selfSign(keyName); |
| 420 | Info::addCertificateAsIdentityDefault(*cert); |
| 421 | } |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 422 | Block wireCert(tlv::security::CertificatePackage, cert->wireEncode()); |
| 423 | |
| 424 | Block wire(tlv::security::IdentityPackage); |
| 425 | wire.push_back(wireCert); |
| 426 | wire.push_back(wireKey); |
| 427 | |
| 428 | return wire; |
| 429 | } |
| 430 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 431 | /** |
| 432 | * @brief import an identity. |
| 433 | * |
| 434 | * @param The encoded import data. |
| 435 | * @param passwordStr The password to secure the private key. |
| 436 | */ |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 437 | void |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 438 | importIdentity(const Block& block, const std::string& passwordStr) |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 439 | { |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 440 | try |
| 441 | { |
| 442 | block.parse(); |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 443 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 444 | Data data; |
| 445 | data.wireDecode(block.get(tlv::security::CertificatePackage).blockFromValue()); |
| 446 | shared_ptr<IdentityCertificate> cert = make_shared<IdentityCertificate>(data); |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 447 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 448 | Name keyName = IdentityCertificate::certificateNameToPublicKeyName(cert->getName()); |
| 449 | Name identity = keyName.getPrefix(-1); |
| 450 | |
| 451 | // Add identity |
| 452 | Info::addIdentity(identity); |
| 453 | |
| 454 | // Add key |
| 455 | Block wireKey = block.get(tlv::security::KeyPackage); |
| 456 | Tpm::importPrivateKeyPkcs8IntoTpm(keyName, wireKey.value(), wireKey.value_size(), passwordStr); |
| 457 | shared_ptr<PublicKey> pubKey = Tpm::getPublicKeyFromTpm(keyName.toUri()); |
| 458 | Info::addPublicKey(keyName, KEY_TYPE_RSA, *pubKey); // HACK! We should set key type according to the pkcs8 info. |
| 459 | Info::setDefaultKeyNameForIdentity(keyName); |
| 460 | |
| 461 | // Add cert |
| 462 | Info::addCertificateAsIdentityDefault(*cert); |
| 463 | } |
| 464 | catch(Block::Error& e) |
| 465 | { |
| 466 | return; |
| 467 | } |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 468 | } |
| 469 | |
Jeff Thompson | 8efe5ad | 2013-08-20 17:36:38 -0700 | [diff] [blame] | 470 | |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 471 | private: |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 472 | /** |
| 473 | * @brief sign a packet using a pariticular certificate. |
| 474 | * |
| 475 | * @param packet The packet to be signed. |
| 476 | * @param certificate The signing certificate. |
| 477 | */ |
| 478 | template<typename T> |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 479 | void |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 480 | sign(T& packet, const IdentityCertificate& certificate) |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 481 | { |
| 482 | SignatureSha256WithRsa signature; |
| 483 | signature.setKeyLocator(certificate.getName().getPrefix(-1)); |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 484 | |
| 485 | // For temporary usage, we support RSA + SHA256 only, but will support more. |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 486 | signPacketWrapper(packet, signature, certificate.getPublicKeyName(), DIGEST_ALGORITHM_SHA256); |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 487 | } |
| 488 | |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 489 | /** |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 490 | * @brief Generate a key pair for the specified identity. |
| 491 | * |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 492 | * @param identityName The name of the specified identity. |
| 493 | * @param isKsk true for generating a Key-Signing-Key (KSK), false for a Data-Signing-Key (KSK). |
| 494 | * @param keyType The type of the key pair, e.g. KEY_TYPE_RSA. |
| 495 | * @param keySize The size of the key pair. |
| 496 | * @return The name of the generated key. |
| 497 | */ |
| 498 | Name |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 499 | generateKeyPair(const Name& identityName, bool isKsk = false, KeyType keyType = KEY_TYPE_RSA, int keySize = 2048) |
| 500 | { |
| 501 | Name keyName = Info::getNewKeyName(identityName, isKsk); |
| 502 | |
| 503 | Tpm::generateKeyPairInTpm(keyName.toUri(), keyType, keySize); |
| 504 | |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 505 | shared_ptr<PublicKey> pubKey = Tpm::getPublicKeyFromTpm(keyName.toUri()); |
Yingdi Yu | ef26ee3 | 2014-01-15 16:41:14 -0800 | [diff] [blame] | 506 | Info::addPublicKey(keyName, keyType, *pubKey); |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 507 | |
| 508 | return keyName; |
| 509 | } |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 510 | |
Yingdi Yu | 8726f65 | 2014-01-23 10:35:12 -0800 | [diff] [blame] | 511 | /** |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 512 | * @brief Sign the data using a particular key. |
| 513 | * |
| 514 | * @param data Reference to the data packet. |
| 515 | * @param signature Signature to be added. |
Yingdi Yu | 8726f65 | 2014-01-23 10:35:12 -0800 | [diff] [blame] | 516 | * @param keyName The name of the signing key. |
| 517 | * @param digestAlgorithm the digest algorithm. |
| 518 | * @throws Tpm::Error |
| 519 | */ |
| 520 | void |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 521 | signPacketWrapper(Data& data, const SignatureSha256WithRsa& signature, const Name& keyName, DigestAlgorithm digestAlgorithm) |
Yingdi Yu | 8726f65 | 2014-01-23 10:35:12 -0800 | [diff] [blame] | 522 | { |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 523 | data.setSignature(signature); |
Yingdi Yu | 8726f65 | 2014-01-23 10:35:12 -0800 | [diff] [blame] | 524 | data.setSignatureValue |
| 525 | (Tpm::signInTpm(data.wireEncode().value(), |
| 526 | data.wireEncode().value_size() - data.getSignature().getValue().size(), |
| 527 | keyName, digestAlgorithm)); |
| 528 | } |
| 529 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 530 | /** |
| 531 | * @brief Sign the interest using a particular key. |
| 532 | * |
| 533 | * @param interest Reference to the interest packet. |
| 534 | * @param signature Signature to be added. |
| 535 | * @param keyName The name of the signing key. |
| 536 | * @param digestAlgorithm the digest algorithm. |
| 537 | * @throws Tpm::Error |
| 538 | */ |
| 539 | void |
| 540 | signPacketWrapper(Interest& interest, const SignatureSha256WithRsa& signature, const Name& keyName, DigestAlgorithm digestAlgorithm) |
| 541 | { |
| 542 | Name signedName = Name(interest.getName()).append(signature.getInfo()); |
| 543 | |
| 544 | Block sigValue = Tpm::signInTpm(signedName.wireEncode().value(), |
| 545 | signedName.wireEncode().value_size(), |
| 546 | keyName, |
| 547 | DIGEST_ALGORITHM_SHA256); |
| 548 | sigValue.encode(); |
| 549 | signedName.append(sigValue); |
| 550 | interest.setName(signedName); |
| 551 | } |
| 552 | |
| 553 | |
Jeff Thompson | 47c93cf | 2013-08-09 00:38:48 -0700 | [diff] [blame] | 554 | }; |
| 555 | |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 556 | } // namespace ndn |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 557 | |
Yingdi Yu | 0402092 | 2014-01-22 12:46:53 -0800 | [diff] [blame] | 558 | |
| 559 | |
Alexander Afanasyev | 3e08d5d | 2014-02-12 19:24:28 -0800 | [diff] [blame] | 560 | #if defined(NDN_CPP_HAVE_OSX_SECURITY) and defined(NDN_CPP_WITH_OSX_KEYCHAIN) |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 561 | |
| 562 | namespace ndn |
Alexander Afanasyev | e64788e | 2014-01-05 22:38:21 -0800 | [diff] [blame] | 563 | { |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 564 | typedef KeyChainImpl<SecPublicInfoSqlite3, SecTpmOsx> KeyChain; |
| 565 | }; |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 566 | |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 567 | #else |
Alexander Afanasyev | e64788e | 2014-01-05 22:38:21 -0800 | [diff] [blame] | 568 | |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 569 | namespace ndn |
Alexander Afanasyev | e64788e | 2014-01-05 22:38:21 -0800 | [diff] [blame] | 570 | { |
Yingdi Yu | 0402092 | 2014-01-22 12:46:53 -0800 | [diff] [blame] | 571 | typedef KeyChainImpl<SecPublicInfoSqlite3, SecTpmFile> KeyChain; |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 572 | }; |
Alexander Afanasyev | e64788e | 2014-01-05 22:38:21 -0800 | [diff] [blame] | 573 | |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 574 | #endif //NDN_CPP_HAVE_OSX_SECURITY |
Jeff Thompson | 47c93cf | 2013-08-09 00:38:48 -0700 | [diff] [blame] | 575 | |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 576 | #endif //NDN_SECURITY_KEY_CHAIN_HPP |