OverSim
CryptoModule.cc
Go to the documentation of this file.
1 //
2 // Copyright (C) 2009 Institut fuer Telematik, Universitaet Karlsruhe (TH)
3 //
4 // This program is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU General Public License
6 // as published by the Free Software Foundation; either version 2
7 // of the License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 //
18 
25 #include <CommonMessages_m.h>
26 #include <OverlayAccess.h>
27 #include <GlobalStatisticsAccess.h>
28 #include <CryptoModule.h>
29 
30 using namespace std;
31 
33 
35 {
36  globalStatistics = NULL;
37  overlay = NULL;
38 }
39 
41 {
42 }
43 
45 {
46  globalStatistics = GlobalStatisticsAccess().get();
47  overlay = OverlayAccess().get(this);
48 
49  numSign = 0;
50 
51 // EV << "[CryptoModule::initialize() @ " << overlay->getThisNode().getIp()
52 // << " (" << overlay->getThisNode().getKey().toString(16) << ")]\n"
53 // << " Reading key from file " << par("keyFile").stdstringValue()
54 // << endl;
55 }
56 
58 {
59  // need to remove controlInfo before serializing
60  BaseRpcMessage *msgStripped = static_cast<BaseRpcMessage*>(msg->dup());
61 
62  if (msgStripped->getControlInfo() != NULL) {
63  delete msgStripped->removeControlInfo();
64  }
65 
66  // serialize message (needed to calculate message hash)
67  commBuffer.reset();
68  commBuffer.packObject(msgStripped);
69  delete msgStripped;
70 
71  // calculate hash and signature
72  // commBuffer.getBuffer(), commBuffer.getBufferLength()
73 
74  // ...
75 
76  // append public key and signature
77  msg->setAuthBlockArraySize(1);
78  msg->getAuthBlock(0).setPubKey(BinaryValue("123"));
79  msg->getAuthBlock(0).setSignature(BinaryValue("456"));
80  msg->getAuthBlock(0).setCert(BinaryValue("789"));
81 
82  // record statistics
83  RECORD_STATS(numSign++);
84 }
85 
87 {
88  if (msg->getAuthBlockArraySize() == 0) {
89  // message contains no signature
90  return false;
91  }
92 
93  // need to remove controlInfo before serializing
94  BaseRpcMessage *msgStripped = static_cast<BaseRpcMessage*>(msg->dup());
95 
96  if (msgStripped->getControlInfo() != NULL) {
97  delete msgStripped->removeControlInfo();
98  }
99 
100  // serialize message (needed to calculate message hash)
101  commBuffer.reset();
102  commBuffer.packObject(msgStripped);
103  delete msgStripped;
104 
105  // calculate hash and signature
106  commBuffer.getBuffer();
107 
108  //const BinaryValue& pubKey = msg->getAuthBlock(0).getPubKey();
109  //const BinaryValue& signature = msg->getAuthBlock(0).getSignature();
110  //const BinaryValue& cert = msg->getAuthBlock(0).getCert();
111 
112  //...
113 
114  return true;
115 }
116 
117 void CryptoModule::handleMessage(cMessage *msg)
118 {
119  delete msg; // just discard everything we receive
120 }
121 
123 {
124  simtime_t time = globalStatistics->calcMeasuredLifetime(
125  overlay->getCreationTime());
126 
127  if (time >= GlobalStatistics::MIN_MEASURED) {
128  if (numSign > 0) {
129  globalStatistics->addStdDev("CryptoModule: Sign Operations/s",
130  numSign / time);
131  }
132  }
133 }
134