Yingdi Yu | ad56f1c | 2013-10-10 17:27:54 -0700 | [diff] [blame] | 1 | /* -*- 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 Yu | 38e329b | 2013-10-10 22:11:35 -0700 | [diff] [blame] | 12 | #include "exception.h" |
Yingdi Yu | 38e329b | 2013-10-10 22:11:35 -0700 | [diff] [blame] | 13 | #include <ndn.cxx/fields/signature-sha256-with-rsa.h> |
Yingdi Yu | 68aced9 | 2013-10-17 21:13:03 -0700 | [diff] [blame^] | 14 | #include "logging.h" |
| 15 | |
Yingdi Yu | ad56f1c | 2013-10-10 17:27:54 -0700 | [diff] [blame] | 16 | |
Yingdi Yu | ad56f1c | 2013-10-10 17:27:54 -0700 | [diff] [blame] | 17 | using namespace ndn; |
Yingdi Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 18 | using namespace std; |
Yingdi Yu | ad56f1c | 2013-10-10 17:27:54 -0700 | [diff] [blame] | 19 | |
Yingdi Yu | 68aced9 | 2013-10-17 21:13:03 -0700 | [diff] [blame^] | 20 | INIT_LOGGER("ProfileData"); |
| 21 | |
Yingdi Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 22 | ProfileData::ProfileData(const Name& identity, |
| 23 | const Profile& profile) |
Yingdi Yu | ad56f1c | 2013-10-10 17:27:54 -0700 | [diff] [blame] | 24 | : Data() |
Yingdi Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 25 | , m_identity(identity) |
| 26 | , m_profile(profile) |
Yingdi Yu | ad56f1c | 2013-10-10 17:27:54 -0700 | [diff] [blame] | 27 | { |
Yingdi Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 28 | 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 Yu | ad56f1c | 2013-10-10 17:27:54 -0700 | [diff] [blame] | 37 | } |
| 38 | |
Yingdi Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 39 | ProfileData::ProfileData(const ProfileData& profileData) |
Yingdi Yu | ad56f1c | 2013-10-10 17:27:54 -0700 | [diff] [blame] | 40 | : Data() |
Yingdi Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 41 | , m_identity(profileData.m_identity) |
| 42 | , m_profile(profileData.m_profile) |
Yingdi Yu | ad56f1c | 2013-10-10 17:27:54 -0700 | [diff] [blame] | 43 | { |
Yingdi Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 44 | 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 Yu | ad56f1c | 2013-10-10 17:27:54 -0700 | [diff] [blame] | 59 | } |
Yingdi Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 60 | |
| 61 | ProfileData::ProfileData(const Data& data) |
Yingdi Yu | 38e329b | 2013-10-10 22:11:35 -0700 | [diff] [blame] | 62 | : Data() |
Yingdi Yu | ad56f1c | 2013-10-10 17:27:54 -0700 | [diff] [blame] | 63 | { |
Yingdi Yu | 68aced9 | 2013-10-17 21:13:03 -0700 | [diff] [blame^] | 64 | // _LOG_DEBUG("ProfileData constructor"); |
Yingdi Yu | 38e329b | 2013-10-10 22:11:35 -0700 | [diff] [blame] | 65 | 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 Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 78 | if(profileIndex < 0) |
Yingdi Yu | 38e329b | 2013-10-10 22:11:35 -0700 | [diff] [blame] | 79 | throw LnException("No PROFILE component in data name!"); |
Yingdi Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 80 | |
| 81 | m_identity = dataName.getSubName(0, profileIndex); |
| 82 | |
Yingdi Yu | 38e329b | 2013-10-10 22:11:35 -0700 | [diff] [blame] | 83 | Ptr<const signature::Sha256WithRsa> dataSig = boost::dynamic_pointer_cast<const signature::Sha256WithRsa>(data.getSignature()); |
| 84 | Ptr<signature::Sha256WithRsa> newSig = Ptr<signature::Sha256WithRsa>::Create(); |
Yingdi Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 85 | |
Yingdi Yu | 38e329b | 2013-10-10 22:11:35 -0700 | [diff] [blame] | 86 | 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 Yu | 68aced9 | 2013-10-17 21:13:03 -0700 | [diff] [blame^] | 98 | // _LOG_DEBUG("Decode Profile"); |
Yingdi Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 99 | m_profile = *Profile::fromDerBlob(data.content()); |
Yingdi Yu | 68aced9 | 2013-10-17 21:13:03 -0700 | [diff] [blame^] | 100 | // _LOG_DEBUG("Profile Decoded"); |
Yingdi Yu | ad56f1c | 2013-10-10 17:27:54 -0700 | [diff] [blame] | 101 | } |