blob: 9a8087b533058a78b4723856b758176407da9c06 [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;
Yingdi Yub4be64a2013-10-13 17:24:50 -070029
Yingdi Yu7989eb22013-10-31 17:38:22 -070030 dataName.append("PROFILE").appendVersion();
Yingdi Yub4be64a2013-10-13 17:24:50 -070031 setName(dataName);
32 Ptr<Blob> profileBlob = profile.toDerBlob();
33 setContent(Content(profileBlob->buf(), profileBlob->size()));
Yingdi Yu4390ce52013-10-10 17:27:54 -070034}
35
Yingdi Yub4be64a2013-10-13 17:24:50 -070036ProfileData::ProfileData(const ProfileData& profileData)
Yingdi Yu4390ce52013-10-10 17:27:54 -070037 : Data()
Yingdi Yub4be64a2013-10-13 17:24:50 -070038 , m_identity(profileData.m_identity)
39 , m_profile(profileData.m_profile)
Yingdi Yu4390ce52013-10-10 17:27:54 -070040{
Yingdi Yub4be64a2013-10-13 17:24:50 -070041 Ptr<const signature::Sha256WithRsa> dataSig = boost::dynamic_pointer_cast<const signature::Sha256WithRsa>(profileData.getSignature());
42 Ptr<signature::Sha256WithRsa> newSig = Ptr<signature::Sha256WithRsa>::Create();
43
44 Ptr<SignedBlob> newSignedBlob = NULL;
45 if(profileData.getSignedBlob() != NULL)
46 newSignedBlob = Ptr<SignedBlob>(new SignedBlob(*profileData.getSignedBlob()));
47
48 newSig->setKeyLocator(dataSig->getKeyLocator());
49 newSig->setPublisherKeyDigest(dataSig->getPublisherKeyDigest());
50 newSig->setSignatureBits(dataSig->getSignatureBits());
51
52 setName(profileData.getName());
53 setSignature(newSig);
54 setContent(profileData.getContent());
55 setSignedBlob(newSignedBlob);
Yingdi Yu4390ce52013-10-10 17:27:54 -070056}
Yingdi Yub4be64a2013-10-13 17:24:50 -070057
58ProfileData::ProfileData(const Data& data)
Yingdi Yu7af8b512013-10-10 22:11:35 -070059 : Data()
Yingdi Yu4390ce52013-10-10 17:27:54 -070060{
Yingdi Yu92e8e482013-10-17 21:13:03 -070061 // _LOG_DEBUG("ProfileData constructor");
Yingdi Yu7af8b512013-10-10 22:11:35 -070062 const Name& dataName = data.getName();
63 name::Component appFlag(string("PROFILE"));
64
65 int profileIndex = -1;
66 for(int i = 0; i < dataName.size(); i++)
67 {
68 if(0 == dataName.get(i).compare(appFlag))
69 {
70 profileIndex = i;
71 break;
72 }
73 }
74
Yingdi Yub4be64a2013-10-13 17:24:50 -070075 if(profileIndex < 0)
Yingdi Yu7af8b512013-10-10 22:11:35 -070076 throw LnException("No PROFILE component in data name!");
Yingdi Yub4be64a2013-10-13 17:24:50 -070077
78 m_identity = dataName.getSubName(0, profileIndex);
79
Yingdi Yu7af8b512013-10-10 22:11:35 -070080 Ptr<const signature::Sha256WithRsa> dataSig = boost::dynamic_pointer_cast<const signature::Sha256WithRsa>(data.getSignature());
81 Ptr<signature::Sha256WithRsa> newSig = Ptr<signature::Sha256WithRsa>::Create();
Yingdi Yub4be64a2013-10-13 17:24:50 -070082
Yingdi Yu7af8b512013-10-10 22:11:35 -070083 Ptr<SignedBlob> newSignedBlob = NULL;
84 if(data.getSignedBlob() != NULL)
85 newSignedBlob = Ptr<SignedBlob>(new SignedBlob(*data.getSignedBlob()));
86
87 newSig->setKeyLocator(dataSig->getKeyLocator());
88 newSig->setPublisherKeyDigest(dataSig->getPublisherKeyDigest());
89 newSig->setSignatureBits(dataSig->getSignatureBits());
90
91 setName(data.getName());
92 setSignature(newSig);
93 setContent(data.getContent());
94 setSignedBlob(newSignedBlob);
Yingdi Yu92e8e482013-10-17 21:13:03 -070095 // _LOG_DEBUG("Decode Profile");
Yingdi Yub4be64a2013-10-13 17:24:50 -070096 m_profile = *Profile::fromDerBlob(data.content());
Yingdi Yu92e8e482013-10-17 21:13:03 -070097 // _LOG_DEBUG("Profile Decoded");
Yingdi Yu4390ce52013-10-10 17:27:54 -070098}