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