1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
|
void* readMessage(void *sid)
{
int s = *((int*)sid);
int rc, index;
char iobuf[LENGTH];
bool flag, carExist, firstEntry;
string store_request, store_response, branchNum, randomShareKey, TS1, TS2, ticket, encrypt_data, CASShareKey, ASVShareKey;
vector<string> request, response;
flag = true, carExist = false, firstEntry = true;
printf("\ns=%d, I am going to read from client\n", s);
int count = 0; // remove later
int numOfRequest = 0;
/*
Receving from protocol 1
request vector
0. Car
1. AS
2. TS
3. Encryption
4. Encryption + hash
*/
while(rc = read(s, iobuf, sizeof (iobuf)))
{
if (rc == -1)
{
perror("read");
close(s);
pthread_exit(0);
}
store_request.assign(iobuf);//convert from char array to string
if(firstEntry){
numOfRequest = atoi(store_request.c_str());//get number of request coming in
firstEntry = false;
}else{// keep adding the request coming in from client
request.push_back(store_request);//add all incoming request to vector
count++;
memset(&iobuf, 0, sizeof(iobuf));
if(count == numOfRequest){//finish all the communication
cout << "Protocol 1: C -> AS" << endl;
for(int i = 0; i < request.size(); i++)
cout << i + 1 << " : " << request[i] << endl;
carExist = checkCar(request[0], keyCAS, CBInfro, CBInfroArraySize);
if(carExist){//if car exist
CASShareKey = getShareKey(request[0], keyCAS);
if(intergrityCheck(request, branchNum, CASShareKey) && isOneMinute(request[2]) && checkBranchExist(CBInfro, request[0], branchNum, CBInfroArraySize)){
store_response = "AS: Client successful authenicate.";
randomShareKey = genShareKeyCV();
TS2= getTimeStamp();
ASVShareKey = getKeyASV(keyASV, atoi(branchNum.c_str()));
ticket = getTicket(ASVShareKey, randomShareKey, request[0], branchNum, TS2);
encrypt_data = ASCInfoEncryption(CASShareKey, randomShareKey, request, branchNum, TS2);
//response push_back
response.push_back(request[1]);
response.push_back(branchNum);
response.push_back(encrypt_data);
response.push_back(ticket);
}else{//car exist but during integrity checking encounter error
cout << "[Message] Error found during authentication check\n";
store_response = "Error";
response.push_back(store_response);
}
}else{//car does not exist
cout << "[Message] Car does not exist \n";
store_response = "Error";
response.push_back(store_response);
}//end else
/*Protocol 2
AS -> C
response vector
0. AS
1. Branch
2. Encryption
3. Ticket
*/
cout << "Protocol 2: AS -> C "<< endl;
for(int i = 0; i < response.size();i++){
memset(&iobuf, 0, sizeof(iobuf));//clear buffer
strcpy(iobuf, response[i].c_str());
rc = write(s, iobuf, sizeof(iobuf));
cout << i + 1<< " :" << response[i] << endl;
cout << " :" << iobuf << endl;
if (rc == -1)
{
perror("write");
close(s);
pthread_exit(0);
}//end if
memset(&iobuf, 0, sizeof(iobuf));//clear buffer
rc = read(s, iobuf, sizeof(iobuf));
if (rc == -1)
{
perror("read");
close(s);
pthread_exit(0);
}
if(i == response.size()-1){
store_response="Completed";
memset(&iobuf, 0, sizeof(iobuf));//clear buffer
strcpy(iobuf, store_response.c_str());
rc = write(s, iobuf, sizeof(iobuf));
if (rc == -1)
{
perror("write");
close(s);
pthread_exit(0);
}//end if
}
}//end of for loop
}//end of if
}//end of else
}//end of inner while
close(s);
printf("\nThread finished\n");
pthread_exit(0);
return 0;
}
|