blob: f057a6de0ce8bea96a64dfe00319c0f981db3455 [file] [log] [blame]
Yingdi Yu4390ce52013-10-10 17:27:54 -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 "profile-data.h"
Yingdi Yu7af8b512013-10-10 22:11:35 -070012#include "exception.h"
Yingdi Yu7af8b512013-10-10 22:11:35 -070013#include <ndn.cxx/fields/signature-sha256-with-rsa.h>
Yingdi Yu92e8e482013-10-17 21:13:03 -070014#include "logging.h"
15
Yingdi Yu4390ce52013-10-10 17:27:54 -070016
Yingdi Yu4390ce52013-10-10 17:27:54 -070017using namespace ndn;
Yingdi Yub4be64a2013-10-13 17:24:50 -070018using namespace std;
Yingdi Yu4390ce52013-10-10 17:27:54 -070019
Yingdi Yu92e8e482013-10-17 21:13:03 -070020INIT_LOGGER("ProfileData");
21
Yingdi Yub4be64a2013-10-13 17:24:50 -070022ProfileData::ProfileData(const Name& identity,
23 const Profile& profile)
Yingdi Yu4390ce52013-10-10 17:27:54 -070024 : Data()
Yingdi Yub4be64a2013-10-13 17:24:50 -070025 , m_identity(identity)
26 , m_profile(profile)
Yingdi Yu4390ce52013-10-10 17:27:54 -070027{
Yingdi Yub4be64a2013-10-13 17:24:50 -070028 Name dataName = identity;
29 TimeInterval ti = time::NowUnixTimestamp();
30 ostringstream oss;
31 oss << ti.total_seconds();
32
33 dataName.append("PROFILE").append(oss.str());
34 setName(dataName);
35 Ptr<Blob> profileBlob = profile.toDerBlob();
36 setContent(Content(profileBlob->buf(), profileBlob->size()));
Yingdi Yu4390ce52013-10-10 17:27:54 -070037}
38
Yingdi Yub4be64a2013-10-13 17:24:50 -070039ProfileData::ProfileData(const ProfileData& profileData)
Yingdi Yu4390ce52013-10-10 17:27:54 -070040 : Data()
Yingdi Yub4be64a2013-10-13 17:24:50 -070041 , m_identity(profileData.m_identity)
42 , m_profile(profileData.m_profile)
Yingdi Yu4390ce52013-10-10 17:27:54 -070043{
Yingdi Yub4be64a2013-10-13 17:24:50 -070044 Ptr<const signature::Sha256WithRsa> dataSig = boost::dynamic_pointer_cast<const signature::Sha256WithRsa>(profileData.getSignature());
45 Ptr<signature::Sha256WithRsa> newSig = Ptr<signature::Sha256WithRsa>::Create();
46
47 Ptr<SignedBlob> newSignedBlob = NULL;
48 if(profileData.getSignedBlob() != NULL)
49 newSignedBlob = Ptr<SignedBlob>(new SignedBlob(*profileData.getSignedBlob()));
50
51 newSig->setKeyLocator(dataSig->getKeyLocator());
52 newSig->setPublisherKeyDigest(dataSig->getPublisherKeyDigest());
53 newSig->setSignatureBits(dataSig->getSignatureBits());
54
55 setName(profileData.getName());
56 setSignature(newSig);
57 setContent(profileData.getContent());
58 setSignedBlob(newSignedBlob);
Yingdi Yu4390ce52013-10-10 17:27:54 -070059}
Yingdi Yub4be64a2013-10-13 17:24:50 -070060
61ProfileData::ProfileData(const Data& data)
Yingdi Yu7af8b512013-10-10 22:11:35 -070062 : Data()
Yingdi Yu4390ce52013-10-10 17:27:54 -070063{
Yingdi Yu92e8e482013-10-17 21:13:03 -070064 // _LOG_DEBUG("ProfileData constructor");
Yingdi Yu7af8b512013-10-10 22:11:35 -070065 const Name& dataName = data.getName();
66 name::Component appFlag(string("PROFILE"));
67
68 int profileIndex = -1;
69 for(int i = 0; i < dataName.size(); i++)
70 {
71 if(0 == dataName.get(i).compare(appFlag))
72 {
73 profileIndex = i;
74 break;
75 }
76 }
77
Yingdi Yub4be64a2013-10-13 17:24:50 -070078 if(profileIndex < 0)
Yingdi Yu7af8b512013-10-10 22:11:35 -070079 throw LnException("No PROFILE component in data name!");
Yingdi Yub4be64a2013-10-13 17:24:50 -070080
81 m_identity = dataName.getSubName(0, profileIndex);
82
Yingdi Yu7af8b512013-10-10 22:11:35 -070083 Ptr<const signature::Sha256WithRsa> dataSig = boost::dynamic_pointer_cast<const signature::Sha256WithRsa>(data.getSignature());
84 Ptr<signature::Sha256WithRsa> newSig = Ptr<signature::Sha256WithRsa>::Create();
Yingdi Yub4be64a2013-10-13 17:24:50 -070085
Yingdi Yu7af8b512013-10-10 22:11:35 -070086 Ptr<SignedBlob> newSignedBlob = NULL;
87 if(data.getSignedBlob() != NULL)
88 newSignedBlob = Ptr<SignedBlob>(new SignedBlob(*data.getSignedBlob()));
89
90 newSig->setKeyLocator(dataSig->getKeyLocator());
91 newSig->setPublisherKeyDigest(dataSig->getPublisherKeyDigest());
92 newSig->setSignatureBits(dataSig->getSignatureBits());
93
94 setName(data.getName());
95 setSignature(newSig);
96 setContent(data.getContent());
97 setSignedBlob(newSignedBlob);
Yingdi Yu92e8e482013-10-17 21:13:03 -070098 // _LOG_DEBUG("Decode Profile");
Yingdi Yub4be64a2013-10-13 17:24:50 -070099 m_profile = *Profile::fromDerBlob(data.content());
Yingdi Yu92e8e482013-10-17 21:13:03 -0700100 // _LOG_DEBUG("Profile Decoded");
Yingdi Yu4390ce52013-10-10 17:27:54 -0700101}