Yingdi Yu | 0b82a4e | 2013-10-18 11:29:25 -0700 | [diff] [blame^] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2013, Regents of the University of California |
| 4 | * Yingdi Yu |
| 5 | * |
| 6 | * BSD license, See the LICENSE file for more information |
| 7 | * |
| 8 | * Author: Yingdi Yu <yingdi@cs.ucla.edu> |
| 9 | */ |
| 10 | |
| 11 | #include "contact-manager.h" |
| 12 | |
| 13 | #include <ndn.cxx/wrapper/wrapper.h> |
| 14 | #include <ndn.cxx/security/keychain.h> |
| 15 | #include <ndn.cxx/security/identity/basic-identity-storage.h> |
| 16 | #include <ndn.cxx/security/identity/osx-privatekey-storage.h> |
| 17 | #include <ndn.cxx/security/policy/simple-policy-manager.h> |
| 18 | #include <ndn.cxx/security/policy/identity-policy-rule.h> |
| 19 | #include <ndn.cxx/security/cache/ttl-certificate-cache.h> |
| 20 | #include <ndn.cxx/security/encryption/basic-encryption-manager.h> |
| 21 | |
| 22 | #include <fstream> |
| 23 | |
| 24 | using namespace ndn; |
| 25 | using namespace ndn::security; |
| 26 | |
| 27 | ContactManager::ContactManager(Ptr<ContactStorage> contactStorage) |
| 28 | : m_contactStorage(contactStorage) |
| 29 | { |
| 30 | |
| 31 | m_wrapper = Ptr<Wrapper>(new Wrapper(setKeychain())); |
| 32 | } |
| 33 | |
| 34 | ContactManager::~ContactManager() |
| 35 | { |
| 36 | } |
| 37 | |
| 38 | Ptr<Keychain> |
| 39 | ContactManager::setKeychain() |
| 40 | { |
| 41 | Ptr<OSXPrivatekeyStorage> privateStorage = Ptr<OSXPrivatekeyStorage>::Create(); |
| 42 | Ptr<IdentityManager> identityManager = Ptr<IdentityManager>(new IdentityManager(Ptr<BasicIdentityStorage>::Create(), privateStorage)); |
| 43 | Ptr<TTLCertificateCache> certificateCache = Ptr<TTLCertificateCache>(new TTLCertificateCache()); |
| 44 | Ptr<SimplePolicyManager> policyManager = Ptr<SimplePolicyManager>(new SimplePolicyManager(10, certificateCache)); |
| 45 | Ptr<EncryptionManager> encryptionManager = Ptr<EncryptionManager>(new BasicEncryptionManager(privateStorage, "/tmp/encryption.db")); |
| 46 | Ptr<Keychain> keychain = Ptr<Keychain>(new Keychain(identityManager, policyManager, encryptionManager)); |
| 47 | |
| 48 | policyManager->addVerificationPolicyRule(Ptr<IdentityPolicyRule>(new IdentityPolicyRule("^([^<PROFILE-CERT>]*)<PROFILE-CERT>", |
| 49 | "^([^<KEY>]*)<KEY>(<>*<KSK-.*>)<ID-CERT>", |
| 50 | "==", "\\1", "\\1\\2", false))); |
| 51 | policyManager->addVerificationPolicyRule(Ptr<IdentityPolicyRule>(new IdentityPolicyRule("^([^<KEY>]*)<KEY>(<>*)<KSK-.*><ID-CERT>", |
| 52 | "^([^<KEY>]*)<KEY><DSK-.*><ID-CERT>", |
| 53 | ">", "\\1\\2", "\\1", false))); |
| 54 | policyManager->addVerificationPolicyRule(Ptr<IdentityPolicyRule>(new IdentityPolicyRule("^([^<KEY>]*)<KEY><DSK-.*><ID-CERT>", |
| 55 | "^([^<KEY>]*)<KEY>(<>*)<KSK-.*><ID-CERT>", |
| 56 | "==", "\\1", "\\1\\2", false))); |
| 57 | |
| 58 | ifstream is ("trust-anchor.data", ios::binary); |
| 59 | is.seekg (0, ios::end); |
| 60 | ifstream::pos_type size = is.tellg(); |
| 61 | char * memblock = new char [size]; |
| 62 | is.seekg (0, ios::beg); |
| 63 | is.read (memblock, size); |
| 64 | is.close(); |
| 65 | |
| 66 | Ptr<Blob> readBlob = Ptr<Blob>(new Blob(memblock, size)); |
| 67 | Ptr<Data> readData = Data::decodeFromWire (readBlob); |
| 68 | Ptr<IdentityCertificate> anchor = Ptr<IdentityCertificate>(new IdentityCertificate(*readData)); |
| 69 | policyManager->addTrustAnchor(anchor); |
| 70 | |
| 71 | delete memblock; |
| 72 | |
| 73 | return keychain; |
| 74 | } |