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 | 7494261 | 2013-10-24 16:42:32 -0700 | [diff] [blame] | 190 | { |
| 191 | Name keyName = IdentityCertificate::certificateNameToPublicKeyName(certificateName); |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 192 | shared_ptr<PublicKey> publicKey = privateKeyStorage_->getPublicKey(keyName.toUri()); |
| 193 | |
Jeff Thompson | c01e178 | 2013-10-21 14:08:42 -0700 | [diff] [blame] | 194 | Blob sigBits = privateKeyStorage_->sign(buffer, bufferLength, keyName.toUri()); |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 195 | |
| 196 | //For temporary usage, we support RSA + SHA256 only, but will support more. |
Jeff Thompson | ba82c16 | 2013-10-18 17:21:23 -0700 | [diff] [blame] | 197 | shared_ptr<Sha256WithRsaSignature> sha256Sig(new Sha256WithRsaSignature()); |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 198 | |
| 199 | KeyLocator keyLocator; |
Jeff Thompson | ba82c16 | 2013-10-18 17:21:23 -0700 | [diff] [blame] | 200 | keyLocator.setType(ndn_KeyLocatorType_KEYNAME); |
| 201 | keyLocator.setKeyName(certificateName); |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 202 | |
| 203 | sha256Sig->setKeyLocator(keyLocator); |
Jeff Thompson | d63baba | 2013-10-18 17:47:58 -0700 | [diff] [blame] | 204 | sha256Sig->getPublisherPublicKeyDigest().setPublisherPublicKeyDigest(publicKey->getDigest()); |
Jeff Thompson | ba82c16 | 2013-10-18 17:21:23 -0700 | [diff] [blame] | 205 | sha256Sig->setSignature(sigBits); |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 206 | |
| 207 | return sha256Sig; |
Jeff Thompson | e7e069b | 2013-09-27 15:48:48 -0700 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | void |
Jeff Thompson | 86e1d75 | 2013-09-17 17:22:38 -0700 | [diff] [blame] | 211 | IdentityManager::signByCertificate(Data &data, const Name &certificateName, WireFormat& wireFormat) |
Jeff Thompson | 4147191 | 2013-09-12 16:21:50 -0700 | [diff] [blame] | 212 | { |
Jeff Thompson | 7494261 | 2013-10-24 16:42:32 -0700 | [diff] [blame] | 213 | Name keyName = IdentityCertificate::certificateNameToPublicKeyName(certificateName); |
Jeff Thompson | 9296f0c | 2013-09-23 18:10:27 -0700 | [diff] [blame] | 214 | shared_ptr<PublicKey> publicKey = privateKeyStorage_->getPublicKey(keyName); |
Jeff Thompson | 86e1d75 | 2013-09-17 17:22:38 -0700 | [diff] [blame] | 215 | |
| 216 | // For temporary usage, we support RSA + SHA256 only, but will support more. |
| 217 | data.setSignature(Sha256WithRsaSignature()); |
| 218 | // Get a pointer to the clone which Data made. |
| 219 | Sha256WithRsaSignature *signature = dynamic_cast<Sha256WithRsaSignature*>(data.getSignature()); |
| 220 | DigestAlgorithm digestAlgorithm = DIGEST_ALGORITHM_SHA256; |
| 221 | |
| 222 | signature->getKeyLocator().setType(ndn_KeyLocatorType_KEYNAME); |
| 223 | signature->getKeyLocator().setKeyName(certificateName); |
Jeff Thompson | 9296f0c | 2013-09-23 18:10:27 -0700 | [diff] [blame] | 224 | // Omit the certificate digest. |
| 225 | signature->getKeyLocator().setKeyNameType((ndn_KeyNameType)-1); |
Jeff Thompson | 86e1d75 | 2013-09-17 17:22:38 -0700 | [diff] [blame] | 226 | // Ignore witness and leave the digestAlgorithm as the default. |
Jeff Thompson | 9296f0c | 2013-09-23 18:10:27 -0700 | [diff] [blame] | 227 | signature->getPublisherPublicKeyDigest().setPublisherPublicKeyDigest(publicKey->getDigest()); |
Jeff Thompson | 4147191 | 2013-09-12 16:21:50 -0700 | [diff] [blame] | 228 | |
Jeff Thompson | 86e1d75 | 2013-09-17 17:22:38 -0700 | [diff] [blame] | 229 | // Encode once to get the signed portion. |
| 230 | SignedBlob encoding = data.wireEncode(wireFormat); |
| 231 | |
| 232 | signature->setSignature |
Jeff Thompson | 9296f0c | 2013-09-23 18:10:27 -0700 | [diff] [blame] | 233 | (privateKeyStorage_->sign(encoding.signedBuf(), encoding.signedSize(), keyName, digestAlgorithm)); |
Jeff Thompson | 86e1d75 | 2013-09-17 17:22:38 -0700 | [diff] [blame] | 234 | |
| 235 | // Encode again to include the signature. |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 236 | data.wireEncode(wireFormat); |
Jeff Thompson | 4147191 | 2013-09-12 16:21:50 -0700 | [diff] [blame] | 237 | } |
| 238 | |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 239 | shared_ptr<IdentityCertificate> |
| 240 | IdentityManager::selfSign(const Name& keyName) |
Jeff Thompson | e7e069b | 2013-09-27 15:48:48 -0700 | [diff] [blame] | 241 | { |
Jeff Thompson | d63baba | 2013-10-18 17:47:58 -0700 | [diff] [blame] | 242 | shared_ptr<IdentityCertificate> certificate(new IdentityCertificate()); |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 243 | |
Jeff Thompson | 418b05a | 2013-10-22 17:48:54 -0700 | [diff] [blame] | 244 | Name certificateName = keyName.getSubName(0, keyName.size() - 1); |
| 245 | 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] | 246 | certificate->setName(certificateName); |
Jeff Thompson | e7e069b | 2013-09-27 15:48:48 -0700 | [diff] [blame] | 247 | |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 248 | Blob keyBlob = identityStorage_->getKey(keyName); |
| 249 | shared_ptr<PublicKey> publicKey = PublicKey::fromDer(keyBlob); |
Jeff Thompson | e7e069b | 2013-09-27 15:48:48 -0700 | [diff] [blame] | 250 | |
Jeff Thompson | d63baba | 2013-10-18 17:47:58 -0700 | [diff] [blame] | 251 | #if NDN_CPP_HAVE_GMTIME_SUPPORT |
| 252 | time_t nowSeconds = time(NULL); |
| 253 | struct tm current = *gmtime(&nowSeconds); |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 254 | current.tm_hour = 0; |
| 255 | current.tm_min = 0; |
| 256 | current.tm_sec = 0; |
Jeff Thompson | d63baba | 2013-10-18 17:47:58 -0700 | [diff] [blame] | 257 | MillisecondsSince1970 notBefore = timegm(¤t) * 1000.0; |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 258 | current.tm_year = current.tm_year + 20; |
Jeff Thompson | d63baba | 2013-10-18 17:47:58 -0700 | [diff] [blame] | 259 | MillisecondsSince1970 notAfter = timegm(¤t) * 1000.0; |
Jeff Thompson | e7e069b | 2013-09-27 15:48:48 -0700 | [diff] [blame] | 260 | |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 261 | certificate->setNotBefore(notBefore); |
| 262 | certificate->setNotAfter(notAfter); |
Jeff Thompson | d63baba | 2013-10-18 17:47:58 -0700 | [diff] [blame] | 263 | #else |
| 264 | // Don't really expect this to happen. |
| 265 | throw SecurityException("selfSign: Can't set certificate validity because time functions are not supported by the standard library."); |
| 266 | #endif |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 267 | certificate->setPublicKeyInfo(*publicKey); |
Jeff Thompson | d63baba | 2013-10-18 17:47:58 -0700 | [diff] [blame] | 268 | certificate->addSubjectDescription(CertificateSubjectDescription("2.5.4.41", keyName.toUri())); |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 269 | certificate->encode(); |
| 270 | |
Jeff Thompson | d63baba | 2013-10-18 17:47:58 -0700 | [diff] [blame] | 271 | shared_ptr<Sha256WithRsaSignature> sha256Sig(new Sha256WithRsaSignature()); |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 272 | |
| 273 | KeyLocator keyLocator; |
Jeff Thompson | d63baba | 2013-10-18 17:47:58 -0700 | [diff] [blame] | 274 | keyLocator.setType(ndn_KeyLocatorType_KEYNAME); |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 275 | keyLocator.setKeyName(certificateName); |
| 276 | |
| 277 | sha256Sig->setKeyLocator(keyLocator); |
Jeff Thompson | d63baba | 2013-10-18 17:47:58 -0700 | [diff] [blame] | 278 | sha256Sig->getPublisherPublicKeyDigest().setPublisherPublicKeyDigest(publicKey->getDigest()); |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 279 | |
Jeff Thompson | d63baba | 2013-10-18 17:47:58 -0700 | [diff] [blame] | 280 | certificate->setSignature(*sha256Sig); |
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 | Blob unsignedData = certificate->wireEncode(); |
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 sigBits = privateKeyStorage_->sign(unsignedData, keyName.toUri()); |
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 | sha256Sig->setSignature(sigBits); |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 287 | |
| 288 | return certificate; |
Jeff Thompson | e7e069b | 2013-09-27 15:48:48 -0700 | [diff] [blame] | 289 | } |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 290 | |
Jeff Thompson | 418b05a | 2013-10-22 17:48:54 -0700 | [diff] [blame] | 291 | Name |
| 292 | IdentityManager::getKeyNameFromCertificatePrefix(const Name & certificatePrefix) |
| 293 | { |
| 294 | Name result; |
| 295 | |
| 296 | string keyString("KEY"); |
| 297 | int i = 0; |
| 298 | for(; i < certificatePrefix.size(); i++) { |
| 299 | if (certificatePrefix.get(i).toEscapedString() == keyString) |
| 300 | break; |
| 301 | } |
| 302 | |
| 303 | if (i >= certificatePrefix.size()) |
| 304 | throw SecurityException("Identity Certificate Prefix does not have a KEY component"); |
| 305 | |
| 306 | result.append(certificatePrefix.getSubName(0, i)); |
| 307 | result.append(certificatePrefix.getSubName(i + 1, certificatePrefix.size()-i-1)); |
| 308 | |
| 309 | return result; |
| 310 | } |
| 311 | |
Jeff Thompson | 4147191 | 2013-09-12 16:21:50 -0700 | [diff] [blame] | 312 | } |