blob: ef067f9406ed8643d5316e6fbd4a941afc63c50b [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 Yu68aced92013-10-17 21:13:03 -070014#include "logging.h"
15
Yingdi Yuad56f1c2013-10-10 17:27:54 -070016
Yingdi Yuad56f1c2013-10-10 17:27:54 -070017using namespace ndn;
Yingdi Yu5ff62102013-10-13 17:24:50 -070018using namespace std;
Yingdi Yuad56f1c2013-10-10 17:27:54 -070019
Yingdi Yu68aced92013-10-17 21:13:03 -070020INIT_LOGGER("ProfileData");
21
Yingdi Yue9ea5c92013-11-06 18:42:34 -080022ProfileData::ProfileData(const Profile& profile)
Yingdi Yuad56f1c2013-10-10 17:27:54 -070023 : Data()
Yingdi Yue9ea5c92013-11-06 18:42:34 -080024 , m_identity(profile.getIdentityName())
Yingdi Yu5ff62102013-10-13 17:24:50 -070025 , m_profile(profile)
Yingdi Yuad56f1c2013-10-10 17:27:54 -070026{
Yingdi Yue9ea5c92013-11-06 18:42:34 -080027 Name dataName = m_identity;
Yingdi Yu5ff62102013-10-13 17:24:50 -070028
Yingdi Yu42f66462013-10-31 17:38:22 -070029 dataName.append("PROFILE").appendVersion();
Yingdi Yu5ff62102013-10-13 17:24:50 -070030 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 Yu68aced92013-10-17 21:13:03 -070060 // _LOG_DEBUG("ProfileData constructor");
Yingdi Yu38e329b2013-10-10 22:11:35 -070061 const Name& dataName = data.getName();
62 name::Component appFlag(string("PROFILE"));
63
64 int profileIndex = -1;
65 for(int i = 0; i < dataName.size(); i++)
66 {
67 if(0 == dataName.get(i).compare(appFlag))
68 {
69 profileIndex = i;
70 break;
71 }
72 }
73
Yingdi Yu5ff62102013-10-13 17:24:50 -070074 if(profileIndex < 0)
Yingdi Yu38e329b2013-10-10 22:11:35 -070075 throw LnException("No PROFILE component in data name!");
Yingdi Yu5ff62102013-10-13 17:24:50 -070076
77 m_identity = dataName.getSubName(0, profileIndex);
78
Yingdi Yu38e329b2013-10-10 22:11:35 -070079 Ptr<const signature::Sha256WithRsa> dataSig = boost::dynamic_pointer_cast<const signature::Sha256WithRsa>(data.getSignature());
80 Ptr<signature::Sha256WithRsa> newSig = Ptr<signature::Sha256WithRsa>::Create();
Yingdi Yu5ff62102013-10-13 17:24:50 -070081
Yingdi Yu38e329b2013-10-10 22:11:35 -070082 Ptr<SignedBlob> newSignedBlob = NULL;
83 if(data.getSignedBlob() != NULL)
84 newSignedBlob = Ptr<SignedBlob>(new SignedBlob(*data.getSignedBlob()));
85
86 newSig->setKeyLocator(dataSig->getKeyLocator());
87 newSig->setPublisherKeyDigest(dataSig->getPublisherKeyDigest());
88 newSig->setSignatureBits(dataSig->getSignatureBits());
89
90 setName(data.getName());
91 setSignature(newSig);
92 setContent(data.getContent());
93 setSignedBlob(newSignedBlob);
Yingdi Yu68aced92013-10-17 21:13:03 -070094 // _LOG_DEBUG("Decode Profile");
Yingdi Yu5ff62102013-10-13 17:24:50 -070095 m_profile = *Profile::fromDerBlob(data.content());
Yingdi Yu68aced92013-10-17 21:13:03 -070096 // _LOG_DEBUG("Profile Decoded");
Yingdi Yuad56f1c2013-10-10 17:27:54 -070097}