00001 // 00002 // Copyright (C) 2009 Institut fuer Telematik, Universitaet Karlsruhe (TH) 00003 // 00004 // This program is free software; you can redistribute it and/or 00005 // modify it under the terms of the GNU General Public License 00006 // as published by the Free Software Foundation; either version 2 00007 // of the License, or (at your option) any later version. 00008 // 00009 // This program is distributed in the hope that it will be useful, 00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 // GNU General Public License for more details. 00013 // 00014 // You should have received a copy of the GNU General Public License 00015 // along with this program; if not, write to the Free Software 00016 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 00017 // 00018 00025 #include <CommonMessages_m.h> 00026 #include <OverlayAccess.h> 00027 #include <GlobalStatisticsAccess.h> 00028 #include <CryptoModule.h> 00029 00030 using namespace std; 00031 00032 Define_Module(CryptoModule); 00033 00034 CryptoModule::CryptoModule() 00035 { 00036 globalStatistics = NULL; 00037 overlay = NULL; 00038 } 00039 00040 CryptoModule::~CryptoModule() 00041 { 00042 } 00043 00044 void CryptoModule::initialize() 00045 { 00046 globalStatistics = GlobalStatisticsAccess().get(); 00047 overlay = OverlayAccess().get(this); 00048 00049 numSign = 0; 00050 00051 // EV << "[CryptoModule::initialize() @ " << overlay->getThisNode().getAddress() 00052 // << " (" << overlay->getThisNode().getKey().toString(16) << ")]\n" 00053 // << " Reading key from file " << par("keyFile").stdstringValue() 00054 // << endl; 00055 } 00056 00057 void CryptoModule::signMessage(BaseRpcMessage *msg) 00058 { 00059 // need to remove controlInfo before serializing 00060 BaseRpcMessage *msgStripped = static_cast<BaseRpcMessage*>(msg->dup()); 00061 00062 if (msgStripped->getControlInfo() != NULL) { 00063 delete msgStripped->removeControlInfo(); 00064 } 00065 00066 // serialize message (needed to calculate message hash) 00067 commBuffer.reset(); 00068 commBuffer.packObject(msgStripped); 00069 delete msgStripped; 00070 00071 // calculate hash and signature 00072 // commBuffer.getBuffer(), commBuffer.getBufferLength() 00073 00074 // ... 00075 00076 // append public key and signature 00077 msg->setAuthBlockArraySize(1); 00078 msg->getAuthBlock(0).setPubKey(BinaryValue("123")); 00079 msg->getAuthBlock(0).setSignature(BinaryValue("456")); 00080 msg->getAuthBlock(0).setCert(BinaryValue("789")); 00081 00082 // record statistics 00083 RECORD_STATS(numSign++); 00084 } 00085 00086 bool CryptoModule::verifyMessage(BaseRpcMessage *msg) 00087 { 00088 if (msg->getAuthBlockArraySize() == 0) { 00089 // message contains no signature 00090 return false; 00091 } 00092 00093 // need to remove controlInfo before serializing 00094 BaseRpcMessage *msgStripped = static_cast<BaseRpcMessage*>(msg->dup()); 00095 00096 if (msgStripped->getControlInfo() != NULL) { 00097 delete msgStripped->removeControlInfo(); 00098 } 00099 00100 // serialize message (needed to calculate message hash) 00101 commBuffer.reset(); 00102 commBuffer.packObject(msgStripped); 00103 delete msgStripped; 00104 00105 // calculate hash and signature 00106 commBuffer.getBuffer(); 00107 00108 //const BinaryValue& pubKey = msg->getAuthBlock(0).getPubKey(); 00109 //const BinaryValue& signature = msg->getAuthBlock(0).getSignature(); 00110 //const BinaryValue& cert = msg->getAuthBlock(0).getCert(); 00111 00112 //... 00113 00114 return true; 00115 } 00116 00117 void CryptoModule::handleMessage(cMessage *msg) 00118 { 00119 delete msg; // just discard everything we receive 00120 } 00121 00122 void CryptoModule::finish() 00123 { 00124 simtime_t time = globalStatistics->calcMeasuredLifetime( 00125 overlay->getCreationTime()); 00126 00127 if (time >= GlobalStatistics::MIN_MEASURED) { 00128 if (numSign > 0) { 00129 globalStatistics->addStdDev("CryptoModule: Sign Operations/s", 00130 numSign / time); 00131 } 00132 } 00133 } 00134