blob: 0ebd2e369d60cfe967be8c2a1ff8b6522c910621 [file] [log] [blame]
Yingdi Yu0b82a4e2013-10-18 11:29:25 -07001/* -*- 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
24using namespace ndn;
25using namespace ndn::security;
26
27ContactManager::ContactManager(Ptr<ContactStorage> contactStorage)
28 : m_contactStorage(contactStorage)
29{
30
31 m_wrapper = Ptr<Wrapper>(new Wrapper(setKeychain()));
32}
33
34ContactManager::~ContactManager()
35{
36}
37
38Ptr<Keychain>
39ContactManager::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}