Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -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 "chronos-invitation.h" |
Yingdi Yu | ed8cfc4 | 2013-11-01 17:37:51 -0700 | [diff] [blame] | 12 | |
Yingdi Yu | 6df6125 | 2014-01-21 11:05:11 -0800 | [diff] [blame^] | 13 | #include <ndn-cpp-dev/security/identity-certificate.hpp> |
| 14 | #include <ndn-cpp-dev/security/signature-sha256-with-rsa.hpp> |
| 15 | |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 16 | #include "logging.h" |
| 17 | |
| 18 | using namespace std; |
| 19 | using namespace ndn; |
| 20 | |
| 21 | INIT_LOGGER("ChronosInvitation"); |
| 22 | |
Yingdi Yu | 6df6125 | 2014-01-21 11:05:11 -0800 | [diff] [blame^] | 23 | |
| 24 | const size_t ChronosInvitation::NAME_SIZE_MIN = 8; |
| 25 | const size_t ChronosInvitation::INVITEE_START = 4; |
| 26 | const ssize_t SIGNATURE = -1; |
| 27 | const ssize_t KEY_LOCATOR = -2; |
| 28 | const ssize_t INVITER_PREFIX = -3; |
| 29 | const ssize_t CHATROOM = -4; |
| 30 | const Name ChronosInvitation::INVITATION_PREFIX("/ndn/broadcast/chronos/chat-invitation"); |
| 31 | |
| 32 | |
| 33 | ChronosInvitation::ChronosInvitation(const Name& originalInterestName) |
Yingdi Yu | 4237244 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 34 | : m_interestName(originalInterestName) |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 35 | { |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 36 | |
Yingdi Yu | 6df6125 | 2014-01-21 11:05:11 -0800 | [diff] [blame^] | 37 | size_t nameSize = originalInterestName.size(); |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 38 | |
Yingdi Yu | 6df6125 | 2014-01-21 11:05:11 -0800 | [diff] [blame^] | 39 | if(nameSize < NAME_SIZE_MIN) |
| 40 | throw Error("Wrong ChronosInvitation Name: Wrong length"); |
Yingdi Yu | ed8cfc4 | 2013-11-01 17:37:51 -0700 | [diff] [blame] | 41 | |
Yingdi Yu | 6df6125 | 2014-01-21 11:05:11 -0800 | [diff] [blame^] | 42 | if(!INVITATION_PREFIX.isPrefixOf(originalInterestName)) |
| 43 | throw Error("Wrong ChronosInvitation Name: Wrong invitation prefix"); |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 44 | |
Yingdi Yu | 6df6125 | 2014-01-21 11:05:11 -0800 | [diff] [blame^] | 45 | //hack! should be more efficient. |
| 46 | Name signedName = originalInterestName.getPrefix(-1); |
| 47 | m_signedBlob = Buffer(signedName.wireEncode().value(), signedName.wireEncode().value_size()); |
| 48 | |
| 49 | Block signatureBlock(originalInterestName.get(SIGNATURE).getValue().buf(), |
| 50 | originalInterestName.get(SIGNATURE).getValue().size()); |
| 51 | Block signatureInfo(originalInterestName.get(KEY_LOCATOR).getValue().buf(), |
| 52 | originalInterestName.get(KEY_LOCATOR).getValue().size()); |
| 53 | m_signature = Signature(Signature(signatureInfo, signatureBlock)); |
| 54 | |
| 55 | SignatureSha256WithRsa sha256RsaSig(m_signature); |
| 56 | m_inviterCertificateName = sha256RsaSig.getKeyLocator().getName(); |
| 57 | |
| 58 | m_inviterNameSpace = IdentityCertificate::certificateNameToPublicKeyName(m_inviterCertificateName).getPrefix(-1); |
| 59 | |
| 60 | m_inviterRoutingPrefix.wireDecode(Block(originalInterestName.get(INVITER_PREFIX).getValue().buf(), |
| 61 | originalInterestName.get(INVITER_PREFIX).getValue().size())); |
| 62 | |
| 63 | m_chatroom.wireDecode(Block(originalInterestName.get(CHATROOM).getValue().buf(), |
| 64 | originalInterestName.get(CHATROOM).getValue().size())); |
| 65 | |
| 66 | m_inviteeNameSpace = originalInterestName.getSubName(INVITEE_START, nameSize - NAME_SIZE_MIN); |
| 67 | |
| 68 | m_isSigned = true; |
| 69 | } |
| 70 | |
| 71 | ChronosInvitation::ChronosInvitation(const Name &inviteeNameSpace, |
| 72 | const Name &chatroom, |
| 73 | const Name &inviterRoutingPrefix, |
| 74 | const Name &inviterCertificateName) |
| 75 | : m_inviteeNameSpace(inviteeNameSpace) |
| 76 | , m_chatroom(chatroom) |
| 77 | , m_inviterRoutingPrefix(inviterRoutingPrefix) |
| 78 | , m_inviterCertificateName(inviterCertificateName) |
| 79 | { |
| 80 | //implicit conversion, we do not keep version number in KeyLocator; |
| 81 | SignatureSha256WithRsa sha256RsaSig; |
| 82 | sha256RsaSig.setKeyLocator(KeyLocator(inviterCertificateName.getPrefix(-1))); |
| 83 | m_signature.setInfo(sha256RsaSig.getInfo()); |
| 84 | m_inviterNameSpace = IdentityCertificate::certificateNameToPublicKeyName(m_inviterCertificateName).getPrefix(-1); |
| 85 | |
| 86 | m_interestName = INVITATION_PREFIX; |
| 87 | m_interestName.append(inviteeNameSpace).append(chatroom.wireEncode()).append(inviterRoutingPrefix.wireEncode()).append(m_signature.getInfo()); |
| 88 | |
| 89 | m_signedBlob = Buffer(m_interestName.wireEncode().value(), m_interestName.wireEncode().value_size()); |
| 90 | m_isSigned = false; |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | ChronosInvitation::ChronosInvitation(const ChronosInvitation& invitation) |
| 94 | : m_interestName(invitation.m_interestName) |
Yingdi Yu | 6df6125 | 2014-01-21 11:05:11 -0800 | [diff] [blame^] | 95 | , m_signedBlob(invitation.m_signedBlob) |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 96 | , m_inviteeNameSpace(invitation.m_inviteeNameSpace) |
| 97 | , m_chatroom(invitation.m_chatroom) |
Yingdi Yu | 6df6125 | 2014-01-21 11:05:11 -0800 | [diff] [blame^] | 98 | , m_inviterRoutingPrefix(invitation.m_inviterRoutingPrefix) |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 99 | , m_inviterCertificateName(invitation.m_inviterCertificateName) |
Yingdi Yu | 6df6125 | 2014-01-21 11:05:11 -0800 | [diff] [blame^] | 100 | , m_signature(invitation.m_signature) |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 101 | , m_inviterNameSpace(invitation.m_inviterNameSpace) |
Yingdi Yu | 6df6125 | 2014-01-21 11:05:11 -0800 | [diff] [blame^] | 102 | , m_isSigned(invitation.m_isSigned) |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 103 | {} |
Yingdi Yu | 6df6125 | 2014-01-21 11:05:11 -0800 | [diff] [blame^] | 104 | |
| 105 | void |
| 106 | ChronosInvitation::setSignatureValue(const ndn::Block &signatureValue) |
| 107 | { |
| 108 | if(m_isSigned) |
| 109 | return; |
| 110 | |
| 111 | m_interestName.append(signatureValue); |
| 112 | m_signature.setValue(signatureValue); |
| 113 | m_isSigned = true; |
| 114 | } |