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 | 4147191 | 2013-09-12 16:21:50 -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 | 06e787d | 2013-09-12 19:00:55 -0700 | [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 | 4147191 | 2013-09-12 16:21:50 -0700 | [diff] [blame] | 6 | * See COPYING for copyright and distribution information. |
| 7 | */ |
| 8 | |
Jeff Thompson | d63baba | 2013-10-18 17:47:58 -0700 | [diff] [blame] | 9 | #include <ndn-cpp/ndn-cpp-config.h> |
| 10 | #if NDN_CPP_HAVE_TIME_H |
| 11 | #include <time.h> |
| 12 | #endif |
| 13 | #if NDN_CPP_HAVE_SYS_TIME_H |
| 14 | #include <sys/time.h> |
| 15 | #endif |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 16 | #include <ctime> |
| 17 | #include <fstream> |
Jeff Thompson | db68689 | 2013-10-18 17:16:31 -0700 | [diff] [blame] | 18 | #include <math.h> |
Jeff Thompson | 71b2f87 | 2013-12-17 12:03:17 -0800 | [diff] [blame] | 19 | #include <ndn-cpp/key-locator.hpp> |
Alexander Afanasyev | bf1a67a | 2014-01-05 23:36:13 -0800 | [diff] [blame] | 20 | |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 21 | #include "../../util/logging.hpp" |
Jeff Thompson | db68689 | 2013-10-18 17:16:31 -0700 | [diff] [blame] | 22 | #include "../../c/util/time.h" |
Jeff Thompson | 25b4e61 | 2013-10-10 16:03:24 -0700 | [diff] [blame] | 23 | #include <ndn-cpp/security/identity/identity-manager.hpp> |
Alexander Afanasyev | bf1a67a | 2014-01-05 23:36:13 -0800 | [diff] [blame] | 24 | #include "../certificate/identity-certificate.hpp" |
| 25 | #include "../signature/signature-sha256-with-rsa.hpp" |
Jeff Thompson | 4147191 | 2013-09-12 16:21:50 -0700 | [diff] [blame] | 26 | |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 27 | INIT_LOGGER("ndn.security.IdentityManager") |
| 28 | |
Jeff Thompson | 9296f0c | 2013-09-23 18:10:27 -0700 | [diff] [blame] | 29 | using namespace std; |
Jeff Thompson | 4147191 | 2013-09-12 16:21:50 -0700 | [diff] [blame] | 30 | |
Jeff Thompson | 9296f0c | 2013-09-23 18:10:27 -0700 | [diff] [blame] | 31 | namespace ndn { |
Jeff Thompson | 86e1d75 | 2013-09-17 17:22:38 -0700 | [diff] [blame] | 32 | |
Alexander Afanasyev | bf1a67a | 2014-01-05 23:36:13 -0800 | [diff] [blame] | 33 | const ptr_lib::shared_ptr<IdentityStorage> IdentityManager::DefaultIdentityStorage = ptr_lib::shared_ptr<IdentityStorage>(); |
| 34 | const ptr_lib::shared_ptr<PrivateKeyStorage> IdentityManager::DefaultPrivateKeyStorage = ptr_lib::shared_ptr<PrivateKeyStorage>(); |
| 35 | |
| 36 | IdentityManager::IdentityManager(const ptr_lib::shared_ptr<IdentityStorage> &identityStorage /* = DefaultIdentityStorage */, |
| 37 | const ptr_lib::shared_ptr<PrivateKeyStorage> &privateKeyStorage /* = DefaultPrivateKeyStorage */) |
| 38 | : identityStorage_(identityStorage) |
| 39 | , privateKeyStorage_(privateKeyStorage) |
| 40 | { |
| 41 | } |
| 42 | |
Jeff Thompson | e7e069b | 2013-09-27 15:48:48 -0700 | [diff] [blame] | 43 | Name |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 44 | IdentityManager::createIdentity(const Name& identityName) |
Jeff Thompson | e7e069b | 2013-09-27 15:48:48 -0700 | [diff] [blame] | 45 | { |
Alexander Afanasyev | bf1a67a | 2014-01-05 23:36:13 -0800 | [diff] [blame] | 46 | if (!info().doesIdentityExist(identityName)) { |
| 47 | _LOG_DEBUG("Create Identity"); |
| 48 | info().addIdentity(identityName); |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 49 | |
Alexander Afanasyev | bf1a67a | 2014-01-05 23:36:13 -0800 | [diff] [blame] | 50 | _LOG_DEBUG("Create Default RSA key pair"); |
| 51 | Name keyName = generateRSAKeyPairAsDefault(identityName, true); |
Jeff Thompson | e7e069b | 2013-09-27 15:48:48 -0700 | [diff] [blame] | 52 | |
Alexander Afanasyev | bf1a67a | 2014-01-05 23:36:13 -0800 | [diff] [blame] | 53 | _LOG_DEBUG("Create self-signed certificate"); |
| 54 | ptr_lib::shared_ptr<IdentityCertificate> selfCert = selfSign(keyName); |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 55 | |
Alexander Afanasyev | bf1a67a | 2014-01-05 23:36:13 -0800 | [diff] [blame] | 56 | _LOG_DEBUG("Add self-signed certificate as default"); |
Jeff Thompson | e7e069b | 2013-09-27 15:48:48 -0700 | [diff] [blame] | 57 | |
Alexander Afanasyev | bf1a67a | 2014-01-05 23:36:13 -0800 | [diff] [blame] | 58 | addCertificateAsDefault(*selfCert); |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 59 | |
Alexander Afanasyev | bf1a67a | 2014-01-05 23:36:13 -0800 | [diff] [blame] | 60 | return keyName; |
Jeff Thompson | e7e069b | 2013-09-27 15:48:48 -0700 | [diff] [blame] | 61 | } |
| 62 | else |
Alexander Afanasyev | bf1a67a | 2014-01-05 23:36:13 -0800 | [diff] [blame] | 63 | throw Error("Identity has already been created!"); |
Jeff Thompson | e7e069b | 2013-09-27 15:48:48 -0700 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | Name |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 67 | IdentityManager::generateKeyPair(const Name& identityName, bool isKsk, KeyType keyType, int keySize) |
| 68 | { |
| 69 | _LOG_DEBUG("Get new key ID"); |
Alexander Afanasyev | bf1a67a | 2014-01-05 23:36:13 -0800 | [diff] [blame] | 70 | Name keyName = info().getNewKeyName(identityName, isKsk); |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 71 | |
| 72 | _LOG_DEBUG("Generate key pair in private storage"); |
Alexander Afanasyev | bf1a67a | 2014-01-05 23:36:13 -0800 | [diff] [blame] | 73 | tpm().generateKeyPair(keyName.toUri(), keyType, keySize); |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 74 | |
| 75 | _LOG_DEBUG("Create a key record in public storage"); |
Alexander Afanasyev | bf1a67a | 2014-01-05 23:36:13 -0800 | [diff] [blame] | 76 | ptr_lib::shared_ptr<PublicKey> pubKey = tpm().getPublicKey(keyName.toUri()); |
| 77 | info().addKey(keyName, keyType, *pubKey); |
Jeff Thompson | 418b05a | 2013-10-22 17:48:54 -0700 | [diff] [blame] | 78 | |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 79 | return keyName; |
| 80 | } |
| 81 | |
| 82 | Name |
Jeff Thompson | e7e069b | 2013-09-27 15:48:48 -0700 | [diff] [blame] | 83 | IdentityManager::generateRSAKeyPair(const Name& identityName, bool isKsk, int keySize) |
| 84 | { |
| 85 | Name keyName = generateKeyPair(identityName, isKsk, KEY_TYPE_RSA, keySize); |
Jeff Thompson | 418b05a | 2013-10-22 17:48:54 -0700 | [diff] [blame] | 86 | |
Jeff Thompson | e7e069b | 2013-09-27 15:48:48 -0700 | [diff] [blame] | 87 | return keyName; |
| 88 | } |
| 89 | |
| 90 | Name |
| 91 | IdentityManager::generateRSAKeyPairAsDefault(const Name& identityName, bool isKsk, int keySize) |
| 92 | { |
Alexander Afanasyev | e64788e | 2014-01-05 22:38:21 -0800 | [diff] [blame] | 93 | defaultCertificate_.reset(); |
| 94 | |
Jeff Thompson | e7e069b | 2013-09-27 15:48:48 -0700 | [diff] [blame] | 95 | Name keyName = generateKeyPair(identityName, isKsk, KEY_TYPE_RSA, keySize); |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 96 | |
Alexander Afanasyev | bf1a67a | 2014-01-05 23:36:13 -0800 | [diff] [blame] | 97 | info().setDefaultKeyNameForIdentity(keyName, identityName); |
Jeff Thompson | e7e069b | 2013-09-27 15:48:48 -0700 | [diff] [blame] | 98 | |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 99 | return keyName; |
| 100 | } |
| 101 | |
Alexander Afanasyev | bf1a67a | 2014-01-05 23:36:13 -0800 | [diff] [blame] | 102 | ptr_lib::shared_ptr<IdentityCertificate> |
Jeff Thompson | 418b05a | 2013-10-22 17:48:54 -0700 | [diff] [blame] | 103 | IdentityManager::createIdentityCertificate(const Name& certificatePrefix, |
| 104 | const Name& signerCertificateName, |
| 105 | const MillisecondsSince1970& notBefore, |
| 106 | const MillisecondsSince1970& notAfter) |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 107 | { |
Jeff Thompson | 418b05a | 2013-10-22 17:48:54 -0700 | [diff] [blame] | 108 | Name keyName = getKeyNameFromCertificatePrefix(certificatePrefix); |
Alexander Afanasyev | e64788e | 2014-01-05 22:38:21 -0800 | [diff] [blame] | 109 | |
| 110 | ptr_lib::shared_ptr<PublicKey> pubKey = info().getKey(keyName); |
| 111 | if (!pubKey) |
| 112 | throw Error("Requested public key [" + keyName.toUri() + "] doesn't exist"); |
Jeff Thompson | 418b05a | 2013-10-22 17:48:54 -0700 | [diff] [blame] | 113 | |
Alexander Afanasyev | bf1a67a | 2014-01-05 23:36:13 -0800 | [diff] [blame] | 114 | ptr_lib::shared_ptr<IdentityCertificate> certificate = |
| 115 | createIdentityCertificate(certificatePrefix, |
Alexander Afanasyev | e64788e | 2014-01-05 22:38:21 -0800 | [diff] [blame] | 116 | *pubKey, |
Alexander Afanasyev | bf1a67a | 2014-01-05 23:36:13 -0800 | [diff] [blame] | 117 | signerCertificateName, |
| 118 | notBefore, notAfter); |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 119 | |
Alexander Afanasyev | bf1a67a | 2014-01-05 23:36:13 -0800 | [diff] [blame] | 120 | info().addCertificate(*certificate); |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 121 | |
Alexander Afanasyev | bf1a67a | 2014-01-05 23:36:13 -0800 | [diff] [blame] | 122 | return certificate; |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | ptr_lib::shared_ptr<IdentityCertificate> |
Jeff Thompson | 418b05a | 2013-10-22 17:48:54 -0700 | [diff] [blame] | 126 | IdentityManager::createIdentityCertificate(const Name& certificatePrefix, |
| 127 | const PublicKey& publicKey, |
| 128 | const Name& signerCertificateName, |
| 129 | const MillisecondsSince1970& notBefore, |
| 130 | const MillisecondsSince1970& notAfter) |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 131 | { |
Alexander Afanasyev | bf1a67a | 2014-01-05 23:36:13 -0800 | [diff] [blame] | 132 | ptr_lib::shared_ptr<IdentityCertificate> certificate (new IdentityCertificate()); |
Jeff Thompson | 418b05a | 2013-10-22 17:48:54 -0700 | [diff] [blame] | 133 | Name keyName = getKeyNameFromCertificatePrefix(certificatePrefix); |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 134 | |
Jeff Thompson | 418b05a | 2013-10-22 17:48:54 -0700 | [diff] [blame] | 135 | Name certificateName = certificatePrefix; |
Alexander Afanasyev | bf1a67a | 2014-01-05 23:36:13 -0800 | [diff] [blame] | 136 | certificateName.append("ID-CERT").appendVersion(); |
Jeff Thompson | 418b05a | 2013-10-22 17:48:54 -0700 | [diff] [blame] | 137 | |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 138 | certificate->setName(certificateName); |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 139 | certificate->setNotBefore(notBefore); |
| 140 | certificate->setNotAfter(notAfter); |
| 141 | certificate->setPublicKeyInfo(publicKey); |
Jeff Thompson | db68689 | 2013-10-18 17:16:31 -0700 | [diff] [blame] | 142 | certificate->addSubjectDescription(CertificateSubjectDescription("2.5.4.41", keyName.toUri())); |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 143 | certificate->encode(); |
| 144 | |
Alexander Afanasyev | bf1a67a | 2014-01-05 23:36:13 -0800 | [diff] [blame] | 145 | signByCertificate(*certificate, signerCertificateName); |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 146 | |
Alexander Afanasyev | bf1a67a | 2014-01-05 23:36:13 -0800 | [diff] [blame] | 147 | return certificate; |
| 148 | } |
| 149 | |
| 150 | ptr_lib::shared_ptr<IdentityCertificate> |
| 151 | IdentityManager::selfSign(const Name& keyName) |
| 152 | { |
| 153 | ptr_lib::shared_ptr<IdentityCertificate> certificate(new IdentityCertificate()); |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 154 | |
Alexander Afanasyev | bf1a67a | 2014-01-05 23:36:13 -0800 | [diff] [blame] | 155 | Name certificateName = keyName.getSubName(0, keyName.size() - 1); |
| 156 | certificateName.append("KEY").append(keyName.get(keyName.size() - 1)).append("ID-CERT").appendVersion(); |
Alexander Afanasyev | e64788e | 2014-01-05 22:38:21 -0800 | [diff] [blame] | 157 | |
| 158 | ptr_lib::shared_ptr<PublicKey> pubKey = info().getKey(keyName); |
| 159 | if (!pubKey) |
| 160 | throw Error("Requested public key [" + keyName.toUri() + "] doesn't exist"); |
Alexander Afanasyev | bf1a67a | 2014-01-05 23:36:13 -0800 | [diff] [blame] | 161 | |
| 162 | certificate->setName(certificateName); |
| 163 | certificate->setNotBefore(ndn_getNowMilliseconds()); |
| 164 | certificate->setNotAfter(ndn_getNowMilliseconds() + 630720000 /* 20 years*/); |
Alexander Afanasyev | e64788e | 2014-01-05 22:38:21 -0800 | [diff] [blame] | 165 | certificate->setPublicKeyInfo(*pubKey); |
Alexander Afanasyev | bf1a67a | 2014-01-05 23:36:13 -0800 | [diff] [blame] | 166 | certificate->addSubjectDescription(CertificateSubjectDescription("2.5.4.41", keyName.toUri())); |
| 167 | certificate->encode(); |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 168 | |
Alexander Afanasyev | bf1a67a | 2014-01-05 23:36:13 -0800 | [diff] [blame] | 169 | selfSign(*certificate); |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 170 | return certificate; |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | void |
| 174 | IdentityManager::addCertificateAsDefault(const IdentityCertificate& certificate) |
| 175 | { |
Alexander Afanasyev | bf1a67a | 2014-01-05 23:36:13 -0800 | [diff] [blame] | 176 | info().addCertificate(certificate); |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 177 | |
Jeff Thompson | 418b05a | 2013-10-22 17:48:54 -0700 | [diff] [blame] | 178 | setDefaultCertificateForKey(certificate); |
Jeff Thompson | e7e069b | 2013-09-27 15:48:48 -0700 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | void |
Jeff Thompson | 418b05a | 2013-10-22 17:48:54 -0700 | [diff] [blame] | 182 | IdentityManager::addCertificateAsIdentityDefault(const IdentityCertificate& certificate) |
Jeff Thompson | e7e069b | 2013-09-27 15:48:48 -0700 | [diff] [blame] | 183 | { |
Alexander Afanasyev | bf1a67a | 2014-01-05 23:36:13 -0800 | [diff] [blame] | 184 | info().addCertificate(certificate); |
Jeff Thompson | 418b05a | 2013-10-22 17:48:54 -0700 | [diff] [blame] | 185 | |
| 186 | Name keyName = certificate.getPublicKeyName(); |
| 187 | |
| 188 | setDefaultKeyForIdentity(keyName); |
| 189 | |
| 190 | setDefaultCertificateForKey(certificate); |
| 191 | } |
| 192 | |
| 193 | void |
| 194 | IdentityManager::setDefaultCertificateForKey(const IdentityCertificate& certificate) |
| 195 | { |
Alexander Afanasyev | e64788e | 2014-01-05 22:38:21 -0800 | [diff] [blame] | 196 | defaultCertificate_.reset(); |
| 197 | |
Jeff Thompson | 418b05a | 2013-10-22 17:48:54 -0700 | [diff] [blame] | 198 | Name keyName = certificate.getPublicKeyName(); |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 199 | |
Alexander Afanasyev | bf1a67a | 2014-01-05 23:36:13 -0800 | [diff] [blame] | 200 | if(!info().doesKeyExist(keyName)) |
| 201 | throw Error("No corresponding Key record for certificate!"); |
Jeff Thompson | e7e069b | 2013-09-27 15:48:48 -0700 | [diff] [blame] | 202 | |
Alexander Afanasyev | bf1a67a | 2014-01-05 23:36:13 -0800 | [diff] [blame] | 203 | info().setDefaultCertificateNameForKey(keyName, certificate.getName()); |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 204 | } |
Alexander Afanasyev | e64788e | 2014-01-05 22:38:21 -0800 | [diff] [blame] | 205 | |
| 206 | void |
| 207 | IdentityManager::sign(Data &data) |
| 208 | { |
| 209 | if (!defaultCertificate_) |
| 210 | { |
| 211 | defaultCertificate_ = info().getCertificate( |
| 212 | info().getDefaultCertificateNameForIdentity( |
| 213 | info().getDefaultIdentity())); |
| 214 | |
| 215 | if(!defaultCertificate_) |
| 216 | throw Error("Default IdentityCertificate cannot be determined"); |
| 217 | } |
| 218 | |
| 219 | signByCertificate(data, *defaultCertificate_); |
| 220 | } |
| 221 | |
Alexander Afanasyev | bf1a67a | 2014-01-05 23:36:13 -0800 | [diff] [blame] | 222 | Signature |
Jeff Thompson | c01e178 | 2013-10-21 14:08:42 -0700 | [diff] [blame] | 223 | IdentityManager::signByCertificate(const uint8_t* buffer, size_t bufferLength, const Name& certificateName) |
Jeff Thompson | 7494261 | 2013-10-24 16:42:32 -0700 | [diff] [blame] | 224 | { |
Alexander Afanasyev | bf1a67a | 2014-01-05 23:36:13 -0800 | [diff] [blame] | 225 | ptr_lib::shared_ptr<IdentityCertificate> cert = info().getCertificate(certificateName); |
Alexander Afanasyev | e64788e | 2014-01-05 22:38:21 -0800 | [diff] [blame] | 226 | if (!cert) |
| 227 | throw Error("Requested certificate [" + certificateName.toUri() + "] doesn't exist"); |
| 228 | |
Alexander Afanasyev | bf1a67a | 2014-01-05 23:36:13 -0800 | [diff] [blame] | 229 | SignatureSha256WithRsa signature; |
| 230 | signature.setKeyLocator(certificateName.getPrefix(-1)); // implicit conversion should take care |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 231 | |
Alexander Afanasyev | bf1a67a | 2014-01-05 23:36:13 -0800 | [diff] [blame] | 232 | // For temporary usage, we support RSA + SHA256 only, but will support more. |
| 233 | signature.setValue(tpm().sign(buffer, bufferLength, signature, cert->getPublicKeyName(), DIGEST_ALGORITHM_SHA256)); |
| 234 | return signature; |
Jeff Thompson | e7e069b | 2013-09-27 15:48:48 -0700 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | void |
Alexander Afanasyev | bf1a67a | 2014-01-05 23:36:13 -0800 | [diff] [blame] | 238 | IdentityManager::signByCertificate(Data &data, const Name &certificateName) |
Jeff Thompson | 4147191 | 2013-09-12 16:21:50 -0700 | [diff] [blame] | 239 | { |
Alexander Afanasyev | bf1a67a | 2014-01-05 23:36:13 -0800 | [diff] [blame] | 240 | ptr_lib::shared_ptr<IdentityCertificate> cert = info().getCertificate(certificateName); |
Alexander Afanasyev | e64788e | 2014-01-05 22:38:21 -0800 | [diff] [blame] | 241 | if (!cert) |
| 242 | throw Error("Requested certificate [" + certificateName.toUri() + "] doesn't exist"); |
| 243 | |
Alexander Afanasyev | bf1a67a | 2014-01-05 23:36:13 -0800 | [diff] [blame] | 244 | SignatureSha256WithRsa signature; |
| 245 | signature.setKeyLocator(certificateName.getPrefix(-1)); // implicit conversion should take care |
Jeff Thompson | 86e1d75 | 2013-09-17 17:22:38 -0700 | [diff] [blame] | 246 | |
| 247 | // For temporary usage, we support RSA + SHA256 only, but will support more. |
Alexander Afanasyev | bf1a67a | 2014-01-05 23:36:13 -0800 | [diff] [blame] | 248 | signature.setValue(tpm().sign(data, signature, cert->getPublicKeyName(), DIGEST_ALGORITHM_SHA256)); |
| 249 | data.setSignature(signature); |
Jeff Thompson | 4147191 | 2013-09-12 16:21:50 -0700 | [diff] [blame] | 250 | } |
| 251 | |
Alexander Afanasyev | bf1a67a | 2014-01-05 23:36:13 -0800 | [diff] [blame] | 252 | void |
Alexander Afanasyev | e64788e | 2014-01-05 22:38:21 -0800 | [diff] [blame] | 253 | IdentityManager::signByCertificate(Data& data, const IdentityCertificate& certificate) |
| 254 | { |
| 255 | SignatureSha256WithRsa signature; |
| 256 | signature.setKeyLocator(certificate.getName().getPrefix(-1)); |
| 257 | |
| 258 | // For temporary usage, we support RSA + SHA256 only, but will support more. |
| 259 | signature.setValue(tpm().sign(data, signature, certificate.getPublicKeyName(), DIGEST_ALGORITHM_SHA256)); |
| 260 | data.setSignature(signature); |
| 261 | } |
| 262 | |
| 263 | void |
Alexander Afanasyev | bf1a67a | 2014-01-05 23:36:13 -0800 | [diff] [blame] | 264 | IdentityManager::selfSign (IdentityCertificate& cert) |
Jeff Thompson | e7e069b | 2013-09-27 15:48:48 -0700 | [diff] [blame] | 265 | { |
Alexander Afanasyev | bf1a67a | 2014-01-05 23:36:13 -0800 | [diff] [blame] | 266 | SignatureSha256WithRsa signature; |
| 267 | signature.setKeyLocator(cert.getName().getPrefix(cert.getName().size()-1)); // implicit conversion should take care |
Jeff Thompson | e7e069b | 2013-09-27 15:48:48 -0700 | [diff] [blame] | 268 | |
Alexander Afanasyev | bf1a67a | 2014-01-05 23:36:13 -0800 | [diff] [blame] | 269 | // For temporary usage, we support RSA + SHA256 only, but will support more. |
| 270 | signature.setValue(tpm().sign(cert, signature, cert.getPublicKeyName(), DIGEST_ALGORITHM_SHA256)); |
| 271 | cert.setSignature(signature); |
Jeff Thompson | e7e069b | 2013-09-27 15:48:48 -0700 | [diff] [blame] | 272 | } |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 273 | |
Jeff Thompson | 418b05a | 2013-10-22 17:48:54 -0700 | [diff] [blame] | 274 | Name |
| 275 | IdentityManager::getKeyNameFromCertificatePrefix(const Name & certificatePrefix) |
| 276 | { |
| 277 | Name result; |
| 278 | |
| 279 | string keyString("KEY"); |
| 280 | int i = 0; |
| 281 | for(; i < certificatePrefix.size(); i++) { |
| 282 | if (certificatePrefix.get(i).toEscapedString() == keyString) |
| 283 | break; |
| 284 | } |
| 285 | |
| 286 | if (i >= certificatePrefix.size()) |
Alexander Afanasyev | bf1a67a | 2014-01-05 23:36:13 -0800 | [diff] [blame] | 287 | throw Error("Identity Certificate Prefix does not have a KEY component"); |
Jeff Thompson | 418b05a | 2013-10-22 17:48:54 -0700 | [diff] [blame] | 288 | |
| 289 | result.append(certificatePrefix.getSubName(0, i)); |
| 290 | result.append(certificatePrefix.getSubName(i + 1, certificatePrefix.size()-i-1)); |
| 291 | |
| 292 | return result; |
| 293 | } |
| 294 | |
Jeff Thompson | 4147191 | 2013-09-12 16:21:50 -0700 | [diff] [blame] | 295 | } |