blob: b779adca73aabfd4984e8e6fe1533653f1f4672e [file] [log] [blame]
Yingdi Yuad56f1c2013-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 Yu38e329b2013-10-10 22:11:35 -070012#include "exception.h"
Yingdi Yu38e329b2013-10-10 22:11:35 -070013#include <ndn.cxx/fields/signature-sha256-with-rsa.h>
Yingdi Yuad56f1c2013-10-10 17:27:54 -070014
Yingdi Yuad56f1c2013-10-10 17:27:54 -070015using namespace ndn;
Yingdi Yu5ff62102013-10-13 17:24:50 -070016using namespace std;
Yingdi Yuad56f1c2013-10-10 17:27:54 -070017
Yingdi Yu5ff62102013-10-13 17:24:50 -070018ProfileData::ProfileData(const Name& identity,
19 const Profile& profile)
Yingdi Yuad56f1c2013-10-10 17:27:54 -070020 : Data()
Yingdi Yu5ff62102013-10-13 17:24:50 -070021 , m_identity(identity)
22 , m_profile(profile)
Yingdi Yuad56f1c2013-10-10 17:27:54 -070023{
Yingdi Yu5ff62102013-10-13 17:24:50 -070024 Name dataName = identity;
25 TimeInterval ti = time::NowUnixTimestamp();
26 ostringstream oss;
27 oss << ti.total_seconds();
28
29 dataName.append("PROFILE").append(oss.str());
30 setName(dataName);
31 Ptr<Blob> profileBlob = profile.toDerBlob();
32 setContent(Content(profileBlob->buf(), profileBlob->size()));
Yingdi Yuad56f1c2013-10-10 17:27:54 -070033}
34
Yingdi Yu5ff62102013-10-13 17:24:50 -070035ProfileData::ProfileData(const ProfileData& profileData)
Yingdi Yuad56f1c2013-10-10 17:27:54 -070036 : Data()
Yingdi Yu5ff62102013-10-13 17:24:50 -070037 , m_identity(profileData.m_identity)
38 , m_profile(profileData.m_profile)
Yingdi Yuad56f1c2013-10-10 17:27:54 -070039{
Yingdi Yu5ff62102013-10-13 17:24:50 -070040 Ptr<const signature::Sha256WithRsa> dataSig = boost::dynamic_pointer_cast<const signature::Sha256WithRsa>(profileData.getSignature());
41 Ptr<signature::Sha256WithRsa> newSig = Ptr<signature::Sha256WithRsa>::Create();
42
43 Ptr<SignedBlob> newSignedBlob = NULL;
44 if(profileData.getSignedBlob() != NULL)
45 newSignedBlob = Ptr<SignedBlob>(new SignedBlob(*profileData.getSignedBlob()));
46
47 newSig->setKeyLocator(dataSig->getKeyLocator());
48 newSig->setPublisherKeyDigest(dataSig->getPublisherKeyDigest());
49 newSig->setSignatureBits(dataSig->getSignatureBits());
50
51 setName(profileData.getName());
52 setSignature(newSig);
53 setContent(profileData.getContent());
54 setSignedBlob(newSignedBlob);
Yingdi Yuad56f1c2013-10-10 17:27:54 -070055}
Yingdi Yu5ff62102013-10-13 17:24:50 -070056
57ProfileData::ProfileData(const Data& data)
Yingdi Yu38e329b2013-10-10 22:11:35 -070058 : Data()
Yingdi Yuad56f1c2013-10-10 17:27:54 -070059{
Yingdi Yu38e329b2013-10-10 22:11:35 -070060 const Name& dataName = data.getName();
61 name::Component appFlag(string("PROFILE"));
62
63 int profileIndex = -1;
64 for(int i = 0; i < dataName.size(); i++)
65 {
66 if(0 == dataName.get(i).compare(appFlag))
67 {
68 profileIndex = i;
69 break;
70 }
71 }
72
Yingdi Yu5ff62102013-10-13 17:24:50 -070073 if(profileIndex < 0)
Yingdi Yu38e329b2013-10-10 22:11:35 -070074 throw LnException("No PROFILE component in data name!");
Yingdi Yu5ff62102013-10-13 17:24:50 -070075
76 m_identity = dataName.getSubName(0, profileIndex);
77
Yingdi Yu38e329b2013-10-10 22:11:35 -070078 Ptr<const signature::Sha256WithRsa> dataSig = boost::dynamic_pointer_cast<const signature::Sha256WithRsa>(data.getSignature());
79 Ptr<signature::Sha256WithRsa> newSig = Ptr<signature::Sha256WithRsa>::Create();
Yingdi Yu5ff62102013-10-13 17:24:50 -070080
Yingdi Yu38e329b2013-10-10 22:11:35 -070081 Ptr<SignedBlob> newSignedBlob = NULL;
82 if(data.getSignedBlob() != NULL)
83 newSignedBlob = Ptr<SignedBlob>(new SignedBlob(*data.getSignedBlob()));
84
85 newSig->setKeyLocator(dataSig->getKeyLocator());
86 newSig->setPublisherKeyDigest(dataSig->getPublisherKeyDigest());
87 newSig->setSignatureBits(dataSig->getSignatureBits());
88
89 setName(data.getName());
90 setSignature(newSig);
91 setContent(data.getContent());
92 setSignedBlob(newSignedBlob);
93
Yingdi Yu5ff62102013-10-13 17:24:50 -070094 m_profile = *Profile::fromDerBlob(data.content());
Yingdi Yuad56f1c2013-10-10 17:27:54 -070095}