Skip to content

Commit

Permalink
bugfix: BSM verification, binary blob to hex fix (#295)
Browse files Browse the repository at this point in the history
  • Loading branch information
arayama authored Dec 20, 2021
1 parent f7f8343 commit b8b207a
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 53 deletions.
2 changes: 1 addition & 1 deletion src/v2i-hub/DsrcImmediateForwardPlugin/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
},
{
"key": "HSMurl",
"default": "http://192.168.0.237:3000/v1/scms/",
"default": "http://<softHSM IP>:3000/v1/scms/",
"description": "REST API endpoint url needed to make HSM calls"
}

Expand Down
9 changes: 7 additions & 2 deletions src/v2i-hub/MessageReceiverPlugin/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
"messageTypes":[
],
"configuration":[
{
"key":"messageid",
"default":"0012,0013,0014,001D,00F0,00F1,00F2,00F3,00F4,00F5,00F6,00F7",
"description":"Comma separated list of SAE J2735 message ID values."
},
{
"key":"IP",
"default":"127.0.0.1",
Expand Down Expand Up @@ -45,12 +50,12 @@
},
{
"key":"HSMLocation",
"default":"/usr/local/lib/softhsm/libsofthsm2.so",
"default":"<SoftHSM library location>",
"description":"Location of HSM module static library."
},
{
"key":"HSMurl",
"default": "http://192.168.0.237:3000/v1/scms/",
"default": "http://<SoftHSM IP>:3000/v1/scms/",
"description": "REST API endpoint url needed to make HSM calls"
}
]
Expand Down
81 changes: 31 additions & 50 deletions src/v2i-hub/MessageReceiverPlugin/src/MessageReceiverPlugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ int MessageReceiverPlugin::Main()
std::unique_ptr<tmx::utils::UdpServer> server;

byte_stream extractedpayload(4000);
int mlen=0;

while (_plugin->state != IvpPluginState_error)
{
Expand All @@ -459,43 +459,26 @@ int MessageReceiverPlugin::Main()
uint64_t time = Clock::GetMillisecondsSinceEpoch();

totalBytes += len;


extractedpayload=incoming;

// @SONAR_STOP@
// if verification enabled, access HSM

if (verState == 1)
{

// convert unit8_t vector to hex stream

char arr[incoming.size()+1];
for (int i= 0;i < incoming.size()-1;i++)
{
arr[i]=incoming[i];

}

// get hex stream from the binary


std::string msgbin(arr);
unsigned char out[incoming.size()+1];
for(unsigned int c = 0; c < incoming.size(); c++)
{
unsigned int in;
sscanf(msgbin.data() + (c * 2), "%02x", &in);
out[c] = in;
}


string msg(reinterpret_cast <char const*>(out));
stringstream ss;
ss << std::hex << std::setfill('0');
uint16_t it=0;

for (uint16_t it=0; it <len; it++) {
ss << std::setw(2) << static_cast<unsigned>(incoming[it]);
}

string msg = ss.str();

//the incoming payload is hex encoded, convert this to base64

std::string base64msg="";

hex2base64(msg,base64msg);
Expand All @@ -511,7 +494,7 @@ int MessageReceiverPlugin::Main()
std::string result="";
FILE* pipe= popen(cmd,"r");

if (pipe != NULL ) throw std::runtime_error("popen() failed!");
if (pipe == NULL ) throw std::runtime_error("popen() failed!");

try{
while (fgets(buffer, sizeof(buffer),pipe) != NULL)
Expand All @@ -529,58 +512,57 @@ int MessageReceiverPlugin::Main()
int msgValid = sd->valueint;

string extractedmsg="";
bool foundId=false;
bool foundId=false;

if (msgValid == 1)
{
// look for a valid message type. 0012,0013,0014 etc. and count length of bytes to extract the message

std::vector<string>::iterator itr=messageid.begin();
int mlen;

while(itr != messageid.end())
{
//look for the message header within the first 20 bytes.
size_t idloc = msg.find(*itr);


if(idloc != string::npos and idloc < IDCHECKLIMIT) // making sure the msgID lies within the first IDCHECKLIMIT Characters
{
// message id found

if (msg[idloc+4] == '8') // if the length is longer than 256
{
string tmp = msg.substr(idloc+5,3);
const char *c = tmp.c_str(); // take out next three nibble for length
int len = (strtol(c,nullptr,16)+4)*2; // 5 nibbles added for msgid and the extra 1 byte

extractedmsg = msg.substr(idloc,len);
byte_stream temp(extractedmsg.begin(),extractedmsg.end());

extractedpayload = temp;
mlen = (strtol(c,nullptr,16)+4)*2; // 5 nibbles added for msgid and the extra 1 byte
extractedmsg = msg.substr(idloc,mlen);

}

else
{
string tmp = msg.substr(idloc+4,2);
const char *c = tmp.c_str(); // take out next three nibble for length
int len = (strtol(c,nullptr,16)+3)*2; // 5 nibbles added for msgid and the extra 1 byte

extractedmsg = msg.substr(idloc,len);
byte_stream temp(extractedmsg.begin(),extractedmsg.end());

extractedpayload=temp;
mlen = (strtol(c,nullptr,16)+3)*2; // 5 nibbles added for msgid and the extra 1 byte
extractedmsg = msg.substr(idloc,mlen);
}

foundId=true;
break; // can break out if already found a msg id

int k=0;

for (unsigned int i = 0; i < extractedmsg.length(); i += 2) {
string bs = extractedmsg.substr(i, 2);
uint8_t byte = (uint8_t) strtol(bs.c_str(), nullptr, 16);
extractedpayload[k++]=byte;

}
break; // can break out if already found a msg id
}
itr++;
}

if (foundId==false)
{
PLOG(logERROR) <<" Unable to find any valid msg ID in the incoming message. \n";
PLOG(logDEBUG) <<" Unable to find any valid msg ID in the incoming message. \n";
continue; //do not send the message out to v2x hub if msgid check fails
}
}
Expand All @@ -592,6 +574,9 @@ int MessageReceiverPlugin::Main()
}

}
else {
extractedpayload=incoming;
}

// @SONAR_START@

Expand All @@ -615,11 +600,7 @@ int MessageReceiverPlugin::Main()
}
}

len = extractedpayload.size();



this->IncomingMessage(extractedpayload.data(), len, enc.empty() ? NULL : enc.c_str(), 0, 0, time);
this->IncomingMessage(extractedpayload.data(), extractedpayload.size(), enc.empty() ? NULL : enc.c_str(), 0, 0, time);

}
else if (len < 0)
Expand Down

0 comments on commit b8b207a

Please sign in to comment.