blob: e6446cd52d938dc1877c6f6b4d0f3fdd5014be2c [file] [log] [blame]
Jeff Thompson41471912013-09-12 16:21:50 -07001/**
Jeff Thompson7687dc02013-09-13 11:54:07 -07002 * Copyright (C) 2013 Regents of the University of California.
Jeff Thompson06e787d2013-09-12 19:00:55 -07003 * @author: Yingdi Yu <yingdi@cs.ucla.edu>
Jeff Thompson7687dc02013-09-13 11:54:07 -07004 * @author: Jeff Thompson <jefft0@remap.ucla.edu>
Jeff Thompson41471912013-09-12 16:21:50 -07005 * See COPYING for copyright and distribution information.
6 */
7
Jeff Thompsone7e069b2013-09-27 15:48:48 -07008#include "../../util/logging.hpp"
Jeff Thompson86e1d752013-09-17 17:22:38 -07009#include "../../sha256-with-rsa-signature.hpp"
Jeff Thompsone7e069b2013-09-27 15:48:48 -070010#include "../security-exception.hpp"
Jeff Thompson41471912013-09-12 16:21:50 -070011#include "identity-manager.hpp"
12
Jeff Thompson9296f0c2013-09-23 18:10:27 -070013using namespace std;
14using namespace ndn::ptr_lib;
Jeff Thompson41471912013-09-12 16:21:50 -070015
Jeff Thompson9296f0c2013-09-23 18:10:27 -070016namespace ndn {
Jeff Thompson86e1d752013-09-17 17:22:38 -070017
Jeff Thompsone7e069b2013-09-27 15:48:48 -070018Name
19IdentityManager::createIdentity(const Name& identityName)
20{
21 if (!identityStorage_->doesIdentityExist(identityName)) {
22 _LOG_DEBUG("Create Identity");
23 identityStorage_->addIdentity(identityName);
24
25 _LOG_DEBUG("Create Default RSA key pair");
26 Name keyName = generateRSAKeyPairAsDefault(identityName, true);
27
28 _LOG_DEBUG("Create self-signed certificate");
29 shared_ptr<Certificate> selfCert = selfSign(keyName);
30
31 _LOG_DEBUG("Add self-signed certificate as default");
32 addCertificateAsDefault(*selfCert);
33
34 return keyName;
35 }
36 else
37 throw SecurityException("Identity has already been created!");
38}
39
40Name
41IdentityManager::generateRSAKeyPair(const Name& identityName, bool isKsk, int keySize)
42{
43 Name keyName = generateKeyPair(identityName, isKsk, KEY_TYPE_RSA, keySize);
44 _LOG_DEBUG("OK2");
45 return keyName;
46}
47
48Name
49IdentityManager::generateRSAKeyPairAsDefault(const Name& identityName, bool isKsk, int keySize)
50{
51 Name keyName = generateKeyPair(identityName, isKsk, KEY_TYPE_RSA, keySize);
52
53 identityStorage_->setDefaultKeyNameForIdentity(keyName, identityName);
54
55 return keyName;
56}
57
58void
59IdentityManager::setDefaultCertificateForKey(const Name& certificateName)
60{
61 Name keyName = identityStorage_->getKeyNameForCertificate(certificateName);
62
63 if (!identityStorage_->doesKeyExist(keyName))
64 throw SecurityException("No corresponding Key record for certificaite!");
65
66 identityStorage_->setDefaultCertificateNameForKey (keyName, certificateName);
67}
68
69void
70IdentityManager::addCertificateAsIdentityDefault(const Certificate& certificate)
71{
72 identityStorage_->addCertificate(certificate);
73
74 Name keyName = identityStorage_->getKeyNameForCertificate(certificate.getName());
75
76 setDefaultKeyForIdentity(keyName);
77 setDefaultCertificateForKey(certificate.getName());
78}
79
Jeff Thompson0050abe2013-09-17 12:50:25 -070080void
Jeff Thompson86e1d752013-09-17 17:22:38 -070081IdentityManager::signByCertificate(Data &data, const Name &certificateName, WireFormat& wireFormat)
Jeff Thompson41471912013-09-12 16:21:50 -070082{
Jeff Thompson9296f0c2013-09-23 18:10:27 -070083 Name keyName = identityStorage_->getKeyNameForCertificate(certificateName);
84
85 shared_ptr<PublicKey> publicKey = privateKeyStorage_->getPublicKey(keyName);
Jeff Thompson86e1d752013-09-17 17:22:38 -070086
87 // For temporary usage, we support RSA + SHA256 only, but will support more.
88 data.setSignature(Sha256WithRsaSignature());
89 // Get a pointer to the clone which Data made.
90 Sha256WithRsaSignature *signature = dynamic_cast<Sha256WithRsaSignature*>(data.getSignature());
91 DigestAlgorithm digestAlgorithm = DIGEST_ALGORITHM_SHA256;
92
93 signature->getKeyLocator().setType(ndn_KeyLocatorType_KEYNAME);
94 signature->getKeyLocator().setKeyName(certificateName);
Jeff Thompson9296f0c2013-09-23 18:10:27 -070095 // Omit the certificate digest.
96 signature->getKeyLocator().setKeyNameType((ndn_KeyNameType)-1);
Jeff Thompson86e1d752013-09-17 17:22:38 -070097 // Ignore witness and leave the digestAlgorithm as the default.
Jeff Thompson9296f0c2013-09-23 18:10:27 -070098 signature->getPublisherPublicKeyDigest().setPublisherPublicKeyDigest(publicKey->getDigest());
Jeff Thompson41471912013-09-12 16:21:50 -070099
Jeff Thompson86e1d752013-09-17 17:22:38 -0700100 // Encode once to get the signed portion.
101 SignedBlob encoding = data.wireEncode(wireFormat);
102
103 signature->setSignature
Jeff Thompson9296f0c2013-09-23 18:10:27 -0700104 (privateKeyStorage_->sign(encoding.signedBuf(), encoding.signedSize(), keyName, digestAlgorithm));
Jeff Thompson86e1d752013-09-17 17:22:38 -0700105
106 // Encode again to include the signature.
107 data.wireEncode(wireFormat);
Jeff Thompson41471912013-09-12 16:21:50 -0700108}
109
Jeff Thompsone7e069b2013-09-27 15:48:48 -0700110Name
111IdentityManager::generateKeyPair (const Name& identityName, bool isKsk, KeyType keyType, int keySize)
112{
113 _LOG_DEBUG("Get new key ID");
114 Name keyName = identityStorage_->getNewKeyName(identityName, isKsk);
115
116 _LOG_DEBUG("Generate key pair in private storage");
117 privateKeyStorage_->generateKeyPair(keyName.toUri(), keyType, keySize);
118
119 _LOG_DEBUG("Create a key record in public storage");
120 shared_ptr<PublicKey> publicKey = privateKeyStorage_->getPublicKey(keyName);
121 identityStorage_->addKey(keyName, keyType, publicKey->getKeyDer());
122 _LOG_DEBUG("OK");
123 return keyName;
124}
125
126shared_ptr<Certificate>
127IdentityManager::selfSign (const Name& keyName)
128{
129#if 1
130 throw std::runtime_error("MemoryIdentityStorage::getNewKeyName not implemented");
131#endif
132}
133
Jeff Thompson41471912013-09-12 16:21:50 -0700134}