blob: b6084ab92a1e8f72fcb85e9eaf132c8173467a39 [file] [log] [blame]
Yingdi Yu42f66462013-10-31 17:38:22 -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 "chronos-invitation.h"
Yingdi Yued8cfc42013-11-01 17:37:51 -070012
Yingdi Yu6df61252014-01-21 11:05:11 -080013#include <ndn-cpp-dev/security/identity-certificate.hpp>
14#include <ndn-cpp-dev/security/signature-sha256-with-rsa.hpp>
15
Yingdi Yu42f66462013-10-31 17:38:22 -070016#include "logging.h"
17
18using namespace std;
19using namespace ndn;
20
21INIT_LOGGER("ChronosInvitation");
22
Yingdi Yu6df61252014-01-21 11:05:11 -080023
24const size_t ChronosInvitation::NAME_SIZE_MIN = 8;
25const size_t ChronosInvitation::INVITEE_START = 4;
26const ssize_t SIGNATURE = -1;
27const ssize_t KEY_LOCATOR = -2;
28const ssize_t INVITER_PREFIX = -3;
29const ssize_t CHATROOM = -4;
30const Name ChronosInvitation::INVITATION_PREFIX("/ndn/broadcast/chronos/chat-invitation");
31
32
33ChronosInvitation::ChronosInvitation(const Name& originalInterestName)
Yingdi Yu42372442013-11-06 18:43:31 -080034 : m_interestName(originalInterestName)
Yingdi Yu42f66462013-10-31 17:38:22 -070035{
Yingdi Yu42f66462013-10-31 17:38:22 -070036
Yingdi Yu6df61252014-01-21 11:05:11 -080037 size_t nameSize = originalInterestName.size();
Yingdi Yu42f66462013-10-31 17:38:22 -070038
Yingdi Yu6df61252014-01-21 11:05:11 -080039 if(nameSize < NAME_SIZE_MIN)
40 throw Error("Wrong ChronosInvitation Name: Wrong length");
Yingdi Yued8cfc42013-11-01 17:37:51 -070041
Yingdi Yu6df61252014-01-21 11:05:11 -080042 if(!INVITATION_PREFIX.isPrefixOf(originalInterestName))
43 throw Error("Wrong ChronosInvitation Name: Wrong invitation prefix");
Yingdi Yu42f66462013-10-31 17:38:22 -070044
Yingdi Yu6df61252014-01-21 11:05:11 -080045 //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
71ChronosInvitation::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 Yu42f66462013-10-31 17:38:22 -070091}
92
93ChronosInvitation::ChronosInvitation(const ChronosInvitation& invitation)
94 : m_interestName(invitation.m_interestName)
Yingdi Yu6df61252014-01-21 11:05:11 -080095 , m_signedBlob(invitation.m_signedBlob)
Yingdi Yu42f66462013-10-31 17:38:22 -070096 , m_inviteeNameSpace(invitation.m_inviteeNameSpace)
97 , m_chatroom(invitation.m_chatroom)
Yingdi Yu6df61252014-01-21 11:05:11 -080098 , m_inviterRoutingPrefix(invitation.m_inviterRoutingPrefix)
Yingdi Yu42f66462013-10-31 17:38:22 -070099 , m_inviterCertificateName(invitation.m_inviterCertificateName)
Yingdi Yu6df61252014-01-21 11:05:11 -0800100 , m_signature(invitation.m_signature)
Yingdi Yu42f66462013-10-31 17:38:22 -0700101 , m_inviterNameSpace(invitation.m_inviterNameSpace)
Yingdi Yu6df61252014-01-21 11:05:11 -0800102 , m_isSigned(invitation.m_isSigned)
Yingdi Yu42f66462013-10-31 17:38:22 -0700103{}
Yingdi Yu6df61252014-01-21 11:05:11 -0800104
105void
106ChronosInvitation::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}