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 | |
| 9 | #ifndef NDN_KEY_CHAIN_HPP |
Jeff Thompson | 2d27e2f | 2013-08-09 12:55:00 -0700 | [diff] [blame] | 10 | #define NDN_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 | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 15 | |
Yingdi Yu | 0402092 | 2014-01-22 12:46:53 -0800 | [diff] [blame^] | 16 | //PublicInfo |
Yingdi Yu | 4f32463 | 2014-01-15 18:10:03 -0800 | [diff] [blame] | 17 | #include "sec-public-info-sqlite3.hpp" |
| 18 | #include "sec-public-info-memory.hpp" |
Yingdi Yu | 0402092 | 2014-01-22 12:46:53 -0800 | [diff] [blame^] | 19 | //TPM |
| 20 | #include "sec-tpm-file.hpp" |
Yingdi Yu | 4f32463 | 2014-01-15 18:10:03 -0800 | [diff] [blame] | 21 | #include "sec-tpm-memory.hpp" |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 22 | |
Yingdi Yu | 0402092 | 2014-01-22 12:46:53 -0800 | [diff] [blame^] | 23 | #ifdef NDN_CPP_HAVE_OSX_SECURITY |
| 24 | #include "sec-tpm-osx.hpp" |
| 25 | #endif |
| 26 | |
Jeff Thompson | 47c93cf | 2013-08-09 00:38:48 -0700 | [diff] [blame] | 27 | |
| 28 | namespace ndn { |
| 29 | |
Jeff Thompson | 2ce8f49 | 2013-09-17 18:01:25 -0700 | [diff] [blame] | 30 | /** |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 31 | * KeyChain is one of the main classes of the security library. |
Jeff Thompson | ffa36f9 | 2013-09-20 08:42:41 -0700 | [diff] [blame] | 32 | * |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 33 | * 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] | 34 | */ |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 35 | template<class Info, class Tpm> |
| 36 | class KeyChainImpl : public Info, public Tpm |
| 37 | { |
Jeff Thompson | 47c93cf | 2013-08-09 00:38:48 -0700 | [diff] [blame] | 38 | public: |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 39 | // struct Error : public std::runtime_error { Error(const std::string &what) : std::runtime_error(what) {} }; |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 40 | |
| 41 | /** |
| 42 | * Create an identity by creating a pair of Key-Signing-Key (KSK) for this identity and a self-signed certificate of the KSK. |
| 43 | * @param identityName The name of the identity. |
| 44 | * @return The key name of the auto-generated KSK of the identity. |
| 45 | */ |
| 46 | Name |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 47 | createIdentity(const Name& identityName) |
| 48 | { |
| 49 | if (!Info::doesIdentityExist(identityName)) { |
| 50 | Info::addIdentity(identityName); |
| 51 | |
| 52 | Name keyName = generateRSAKeyPairAsDefault(identityName, true); |
| 53 | |
| 54 | ptr_lib::shared_ptr<IdentityCertificate> selfCert = selfSign(keyName); |
| 55 | |
| 56 | Info::addCertificateAsDefault(*selfCert); |
| 57 | |
| 58 | return keyName; |
| 59 | } |
| 60 | else |
| 61 | return Name(); |
| 62 | } |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 63 | |
| 64 | /** |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 65 | * Generate a pair of RSA keys for the specified identity. |
| 66 | * @param identityName The name of the identity. |
| 67 | * @param isKsk true for generating a Key-Signing-Key (KSK), false for a Data-Signing-Key (KSK). |
| 68 | * @param keySize The size of the key. |
| 69 | * @return The generated key name. |
| 70 | */ |
| 71 | Name |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 72 | generateRSAKeyPair(const Name& identityName, bool isKsk = false, int keySize = 2048) |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 73 | { |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 74 | return generateKeyPair(identityName, isKsk, KEY_TYPE_RSA, keySize); |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 75 | } |
Alexander Afanasyev | 64a3d81 | 2014-01-05 23:35:05 -0800 | [diff] [blame] | 76 | |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 77 | /** |
| 78 | * Generate a pair of RSA keys for the specified identity and set it as default key for the identity. |
| 79 | * @param identityName The name of the identity. |
| 80 | * @param isKsk true for generating a Key-Signing-Key (KSK), false for a Data-Signing-Key (KSK). |
| 81 | * @param keySize The size of the key. |
| 82 | * @return The generated key name. |
| 83 | */ |
| 84 | Name |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 85 | generateRSAKeyPairAsDefault(const Name& identityName, bool isKsk = false, int keySize = 2048) |
| 86 | { |
| 87 | Name keyName = generateKeyPair(identityName, isKsk, KEY_TYPE_RSA, keySize); |
| 88 | |
| 89 | Info::setDefaultKeyNameForIdentity(keyName); |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 90 | |
| 91 | return keyName; |
| 92 | } |
Jeff Thompson | 79a2d5d | 2013-09-27 14:32:23 -0700 | [diff] [blame] | 93 | |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 94 | /** |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 95 | * Create an identity certificate for a public key managed by this IdentityManager. |
| 96 | * @param certificatePrefix The name of public key to be signed. |
| 97 | * @param signerCertificateName The name of signing certificate. |
| 98 | * @param notBefore The notBefore value in the validity field of the generated certificate. |
| 99 | * @param notAfter The notAfter vallue in validity field of the generated certificate. |
| 100 | * @return The name of generated identity certificate. |
| 101 | */ |
| 102 | ptr_lib::shared_ptr<IdentityCertificate> |
| 103 | createIdentityCertificate |
| 104 | (const Name& certificatePrefix, |
| 105 | const Name& signerCertificateName, |
| 106 | const MillisecondsSince1970& notBefore, |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 107 | const MillisecondsSince1970& notAfter) |
| 108 | { |
| 109 | Name keyName = getKeyNameFromCertificatePrefix(certificatePrefix); |
| 110 | |
| 111 | ptr_lib::shared_ptr<PublicKey> pubKey = Info::getPublicKey(keyName); |
| 112 | if (!pubKey) |
| 113 | throw std::runtime_error("Requested public key [" + keyName.toUri() + "] doesn't exist"); |
| 114 | |
| 115 | ptr_lib::shared_ptr<IdentityCertificate> certificate = |
| 116 | createIdentityCertificate(certificatePrefix, |
| 117 | *pubKey, |
| 118 | signerCertificateName, |
| 119 | notBefore, notAfter); |
| 120 | |
| 121 | Info::addCertificate(*certificate); |
| 122 | |
| 123 | return certificate; |
| 124 | } |
| 125 | |
Jeff Thompson | 79a2d5d | 2013-09-27 14:32:23 -0700 | [diff] [blame] | 126 | |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 127 | /** |
| 128 | * Create an identity certificate for a public key supplied by the caller. |
| 129 | * @param certificatePrefix The name of public key to be signed. |
| 130 | * @param publickey The public key to be signed. |
| 131 | * @param signerCertificateName The name of signing certificate. |
| 132 | * @param notBefore The notBefore value in the validity field of the generated certificate. |
| 133 | * @param notAfter The notAfter vallue in validity field of the generated certificate. |
| 134 | * @return The generated identity certificate. |
| 135 | */ |
| 136 | ptr_lib::shared_ptr<IdentityCertificate> |
| 137 | createIdentityCertificate |
| 138 | (const Name& certificatePrefix, |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 139 | const PublicKey& publicKey, |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 140 | const Name& signerCertificateName, |
| 141 | const MillisecondsSince1970& notBefore, |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 142 | const MillisecondsSince1970& notAfter) |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 143 | { |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 144 | ptr_lib::shared_ptr<IdentityCertificate> certificate (new IdentityCertificate()); |
| 145 | Name keyName = getKeyNameFromCertificatePrefix(certificatePrefix); |
| 146 | |
| 147 | Name certificateName = certificatePrefix; |
| 148 | certificateName.append("ID-CERT").appendVersion(); |
| 149 | |
| 150 | certificate->setName(certificateName); |
| 151 | certificate->setNotBefore(notBefore); |
| 152 | certificate->setNotAfter(notAfter); |
| 153 | certificate->setPublicKeyInfo(publicKey); |
| 154 | certificate->addSubjectDescription(CertificateSubjectDescription("2.5.4.41", keyName.toUri())); |
| 155 | certificate->encode(); |
| 156 | |
| 157 | sign(*certificate, signerCertificateName); |
| 158 | |
| 159 | return certificate; |
Alexander Afanasyev | bf1a67a | 2014-01-05 23:36:13 -0800 | [diff] [blame] | 160 | } |
| 161 | |
Yingdi Yu | 3c5887c | 2014-01-21 18:19:49 -0800 | [diff] [blame] | 162 | /** |
| 163 | * Fetch the private key for keyName and sign the data, and set the signature block of the data packet. |
| 164 | * Throw Error if signing fails. |
| 165 | * @param data Reference to the input data packet. |
| 166 | * @param keyName The name of the signing key. |
| 167 | * @param digestAlgorithm the digest algorithm. |
| 168 | */ |
| 169 | void |
| 170 | signInTpm(Data &data, const Name& keyName, DigestAlgorithm digestAlgorithm) |
| 171 | { |
| 172 | data.setSignatureValue |
| 173 | (Tpm::signInTpm(data.wireEncode().value(), |
| 174 | data.wireEncode().value_size() - data.getSignature().getValue().size(), |
| 175 | keyName, digestAlgorithm)); |
| 176 | } |
| 177 | |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 178 | void |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 179 | sign(Data &data) |
| 180 | { |
| 181 | if (!Info::defaultCertificate()) |
| 182 | { |
| 183 | Info::refreshDefaultCertificate(); |
| 184 | |
| 185 | if(!Info::defaultCertificate()) |
| 186 | throw std::runtime_error("Default IdentityCertificate cannot be determined"); |
| 187 | } |
| 188 | |
| 189 | sign(data, *Info::defaultCertificate()); |
| 190 | } |
Alexander Afanasyev | e64788e | 2014-01-05 22:38:21 -0800 | [diff] [blame] | 191 | |
Jeff Thompson | 47c93cf | 2013-08-09 00:38:48 -0700 | [diff] [blame] | 192 | /** |
Jeff Thompson | 2ce8f49 | 2013-09-17 18:01:25 -0700 | [diff] [blame] | 193 | * Wire encode the Data object, sign it and set its signature. |
Jeff Thompson | 2ce8f49 | 2013-09-17 18:01:25 -0700 | [diff] [blame] | 194 | * @param data The Data object to be signed. This updates its signature and key locator field and wireEncoding. |
Jeff Thompson | 9296f0c | 2013-09-23 18:10:27 -0700 | [diff] [blame] | 195 | * @param certificateName The certificate name of the key to use for signing. If omitted, infer the signing identity from the data packet name. |
Jeff Thompson | 3c73da4 | 2013-08-12 11:19:05 -0700 | [diff] [blame] | 196 | */ |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 197 | void |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 198 | sign(Data& data, const Name& certificateName) |
| 199 | { |
| 200 | ptr_lib::shared_ptr<IdentityCertificate> cert = Info::getCertificate(certificateName); |
| 201 | if (!cert) |
| 202 | throw std::runtime_error("Requested certificate [" + certificateName.toUri() + "] doesn't exist"); |
| 203 | |
| 204 | SignatureSha256WithRsa signature; |
| 205 | signature.setKeyLocator(certificateName.getPrefix(-1)); // implicit conversion should take care |
| 206 | data.setSignature(signature); |
| 207 | |
| 208 | // For temporary usage, we support RSA + SHA256 only, but will support more. |
Yingdi Yu | 3c5887c | 2014-01-21 18:19:49 -0800 | [diff] [blame] | 209 | signInTpm(data, cert->getPublicKeyName(), DIGEST_ALGORITHM_SHA256); |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 210 | } |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 211 | |
| 212 | void |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 213 | sign(Data& data, const IdentityCertificate& certificate) |
| 214 | { |
| 215 | SignatureSha256WithRsa signature; |
| 216 | signature.setKeyLocator(certificate.getName().getPrefix(-1)); |
| 217 | data.setSignature(signature); |
| 218 | |
| 219 | // For temporary usage, we support RSA + SHA256 only, but will support more. |
Yingdi Yu | 3c5887c | 2014-01-21 18:19:49 -0800 | [diff] [blame] | 220 | signInTpm(data, certificate.getPublicKeyName(), DIGEST_ALGORITHM_SHA256); |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 221 | } |
Jeff Thompson | 79a2d5d | 2013-09-27 14:32:23 -0700 | [diff] [blame] | 222 | |
Jeff Thompson | 29ce310 | 2013-09-27 11:47:48 -0700 | [diff] [blame] | 223 | /** |
Jeff Thompson | c01e178 | 2013-10-21 14:08:42 -0700 | [diff] [blame] | 224 | * Sign the byte array using a certificate name and return a Signature object. |
| 225 | * @param buffer The byte array to be signed. |
| 226 | * @param bufferLength the length of buffer. |
| 227 | * @param certificateName The certificate name used to get the signing key and which will be put into KeyLocator. |
| 228 | * @return The Signature. |
| 229 | */ |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 230 | Signature |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 231 | sign(const uint8_t* buffer, size_t bufferLength, const Name& certificateName) |
| 232 | { |
| 233 | ptr_lib::shared_ptr<IdentityCertificate> cert = Info::getCertificate(certificateName); |
| 234 | if (!cert) |
| 235 | throw std::runtime_error("Requested certificate [" + certificateName.toUri() + "] doesn't exist"); |
| 236 | |
| 237 | SignatureSha256WithRsa signature; |
| 238 | signature.setKeyLocator(certificateName.getPrefix(-1)); // implicit conversion should take care |
| 239 | |
| 240 | // For temporary usage, we support RSA + SHA256 only, but will support more. |
Yingdi Yu | b4bb85a | 2014-01-16 10:11:04 -0800 | [diff] [blame] | 241 | signature.setValue(Tpm::signInTpm(buffer, bufferLength, cert->getPublicKeyName(), DIGEST_ALGORITHM_SHA256)); |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 242 | return signature; |
| 243 | } |
Jeff Thompson | c01e178 | 2013-10-21 14:08:42 -0700 | [diff] [blame] | 244 | |
| 245 | /** |
Jeff Thompson | 29ce310 | 2013-09-27 11:47:48 -0700 | [diff] [blame] | 246 | * Wire encode the Data object, sign it and set its signature. |
Jeff Thompson | 29ce310 | 2013-09-27 11:47:48 -0700 | [diff] [blame] | 247 | * @param data The Data object to be signed. This updates its signature and key locator field and wireEncoding. |
| 248 | * @param identityName The identity name for the key to use for signing. If omitted, infer the signing identity from the data packet name. |
Jeff Thompson | 29ce310 | 2013-09-27 11:47:48 -0700 | [diff] [blame] | 249 | */ |
| 250 | void |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 251 | signByIdentity(Data& data, const Name& identityName = Name()) |
| 252 | { |
| 253 | Name signingCertificateName = Info::getDefaultCertificateNameForIdentity(identityName); |
| 254 | |
| 255 | if (signingCertificateName.getComponentCount() == 0) |
| 256 | throw std::runtime_error("No qualified certificate name found!"); |
| 257 | |
| 258 | sign(data, signingCertificateName); |
| 259 | } |
Jeff Thompson | 3c73da4 | 2013-08-12 11:19:05 -0700 | [diff] [blame] | 260 | |
| 261 | /** |
Jeff Thompson | c01e178 | 2013-10-21 14:08:42 -0700 | [diff] [blame] | 262 | * Sign the byte array using an identity name and return a Signature object. |
| 263 | * @param buffer The byte array to be signed. |
| 264 | * @param bufferLength the length of buffer. |
| 265 | * @param identityName The identity name. |
| 266 | * @return The Signature. |
| 267 | */ |
Alexander Afanasyev | 64a3d81 | 2014-01-05 23:35:05 -0800 | [diff] [blame] | 268 | Signature |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 269 | signByIdentity(const uint8_t* buffer, size_t bufferLength, const Name& identityName = Name()) |
| 270 | { |
| 271 | Name signingCertificateName = Info::getDefaultCertificateNameForIdentity(identityName); |
| 272 | |
| 273 | if (signingCertificateName.size() == 0) |
| 274 | throw std::runtime_error("No qualified certificate name found!"); |
| 275 | |
| 276 | return sign(buffer, bufferLength, signingCertificateName); |
| 277 | } |
Jeff Thompson | c01e178 | 2013-10-21 14:08:42 -0700 | [diff] [blame] | 278 | |
| 279 | /** |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 280 | * Generate a self-signed certificate for a public key. |
| 281 | * @param keyName The name of the public key. |
| 282 | * @return The generated certificate. |
| 283 | */ |
| 284 | ptr_lib::shared_ptr<IdentityCertificate> |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 285 | selfSign(const Name& keyName) |
| 286 | { |
Yingdi Yu | 7ea6950 | 2014-01-15 17:21:29 -0800 | [diff] [blame] | 287 | if(keyName.empty()) |
| 288 | throw std::runtime_error("Incorrect key name: " + keyName.toUri()); |
| 289 | |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 290 | ptr_lib::shared_ptr<IdentityCertificate> certificate = ptr_lib::make_shared<IdentityCertificate>(); |
| 291 | |
| 292 | Name certificateName = keyName.getPrefix(-1); |
| 293 | certificateName.append("KEY").append(keyName.get(-1)).append("ID-CERT").appendVersion(); |
| 294 | |
| 295 | ptr_lib::shared_ptr<PublicKey> pubKey = Info::getPublicKey(keyName); |
| 296 | if (!pubKey) |
| 297 | throw std::runtime_error("Requested public key [" + keyName.toUri() + "] doesn't exist"); |
| 298 | |
| 299 | certificate->setName(certificateName); |
| 300 | certificate->setNotBefore(getNow()); |
| 301 | certificate->setNotAfter(getNow() + 630720000 /* 20 years*/); |
| 302 | certificate->setPublicKeyInfo(*pubKey); |
| 303 | certificate->addSubjectDescription(CertificateSubjectDescription("2.5.4.41", keyName.toUri())); |
| 304 | certificate->encode(); |
| 305 | |
| 306 | selfSign(*certificate); |
| 307 | return certificate; |
| 308 | } |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 309 | |
| 310 | /** |
| 311 | * @brief Self-sign the supplied identity certificate |
Jeff Thompson | 8efe5ad | 2013-08-20 17:36:38 -0700 | [diff] [blame] | 312 | */ |
Jeff Thompson | 2ce8f49 | 2013-09-17 18:01:25 -0700 | [diff] [blame] | 313 | void |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 314 | selfSign (IdentityCertificate& cert) |
| 315 | { |
| 316 | SignatureSha256WithRsa signature; |
| 317 | signature.setKeyLocator(cert.getName().getPrefix(-1)); // implicit conversion should take care |
| 318 | cert.setSignature(signature); |
| 319 | |
| 320 | // For temporary usage, we support RSA + SHA256 only, but will support more. |
Yingdi Yu | 3c5887c | 2014-01-21 18:19:49 -0800 | [diff] [blame] | 321 | signInTpm(cert, cert.getPublicKeyName(), DIGEST_ALGORITHM_SHA256); |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 322 | } |
| 323 | |
Jeff Thompson | 8efe5ad | 2013-08-20 17:36:38 -0700 | [diff] [blame] | 324 | |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 325 | private: |
| 326 | /** |
| 327 | * Generate a key pair for the specified identity. |
| 328 | * @param identityName The name of the specified identity. |
| 329 | * @param isKsk true for generating a Key-Signing-Key (KSK), false for a Data-Signing-Key (KSK). |
| 330 | * @param keyType The type of the key pair, e.g. KEY_TYPE_RSA. |
| 331 | * @param keySize The size of the key pair. |
| 332 | * @return The name of the generated key. |
| 333 | */ |
| 334 | Name |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 335 | generateKeyPair(const Name& identityName, bool isKsk = false, KeyType keyType = KEY_TYPE_RSA, int keySize = 2048) |
| 336 | { |
| 337 | Name keyName = Info::getNewKeyName(identityName, isKsk); |
| 338 | |
| 339 | Tpm::generateKeyPairInTpm(keyName.toUri(), keyType, keySize); |
| 340 | |
| 341 | ptr_lib::shared_ptr<PublicKey> pubKey = Tpm::getPublicKeyFromTpm(keyName.toUri()); |
Yingdi Yu | ef26ee3 | 2014-01-15 16:41:14 -0800 | [diff] [blame] | 342 | Info::addPublicKey(keyName, keyType, *pubKey); |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 343 | |
| 344 | return keyName; |
| 345 | } |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 346 | |
| 347 | static Name |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 348 | getKeyNameFromCertificatePrefix(const Name& certificatePrefix) |
| 349 | { |
| 350 | Name result; |
Alexander Afanasyev | bf1a67a | 2014-01-05 23:36:13 -0800 | [diff] [blame] | 351 | |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 352 | std::string keyString("KEY"); |
| 353 | int i = 0; |
| 354 | for(; i < certificatePrefix.size(); i++) { |
| 355 | if (certificatePrefix.get(i).toEscapedString() == keyString) |
| 356 | break; |
| 357 | } |
Alexander Afanasyev | bf1a67a | 2014-01-05 23:36:13 -0800 | [diff] [blame] | 358 | |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 359 | if (i >= certificatePrefix.size()) |
| 360 | throw std::runtime_error("Identity Certificate Prefix does not have a KEY component"); |
Alexander Afanasyev | bd5ba40 | 2014-01-05 22:41:09 -0800 | [diff] [blame] | 361 | |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 362 | result.append(certificatePrefix.getSubName(0, i)); |
| 363 | result.append(certificatePrefix.getSubName(i + 1, certificatePrefix.size()-i-1)); |
| 364 | |
| 365 | return result; |
| 366 | } |
| 367 | |
Jeff Thompson | 47c93cf | 2013-08-09 00:38:48 -0700 | [diff] [blame] | 368 | }; |
| 369 | |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 370 | } |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 371 | |
Yingdi Yu | 0402092 | 2014-01-22 12:46:53 -0800 | [diff] [blame^] | 372 | |
| 373 | |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 374 | #ifdef NDN_CPP_HAVE_OSX_SECURITY |
| 375 | |
| 376 | namespace ndn |
Alexander Afanasyev | e64788e | 2014-01-05 22:38:21 -0800 | [diff] [blame] | 377 | { |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 378 | typedef KeyChainImpl<SecPublicInfoSqlite3, SecTpmOsx> KeyChain; |
| 379 | }; |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 380 | |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 381 | #else |
Alexander Afanasyev | e64788e | 2014-01-05 22:38:21 -0800 | [diff] [blame] | 382 | |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 383 | namespace ndn |
Alexander Afanasyev | e64788e | 2014-01-05 22:38:21 -0800 | [diff] [blame] | 384 | { |
Yingdi Yu | 0402092 | 2014-01-22 12:46:53 -0800 | [diff] [blame^] | 385 | typedef KeyChainImpl<SecPublicInfoSqlite3, SecTpmFile> KeyChain; |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 386 | }; |
Alexander Afanasyev | e64788e | 2014-01-05 22:38:21 -0800 | [diff] [blame] | 387 | |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 388 | #endif //NDN_CPP_HAVE_OSX_SECURITY |
Jeff Thompson | 47c93cf | 2013-08-09 00:38:48 -0700 | [diff] [blame] | 389 | |
| 390 | #endif |