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 | ad56f1c | 2013-10-10 17:27:54 -0700 | [diff] [blame] | 14 | |
Yingdi Yu | ad56f1c | 2013-10-10 17:27:54 -0700 | [diff] [blame] | 15 | using namespace ndn; |
Yingdi Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame^] | 16 | using namespace std; |
Yingdi Yu | ad56f1c | 2013-10-10 17:27:54 -0700 | [diff] [blame] | 17 | |
Yingdi Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame^] | 18 | ProfileData::ProfileData(const Name& identity, |
| 19 | const Profile& profile) |
Yingdi Yu | ad56f1c | 2013-10-10 17:27:54 -0700 | [diff] [blame] | 20 | : Data() |
Yingdi Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame^] | 21 | , m_identity(identity) |
| 22 | , m_profile(profile) |
Yingdi Yu | ad56f1c | 2013-10-10 17:27:54 -0700 | [diff] [blame] | 23 | { |
Yingdi Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame^] | 24 | 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 Yu | ad56f1c | 2013-10-10 17:27:54 -0700 | [diff] [blame] | 33 | } |
| 34 | |
Yingdi Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame^] | 35 | ProfileData::ProfileData(const ProfileData& profileData) |
Yingdi Yu | ad56f1c | 2013-10-10 17:27:54 -0700 | [diff] [blame] | 36 | : Data() |
Yingdi Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame^] | 37 | , m_identity(profileData.m_identity) |
| 38 | , m_profile(profileData.m_profile) |
Yingdi Yu | ad56f1c | 2013-10-10 17:27:54 -0700 | [diff] [blame] | 39 | { |
Yingdi Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame^] | 40 | 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 Yu | ad56f1c | 2013-10-10 17:27:54 -0700 | [diff] [blame] | 55 | } |
Yingdi Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame^] | 56 | |
| 57 | ProfileData::ProfileData(const Data& data) |
Yingdi Yu | 38e329b | 2013-10-10 22:11:35 -0700 | [diff] [blame] | 58 | : Data() |
Yingdi Yu | ad56f1c | 2013-10-10 17:27:54 -0700 | [diff] [blame] | 59 | { |
Yingdi Yu | 38e329b | 2013-10-10 22:11:35 -0700 | [diff] [blame] | 60 | 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 Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame^] | 73 | if(profileIndex < 0) |
Yingdi Yu | 38e329b | 2013-10-10 22:11:35 -0700 | [diff] [blame] | 74 | throw LnException("No PROFILE component in data name!"); |
Yingdi Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame^] | 75 | |
| 76 | m_identity = dataName.getSubName(0, profileIndex); |
| 77 | |
Yingdi Yu | 38e329b | 2013-10-10 22:11:35 -0700 | [diff] [blame] | 78 | Ptr<const signature::Sha256WithRsa> dataSig = boost::dynamic_pointer_cast<const signature::Sha256WithRsa>(data.getSignature()); |
| 79 | Ptr<signature::Sha256WithRsa> newSig = Ptr<signature::Sha256WithRsa>::Create(); |
Yingdi Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame^] | 80 | |
Yingdi Yu | 38e329b | 2013-10-10 22:11:35 -0700 | [diff] [blame] | 81 | 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 Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame^] | 94 | m_profile = *Profile::fromDerBlob(data.content()); |
Yingdi Yu | ad56f1c | 2013-10-10 17:27:54 -0700 | [diff] [blame] | 95 | } |