blob: e839f0faa24f57e7c5dd9aebbf29b0ee94eeb5a8 [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 Yu2e3199c2013-11-06 18:42:34 -080022ProfileData::ProfileData(const Profile& profile)
Yingdi Yu4390ce52013-10-10 17:27:54 -070023 : Data()
Yingdi Yu2e3199c2013-11-06 18:42:34 -080024 , m_identity(profile.getIdentityName())
Yingdi Yub4be64a2013-10-13 17:24:50 -070025 , m_profile(profile)
Yingdi Yu4390ce52013-10-10 17:27:54 -070026{
Yingdi Yu2e3199c2013-11-06 18:42:34 -080027 Name dataName = m_identity;
Yingdi Yub4be64a2013-10-13 17:24:50 -070028
Yingdi Yu7989eb22013-10-31 17:38:22 -070029 dataName.append("PROFILE").appendVersion();
Yingdi Yub4be64a2013-10-13 17:24:50 -070030 setName(dataName);
31 Ptr<Blob> profileBlob = profile.toDerBlob();
32 setContent(Content(profileBlob->buf(), profileBlob->size()));
Yingdi Yu4390ce52013-10-10 17:27:54 -070033}
34
Yingdi Yub4be64a2013-10-13 17:24:50 -070035ProfileData::ProfileData(const ProfileData& profileData)
Yingdi Yu4390ce52013-10-10 17:27:54 -070036 : Data()
Yingdi Yub4be64a2013-10-13 17:24:50 -070037 , m_identity(profileData.m_identity)
38 , m_profile(profileData.m_profile)
Yingdi Yu4390ce52013-10-10 17:27:54 -070039{
Yingdi Yub4be64a2013-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 Yu4390ce52013-10-10 17:27:54 -070055}
Yingdi Yub4be64a2013-10-13 17:24:50 -070056
57ProfileData::ProfileData(const Data& data)
Yingdi Yu7af8b512013-10-10 22:11:35 -070058 : Data()
Yingdi Yu4390ce52013-10-10 17:27:54 -070059{
Yingdi Yu7af8b512013-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 Yub4be64a2013-10-13 17:24:50 -070073 if(profileIndex < 0)
Yingdi Yu7af8b512013-10-10 22:11:35 -070074 throw LnException("No PROFILE component in data name!");
Yingdi Yub4be64a2013-10-13 17:24:50 -070075
76 m_identity = dataName.getSubName(0, profileIndex);
77
Yingdi Yu7af8b512013-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 Yub4be64a2013-10-13 17:24:50 -070080
Yingdi Yu7af8b512013-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);
Yingdi Yue35bdb82013-11-07 11:32:40 -080093
Yingdi Yub4be64a2013-10-13 17:24:50 -070094 m_profile = *Profile::fromDerBlob(data.content());
Yingdi Yue35bdb82013-11-07 11:32:40 -080095
Yingdi Yu4390ce52013-10-10 17:27:54 -070096}