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 | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 19 | #include <ndn-cpp/key.hpp> |
Jeff Thompson | 25b4e61 | 2013-10-10 16:03:24 -0700 | [diff] [blame] | 20 | #include <ndn-cpp/sha256-with-rsa-signature.hpp> |
| 21 | #include <ndn-cpp/security/security-exception.hpp> |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 22 | #include "../../util/logging.hpp" |
Jeff Thompson | db68689 | 2013-10-18 17:16:31 -0700 | [diff] [blame] | 23 | #include "../../c/util/time.h" |
Jeff Thompson | 25b4e61 | 2013-10-10 16:03:24 -0700 | [diff] [blame] | 24 | #include <ndn-cpp/security/identity/identity-manager.hpp> |
Jeff Thompson | 4147191 | 2013-09-12 16:21:50 -0700 | [diff] [blame] | 25 | |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 26 | INIT_LOGGER("ndn.security.IdentityManager") |
| 27 | |
Jeff Thompson | 9296f0c | 2013-09-23 18:10:27 -0700 | [diff] [blame] | 28 | using namespace std; |
| 29 | using namespace ndn::ptr_lib; |
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 | |
Jeff Thompson | e7e069b | 2013-09-27 15:48:48 -0700 | [diff] [blame] | 33 | Name |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 34 | IdentityManager::createIdentity(const Name& identityName) |
Jeff Thompson | e7e069b | 2013-09-27 15:48:48 -0700 | [diff] [blame] | 35 | { |
| 36 | if (!identityStorage_->doesIdentityExist(identityName)) { |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 37 | _LOG_DEBUG("Create Identity"); |
| 38 | identityStorage_->addIdentity(identityName); |
| 39 | |
| 40 | _LOG_DEBUG("Create Default RSA key pair"); |
| 41 | Name keyName = generateRSAKeyPairAsDefault(identityName, true); |
Jeff Thompson | e7e069b | 2013-09-27 15:48:48 -0700 | [diff] [blame] | 42 | |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 43 | _LOG_DEBUG("Create self-signed certificate"); |
| 44 | shared_ptr<IdentityCertificate> selfCert = selfSign(keyName); |
| 45 | |
| 46 | _LOG_DEBUG("Add self-signed certificate as default"); |
Jeff Thompson | e7e069b | 2013-09-27 15:48:48 -0700 | [diff] [blame] | 47 | |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 48 | addCertificateAsDefault(*selfCert); |
| 49 | |
| 50 | return keyName; |
Jeff Thompson | e7e069b | 2013-09-27 15:48:48 -0700 | [diff] [blame] | 51 | } |
| 52 | else |
| 53 | throw SecurityException("Identity has already been created!"); |
| 54 | } |
| 55 | |
| 56 | Name |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 57 | IdentityManager::generateKeyPair(const Name& identityName, bool isKsk, KeyType keyType, int keySize) |
| 58 | { |
| 59 | _LOG_DEBUG("Get new key ID"); |
| 60 | Name keyName = identityStorage_->getNewKeyName(identityName, isKsk); |
| 61 | |
| 62 | _LOG_DEBUG("Generate key pair in private storage"); |
| 63 | privateKeyStorage_->generateKeyPair(keyName.toUri(), keyType, keySize); |
| 64 | |
| 65 | _LOG_DEBUG("Create a key record in public storage"); |
| 66 | shared_ptr<PublicKey> pubKey = privateKeyStorage_->getPublicKey(keyName.toUri()); |
| 67 | identityStorage_->addKey(keyName, keyType, pubKey->getKeyDer()); |
Jeff Thompson | 418b05a | 2013-10-22 17:48:54 -0700 | [diff] [blame] | 68 | |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 69 | return keyName; |
| 70 | } |
| 71 | |
| 72 | Name |
Jeff Thompson | e7e069b | 2013-09-27 15:48:48 -0700 | [diff] [blame] | 73 | IdentityManager::generateRSAKeyPair(const Name& identityName, bool isKsk, int keySize) |
| 74 | { |
| 75 | Name keyName = generateKeyPair(identityName, isKsk, KEY_TYPE_RSA, keySize); |
Jeff Thompson | 418b05a | 2013-10-22 17:48:54 -0700 | [diff] [blame] | 76 | |
Jeff Thompson | e7e069b | 2013-09-27 15:48:48 -0700 | [diff] [blame] | 77 | return keyName; |
| 78 | } |
| 79 | |
| 80 | Name |
| 81 | IdentityManager::generateRSAKeyPairAsDefault(const Name& identityName, bool isKsk, int keySize) |
| 82 | { |
| 83 | Name keyName = generateKeyPair(identityName, isKsk, KEY_TYPE_RSA, keySize); |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 84 | |
Jeff Thompson | e7e069b | 2013-09-27 15:48:48 -0700 | [diff] [blame] | 85 | identityStorage_->setDefaultKeyNameForIdentity(keyName, identityName); |
| 86 | |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 87 | return keyName; |
| 88 | } |
| 89 | |
| 90 | Name |
Jeff Thompson | 418b05a | 2013-10-22 17:48:54 -0700 | [diff] [blame] | 91 | IdentityManager::createIdentityCertificate(const Name& certificatePrefix, |
| 92 | const Name& signerCertificateName, |
| 93 | const MillisecondsSince1970& notBefore, |
| 94 | const MillisecondsSince1970& notAfter) |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 95 | { |
Jeff Thompson | 418b05a | 2013-10-22 17:48:54 -0700 | [diff] [blame] | 96 | Name keyName = getKeyNameFromCertificatePrefix(certificatePrefix); |
| 97 | |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 98 | Blob keyBlob = identityStorage_->getKey(keyName); |
| 99 | shared_ptr<PublicKey> publicKey = PublicKey::fromDer(keyBlob); |
| 100 | |
| 101 | shared_ptr<IdentityCertificate> certificate = createIdentityCertificate |
Jeff Thompson | 418b05a | 2013-10-22 17:48:54 -0700 | [diff] [blame] | 102 | (certificatePrefix, *publicKey, signerCertificateName, notBefore, notAfter); |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 103 | |
| 104 | identityStorage_->addCertificate(*certificate); |
| 105 | |
| 106 | return certificate->getName(); |
| 107 | } |
| 108 | |
| 109 | ptr_lib::shared_ptr<IdentityCertificate> |
Jeff Thompson | 418b05a | 2013-10-22 17:48:54 -0700 | [diff] [blame] | 110 | IdentityManager::createIdentityCertificate(const Name& certificatePrefix, |
| 111 | const PublicKey& publicKey, |
| 112 | const Name& signerCertificateName, |
| 113 | const MillisecondsSince1970& notBefore, |
| 114 | const MillisecondsSince1970& notAfter) |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 115 | { |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 116 | shared_ptr<IdentityCertificate> certificate(new IdentityCertificate()); |
Jeff Thompson | 418b05a | 2013-10-22 17:48:54 -0700 | [diff] [blame] | 117 | Name keyName = getKeyNameFromCertificatePrefix(certificatePrefix); |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 118 | |
Jeff Thompson | 418b05a | 2013-10-22 17:48:54 -0700 | [diff] [blame] | 119 | Name certificateName = certificatePrefix; |
Jeff Thompson | db68689 | 2013-10-18 17:16:31 -0700 | [diff] [blame] | 120 | MillisecondsSince1970 ti = ::ndn_getNowMilliseconds(); |
| 121 | // Get the number of seconds. |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 122 | ostringstream oss; |
Jeff Thompson | db68689 | 2013-10-18 17:16:31 -0700 | [diff] [blame] | 123 | oss << floor(ti / 1000.0); |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 124 | |
Jeff Thompson | 418b05a | 2013-10-22 17:48:54 -0700 | [diff] [blame] | 125 | certificateName.append("ID-CERT").append(oss.str()); |
| 126 | |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 127 | certificate->setName(certificateName); |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 128 | certificate->setNotBefore(notBefore); |
| 129 | certificate->setNotAfter(notAfter); |
| 130 | certificate->setPublicKeyInfo(publicKey); |
Jeff Thompson | db68689 | 2013-10-18 17:16:31 -0700 | [diff] [blame] | 131 | certificate->addSubjectDescription(CertificateSubjectDescription("2.5.4.41", keyName.toUri())); |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 132 | certificate->encode(); |
| 133 | |
| 134 | shared_ptr<Sha256WithRsaSignature> sha256Sig(new Sha256WithRsaSignature()); |
| 135 | |
| 136 | KeyLocator keyLocator; |
Jeff Thompson | db68689 | 2013-10-18 17:16:31 -0700 | [diff] [blame] | 137 | keyLocator.setType(ndn_KeyLocatorType_KEYNAME); |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 138 | keyLocator.setKeyName(signerCertificateName); |
| 139 | |
| 140 | sha256Sig->setKeyLocator(keyLocator); |
Jeff Thompson | d63baba | 2013-10-18 17:47:58 -0700 | [diff] [blame] | 141 | sha256Sig->getPublisherPublicKeyDigest().setPublisherPublicKeyDigest(publicKey.getDigest()); |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 142 | |
Jeff Thompson | db68689 | 2013-10-18 17:16:31 -0700 | [diff] [blame] | 143 | certificate->setSignature(*sha256Sig); |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 144 | |
Jeff Thompson | db68689 | 2013-10-18 17:16:31 -0700 | [diff] [blame] | 145 | SignedBlob unsignedData = certificate->wireEncode(); |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 146 | |
Jeff Thompson | 418b05a | 2013-10-22 17:48:54 -0700 | [diff] [blame] | 147 | shared_ptr<IdentityCertificate> signerCertificate = getCertificate(signerCertificateName); |
| 148 | Name signerkeyName = signerCertificate->getPublicKeyName(); |
| 149 | |
| 150 | Blob sigBits = privateKeyStorage_->sign(unsignedData, signerkeyName); |
| 151 | |
Jeff Thompson | ba82c16 | 2013-10-18 17:21:23 -0700 | [diff] [blame] | 152 | sha256Sig->setSignature(sigBits); |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 153 | |
| 154 | return certificate; |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | void |
| 158 | IdentityManager::addCertificateAsDefault(const IdentityCertificate& certificate) |
| 159 | { |
| 160 | identityStorage_->addCertificate(certificate); |
| 161 | |
Jeff Thompson | 418b05a | 2013-10-22 17:48:54 -0700 | [diff] [blame] | 162 | setDefaultCertificateForKey(certificate); |
Jeff Thompson | e7e069b | 2013-09-27 15:48:48 -0700 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | void |
Jeff Thompson | 418b05a | 2013-10-22 17:48:54 -0700 | [diff] [blame] | 166 | IdentityManager::addCertificateAsIdentityDefault(const IdentityCertificate& certificate) |
Jeff Thompson | e7e069b | 2013-09-27 15:48:48 -0700 | [diff] [blame] | 167 | { |
Jeff Thompson | 418b05a | 2013-10-22 17:48:54 -0700 | [diff] [blame] | 168 | identityStorage_->addCertificate(certificate); |
| 169 | |
| 170 | Name keyName = certificate.getPublicKeyName(); |
| 171 | |
| 172 | setDefaultKeyForIdentity(keyName); |
| 173 | |
| 174 | setDefaultCertificateForKey(certificate); |
| 175 | } |
| 176 | |
| 177 | void |
| 178 | IdentityManager::setDefaultCertificateForKey(const IdentityCertificate& certificate) |
| 179 | { |
| 180 | Name keyName = certificate.getPublicKeyName(); |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 181 | |
| 182 | if(!identityStorage_->doesKeyExist(keyName)) |
Jeff Thompson | 418b05a | 2013-10-22 17:48:54 -0700 | [diff] [blame] | 183 | throw SecurityException("No corresponding Key record for certificate!"); |
Jeff Thompson | e7e069b | 2013-09-27 15:48:48 -0700 | [diff] [blame] | 184 | |
Jeff Thompson | 418b05a | 2013-10-22 17:48:54 -0700 | [diff] [blame] | 185 | identityStorage_->setDefaultCertificateNameForKey(keyName, certificate.getName()); |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | ptr_lib::shared_ptr<Signature> |
Jeff Thompson | c01e178 | 2013-10-21 14:08:42 -0700 | [diff] [blame] | 189 | IdentityManager::signByCertificate(const uint8_t* buffer, size_t bufferLength, const Name& certificateName) |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 190 | { |
Jeff Thompson | 418b05a | 2013-10-22 17:48:54 -0700 | [diff] [blame] | 191 | shared_ptr<IdentityCertificate> certificate = getCertificate(certificateName); |
| 192 | Name keyName = certificate->getPublicKeyName(); |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 193 | shared_ptr<PublicKey> publicKey = privateKeyStorage_->getPublicKey(keyName.toUri()); |
| 194 | |
Jeff Thompson | c01e178 | 2013-10-21 14:08:42 -0700 | [diff] [blame] | 195 | Blob sigBits = privateKeyStorage_->sign(buffer, bufferLength, keyName.toUri()); |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 196 | |
| 197 | //For temporary usage, we support RSA + SHA256 only, but will support more. |
Jeff Thompson | ba82c16 | 2013-10-18 17:21:23 -0700 | [diff] [blame] | 198 | shared_ptr<Sha256WithRsaSignature> sha256Sig(new Sha256WithRsaSignature()); |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 199 | |
| 200 | KeyLocator keyLocator; |
Jeff Thompson | ba82c16 | 2013-10-18 17:21:23 -0700 | [diff] [blame] | 201 | keyLocator.setType(ndn_KeyLocatorType_KEYNAME); |
| 202 | keyLocator.setKeyName(certificateName); |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 203 | |
| 204 | sha256Sig->setKeyLocator(keyLocator); |
Jeff Thompson | d63baba | 2013-10-18 17:47:58 -0700 | [diff] [blame] | 205 | sha256Sig->getPublisherPublicKeyDigest().setPublisherPublicKeyDigest(publicKey->getDigest()); |
Jeff Thompson | ba82c16 | 2013-10-18 17:21:23 -0700 | [diff] [blame] | 206 | sha256Sig->setSignature(sigBits); |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 207 | |
| 208 | return sha256Sig; |
Jeff Thompson | e7e069b | 2013-09-27 15:48:48 -0700 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | void |
Jeff Thompson | 86e1d75 | 2013-09-17 17:22:38 -0700 | [diff] [blame] | 212 | IdentityManager::signByCertificate(Data &data, const Name &certificateName, WireFormat& wireFormat) |
Jeff Thompson | 4147191 | 2013-09-12 16:21:50 -0700 | [diff] [blame] | 213 | { |
Jeff Thompson | 418b05a | 2013-10-22 17:48:54 -0700 | [diff] [blame] | 214 | shared_ptr<IdentityCertificate> certificate = getCertificate(certificateName); |
| 215 | Name keyName = certificate->getPublicKeyName(); |
Jeff Thompson | 9296f0c | 2013-09-23 18:10:27 -0700 | [diff] [blame] | 216 | shared_ptr<PublicKey> publicKey = privateKeyStorage_->getPublicKey(keyName); |
Jeff Thompson | 86e1d75 | 2013-09-17 17:22:38 -0700 | [diff] [blame] | 217 | |
| 218 | // For temporary usage, we support RSA + SHA256 only, but will support more. |
| 219 | data.setSignature(Sha256WithRsaSignature()); |
| 220 | // Get a pointer to the clone which Data made. |
| 221 | Sha256WithRsaSignature *signature = dynamic_cast<Sha256WithRsaSignature*>(data.getSignature()); |
| 222 | DigestAlgorithm digestAlgorithm = DIGEST_ALGORITHM_SHA256; |
| 223 | |
| 224 | signature->getKeyLocator().setType(ndn_KeyLocatorType_KEYNAME); |
| 225 | signature->getKeyLocator().setKeyName(certificateName); |
Jeff Thompson | 9296f0c | 2013-09-23 18:10:27 -0700 | [diff] [blame] | 226 | // Omit the certificate digest. |
| 227 | signature->getKeyLocator().setKeyNameType((ndn_KeyNameType)-1); |
Jeff Thompson | 86e1d75 | 2013-09-17 17:22:38 -0700 | [diff] [blame] | 228 | // Ignore witness and leave the digestAlgorithm as the default. |
Jeff Thompson | 9296f0c | 2013-09-23 18:10:27 -0700 | [diff] [blame] | 229 | signature->getPublisherPublicKeyDigest().setPublisherPublicKeyDigest(publicKey->getDigest()); |
Jeff Thompson | 4147191 | 2013-09-12 16:21:50 -0700 | [diff] [blame] | 230 | |
Jeff Thompson | 86e1d75 | 2013-09-17 17:22:38 -0700 | [diff] [blame] | 231 | // Encode once to get the signed portion. |
| 232 | SignedBlob encoding = data.wireEncode(wireFormat); |
| 233 | |
| 234 | signature->setSignature |
Jeff Thompson | 9296f0c | 2013-09-23 18:10:27 -0700 | [diff] [blame] | 235 | (privateKeyStorage_->sign(encoding.signedBuf(), encoding.signedSize(), keyName, digestAlgorithm)); |
Jeff Thompson | 86e1d75 | 2013-09-17 17:22:38 -0700 | [diff] [blame] | 236 | |
| 237 | // Encode again to include the signature. |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 238 | data.wireEncode(wireFormat); |
Jeff Thompson | 4147191 | 2013-09-12 16:21:50 -0700 | [diff] [blame] | 239 | } |
| 240 | |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 241 | shared_ptr<IdentityCertificate> |
| 242 | IdentityManager::selfSign(const Name& keyName) |
Jeff Thompson | e7e069b | 2013-09-27 15:48:48 -0700 | [diff] [blame] | 243 | { |
Jeff Thompson | d63baba | 2013-10-18 17:47:58 -0700 | [diff] [blame] | 244 | shared_ptr<IdentityCertificate> certificate(new IdentityCertificate()); |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 245 | |
Jeff Thompson | 418b05a | 2013-10-22 17:48:54 -0700 | [diff] [blame] | 246 | Name certificateName = keyName.getSubName(0, keyName.size() - 1); |
| 247 | certificateName.append("KEY").append(keyName.get(keyName.size() - 1)).append("ID-CERT").append("0"); |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 248 | certificate->setName(certificateName); |
Jeff Thompson | e7e069b | 2013-09-27 15:48:48 -0700 | [diff] [blame] | 249 | |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 250 | Blob keyBlob = identityStorage_->getKey(keyName); |
| 251 | shared_ptr<PublicKey> publicKey = PublicKey::fromDer(keyBlob); |
Jeff Thompson | e7e069b | 2013-09-27 15:48:48 -0700 | [diff] [blame] | 252 | |
Jeff Thompson | d63baba | 2013-10-18 17:47:58 -0700 | [diff] [blame] | 253 | #if NDN_CPP_HAVE_GMTIME_SUPPORT |
| 254 | time_t nowSeconds = time(NULL); |
| 255 | struct tm current = *gmtime(&nowSeconds); |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 256 | current.tm_hour = 0; |
| 257 | current.tm_min = 0; |
| 258 | current.tm_sec = 0; |
Jeff Thompson | d63baba | 2013-10-18 17:47:58 -0700 | [diff] [blame] | 259 | MillisecondsSince1970 notBefore = timegm(¤t) * 1000.0; |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 260 | current.tm_year = current.tm_year + 20; |
Jeff Thompson | d63baba | 2013-10-18 17:47:58 -0700 | [diff] [blame] | 261 | MillisecondsSince1970 notAfter = timegm(¤t) * 1000.0; |
Jeff Thompson | e7e069b | 2013-09-27 15:48:48 -0700 | [diff] [blame] | 262 | |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 263 | certificate->setNotBefore(notBefore); |
| 264 | certificate->setNotAfter(notAfter); |
Jeff Thompson | d63baba | 2013-10-18 17:47:58 -0700 | [diff] [blame] | 265 | #else |
| 266 | // Don't really expect this to happen. |
| 267 | throw SecurityException("selfSign: Can't set certificate validity because time functions are not supported by the standard library."); |
| 268 | #endif |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 269 | certificate->setPublicKeyInfo(*publicKey); |
Jeff Thompson | d63baba | 2013-10-18 17:47:58 -0700 | [diff] [blame] | 270 | certificate->addSubjectDescription(CertificateSubjectDescription("2.5.4.41", keyName.toUri())); |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 271 | certificate->encode(); |
| 272 | |
Jeff Thompson | d63baba | 2013-10-18 17:47:58 -0700 | [diff] [blame] | 273 | shared_ptr<Sha256WithRsaSignature> sha256Sig(new Sha256WithRsaSignature()); |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 274 | |
| 275 | KeyLocator keyLocator; |
Jeff Thompson | d63baba | 2013-10-18 17:47:58 -0700 | [diff] [blame] | 276 | keyLocator.setType(ndn_KeyLocatorType_KEYNAME); |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 277 | keyLocator.setKeyName(certificateName); |
| 278 | |
| 279 | sha256Sig->setKeyLocator(keyLocator); |
Jeff Thompson | d63baba | 2013-10-18 17:47:58 -0700 | [diff] [blame] | 280 | sha256Sig->getPublisherPublicKeyDigest().setPublisherPublicKeyDigest(publicKey->getDigest()); |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 281 | |
Jeff Thompson | d63baba | 2013-10-18 17:47:58 -0700 | [diff] [blame] | 282 | certificate->setSignature(*sha256Sig); |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 283 | |
Jeff Thompson | d63baba | 2013-10-18 17:47:58 -0700 | [diff] [blame] | 284 | Blob unsignedData = certificate->wireEncode(); |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 285 | |
Jeff Thompson | d63baba | 2013-10-18 17:47:58 -0700 | [diff] [blame] | 286 | Blob sigBits = privateKeyStorage_->sign(unsignedData, keyName.toUri()); |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 287 | |
Jeff Thompson | d63baba | 2013-10-18 17:47:58 -0700 | [diff] [blame] | 288 | sha256Sig->setSignature(sigBits); |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 289 | |
| 290 | return certificate; |
Jeff Thompson | e7e069b | 2013-09-27 15:48:48 -0700 | [diff] [blame] | 291 | } |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 292 | |
Jeff Thompson | 418b05a | 2013-10-22 17:48:54 -0700 | [diff] [blame] | 293 | Name |
| 294 | IdentityManager::getKeyNameFromCertificatePrefix(const Name & certificatePrefix) |
| 295 | { |
| 296 | Name result; |
| 297 | |
| 298 | string keyString("KEY"); |
| 299 | int i = 0; |
| 300 | for(; i < certificatePrefix.size(); i++) { |
| 301 | if (certificatePrefix.get(i).toEscapedString() == keyString) |
| 302 | break; |
| 303 | } |
| 304 | |
| 305 | if (i >= certificatePrefix.size()) |
| 306 | throw SecurityException("Identity Certificate Prefix does not have a KEY component"); |
| 307 | |
| 308 | result.append(certificatePrefix.getSubName(0, i)); |
| 309 | result.append(certificatePrefix.getSubName(i + 1, certificatePrefix.size()-i-1)); |
| 310 | |
| 311 | return result; |
| 312 | } |
| 313 | |
Jeff Thompson | 4147191 | 2013-09-12 16:21:50 -0700 | [diff] [blame] | 314 | } |