diff --git a/src/tpm.c b/src/tpm.c index dabe89f..76fb857 100644 --- a/src/tpm.c +++ b/src/tpm.c @@ -13,6 +13,12 @@ #include #include +static char* tts(int type) { + static char* stype[] = {"null", "entry", "exit", "single"}; + if (type < 1 || type > 3) return stype[0]; + else return stype[type]; +} + int tpm_is_inside_zone(void* fc, tolling_info_t* ti) { int rv = 0; @@ -37,6 +43,15 @@ int tpm_pay(void* fc, tolling_info_t* info, void* security_socket, uint8_t* neig facilities_t* facilities = (facilities_t*) fc; tolling_t* tolling = (tolling_t*) &facilities->tolling; + const size_t buf_len = 2048; + uint8_t tpm_uper[buf_len]; + uint8_t buf[buf_len]; + + if (!tolling->enabled) { + syslog_debug("[facilities] [tolling] tolling is disabled"); + goto cleanup; + } + pthread_mutex_lock(&facilities->epv.time.lock); tolling->tz = it2s_tender_get_now(TIME_MICROSECONDS) ; pthread_mutex_unlock(&facilities->epv.time.lock); @@ -47,19 +62,10 @@ int tpm_pay(void* fc, tolling_info_t* info, void* security_socket, uint8_t* neig SecurityReply_t* srep = NULL; FacilitiesIndication_t* fi = NULL; - const size_t buf_len = 2048; - uint8_t tpm_uper[buf_len]; - uint8_t buf[buf_len]; - - if (!tolling->enabled) { - syslog_debug("[facilities] [tolling] tolling is disabled"); - goto cleanup; - } - tolling->station.obu.active = true; tolling->station.obu.nonce = rand() + 1; - syslog_info("[facilities] [tolling] issuing toll payment > client: %ld | nonce: %ld", tolling->station.obu.client_id, tolling->station.obu.nonce); + syslog_info("[facilities] [tolling] issuing toll %s.request | client: %ld nonce: %ld", tts(info->asn->tollType), tolling->station.obu.client_id, tolling->station.obu.nonce); // TPM tpm = calloc(1, sizeof(TPM_t)); @@ -101,15 +107,21 @@ int tpm_pay(void* fc, tolling_info_t* info, void* security_socket, uint8_t* neig tpm->tpm->tollingType = calloc(1, sizeof(TollingType_t)); TollingType_t* type = tpm->tpm->tollingType; - tolling->station.obu.toll_type = info->asn->tollType; switch (info->asn->tollType) { case TollType_entry: + if (tolling->station.obu.toll_type != -1) { + syslog_err("[facilities] [tolling] trying to issue entry.request but current tolling state is %s", + tts(tolling->station.obu.toll_type)); + rv = 1; + goto cleanup; + } type->present = TollingType_PR_entry; type->choice.entry.present = TollingEntry_PR_request; type->choice.entry.choice.request.clientId = tolling->station.obu.client_id; type->choice.entry.choice.request.infoId = info->asn->id; type->choice.entry.choice.request.transactionNonce = tolling->station.obu.nonce; break; + case TollType_exit: type->present = TollingType_PR_exit; type->choice.exit = calloc(1, sizeof(TollingExit_t)); @@ -118,15 +130,29 @@ int tpm_pay(void* fc, tolling_info_t* info, void* security_socket, uint8_t* neig type->choice.exit->choice.request->clientId = tolling->station.obu.client_id; type->choice.exit->choice.request->infoId = info->asn->id; type->choice.exit->choice.request->transactionNonce = tolling->station.obu.nonce; - uint8_t b_tep[1024]; - asn_enc_rval_t e_tep = uper_encode_to_buffer(&asn_DEF_TPM, NULL, tolling->station.obu.entry_proof, b_tep, 1024); - if (e_tep.encoded == -1) { - syslog_err("[facilities] [tolling] error encoding TPM entry proof"); - return 1; + + if (tolling->station.obu.toll_type != TollingType_PR_entry) { + syslog_err("[facilities] [tolling] trying to issue exit.request but current tolling state is %s - will not provide entry proof", + tts(tolling->station.obu.toll_type)); + } else { + uint8_t b_tep[1024]; + asn_enc_rval_t e_tep = uper_encode_to_buffer(&asn_DEF_TPM, NULL, tolling->station.obu.entry_proof, b_tep, 1024); + if (e_tep.encoded == -1) { + syslog_err("[facilities] [tolling] error encoding TPM entry proof"); + rv = 1; + goto cleanup; + } + uper_decode_complete(NULL, &asn_DEF_TPM, (void**) &type->choice.exit->choice.request->entryProof, b_tep, (e_tep.encoded+7)/8); } - uper_decode_complete(NULL, &asn_DEF_TPM, (void**) &type->choice.exit->choice.request->entryProof, b_tep, (e_tep.encoded+7)/8); break; + case TollType_single: + if (tolling->station.obu.toll_type != -1) { + syslog_err("[facilities] [tolling] trying to issue single.request but current tolling state is %s", + tts(tolling->station.obu.toll_type)); + rv = 1; + goto cleanup; + } type->present = TollingType_PR_single; type->choice.single.present = TollingSingle_PR_request; type->choice.single.choice.request.clientId = tolling->station.obu.client_id; @@ -135,6 +161,8 @@ int tpm_pay(void* fc, tolling_info_t* info, void* security_socket, uint8_t* neig break; } + tolling->station.obu.toll_type = info->asn->tollType; + // Encode TollingPaymentMessage asn_enc_rval_t enc = oer_encode_to_buffer(&asn_DEF_TollingPaymentMessage, NULL, tpm->tpm, buf, buf_len); if (enc.encoded == -1) { @@ -411,7 +439,8 @@ static void rsu_handle_recv(facilities_t* facilities, TPM_t* tpm_rx, void* secur switch (tolling->protocol) { case TOLLING_PROTOCOL_SIMPLE: - syslog_info("[facilities] [tolling] received toll payment > client: %lld (certificate id: %02x%02x%02x) | nonce: %lld", + syslog_info("[facilities] [tolling] received toll %s.request | client: %lld (certificate id: 0x%02x%02x%02x) nonce: %lld", + tts(type_rx->present), (long long) client_id, neighbour ? neighbour[5] : 0, neighbour ? neighbour[6] : 0, @@ -420,7 +449,8 @@ static void rsu_handle_recv(facilities_t* facilities, TPM_t* tpm_rx, void* secur ); break; case TOLLING_PROTOCOL_TLS: - syslog_info("[facilities] [tolling] received toll payment > client: %lld | nonce: %lld", + syslog_info("[facilities] [tolling] received toll %s.request | client: %lld nonce: %lld", + tts(type_rx->present), (long long) client_id, (long long) nonce ); @@ -672,10 +702,10 @@ static void rsu_handle_recv(facilities_t* facilities, TPM_t* tpm_rx, void* secur memcpy(sreq->choice.tlsSend.data.buf, tpm_uper, tpm_uper_len); id = rand() + 1; // TODO handle various vehicles - if (!tolling->station.obu.tls_conn_id) { - tolling->station.obu.tls_conn_id = id; + if (!tolling->station.rsu.tls_conn_id) { + tolling->station.rsu.tls_conn_id = id; } - sreq->choice.tlsSend.connId = tolling->station.obu.tls_conn_id; + sreq->choice.tlsSend.connId = tolling->station.rsu.tls_conn_id; buf[0] = 4; asn_enc_rval_t enc = oer_encode_to_buffer(&asn_DEF_SecurityRequest, NULL, sreq, buf+1, buf_len-1); @@ -803,7 +833,7 @@ static void veh_handle_recv(tolling_t* tolling, TPM_t* tpm_rx, void* security_so return; } if (tolling->station.obu.toll_type != TollingType_PR_entry) { - syslog_err("[facilities] [tolling] received TPM (%d) is not expected toll type (%d)", + syslog_err("[facilities] [tolling] received TPM toll (%d) is not expected toll type (%d)", TollingType_PR_entry, tolling->station.obu.toll_type); } client_id = type_rx->choice.entry.choice.reply.clientId; @@ -828,7 +858,7 @@ static void veh_handle_recv(tolling_t* tolling, TPM_t* tpm_rx, void* security_so return; } if (tolling->station.obu.toll_type != TollingType_PR_exit) { - syslog_err("[facilities] [tolling] received TPM (%d) is not expected toll type (%d)", + syslog_err("[facilities] [tolling] received TPM toll (%d) is not expected toll type (%d)", TollingType_PR_exit, tolling->station.obu.toll_type); return; } @@ -844,7 +874,7 @@ static void veh_handle_recv(tolling_t* tolling, TPM_t* tpm_rx, void* security_so return; } if (tolling->station.obu.toll_type != TollingType_PR_single) { - syslog_err("[facilities] [tolling] received TPM (%d) is not expected toll type (%d)", + syslog_err("[facilities] [tolling] received TPM type (%d) is not expected toll type (%d)", TollingType_PR_single, tolling->station.obu.toll_type); } client_id = type_rx->choice.single.choice.reply.clientId; @@ -932,11 +962,11 @@ static void veh_handle_recv(tolling_t* tolling, TPM_t* tpm_rx, void* security_so switch (type_rx->present) { case TollingType_PR_entry: - syslog_info("[facilities] [tolling] received entry tolling reply | client:%lld nonce:%ld accepted:%s", (long long) tolling->station.obu.client_id, tolling->station.obu.nonce, accepted ? "yes" : "no"); + syslog_info("[facilities] [tolling] received entry.reply | client: %lld nonce: %ld accepted: %s", (long long) tolling->station.obu.client_id, tolling->station.obu.nonce, accepted ? "yes" : "no"); break; case TollingType_PR_exit: - syslog_info("[facilities] [tolling] received exit tolling reply | client:%lld nonce:%ld accepted:%s", (long long) tolling->station.obu.client_id, tolling->station.obu.nonce, accepted ? "yes" : "no"); + syslog_info("[facilities] [tolling] received exit.reply | client: %lld nonce: %ld accepted: %s", (long long) tolling->station.obu.client_id, tolling->station.obu.nonce, accepted ? "yes" : "no"); tolling->station.obu.toll_type = -1; tolling->station.obu.active = false; ASN_STRUCT_FREE(asn_DEF_TPM, tolling->station.obu.entry_proof); @@ -944,7 +974,7 @@ static void veh_handle_recv(tolling_t* tolling, TPM_t* tpm_rx, void* security_so break; case TollingType_PR_single: - syslog_info("[facilities] [tolling] received single tolling reply | client:%lld nonce:%ld accepted:%s", (long long) tolling->station.obu.client_id, tolling->station.obu.nonce, accepted ? "yes" : "no"); + syslog_info("[facilities] [tolling] received single.reply | client: %lld nonce: %ld accepted: %s", (long long) tolling->station.obu.client_id, tolling->station.obu.nonce, accepted ? "yes" : "no"); tolling->station.obu.toll_type = -1; tolling->station.obu.active = false; break; diff --git a/src/tpm.h b/src/tpm.h index dab1841..5b8009c 100644 --- a/src/tpm.h +++ b/src/tpm.h @@ -29,7 +29,7 @@ typedef struct tolling { union { // RSU struct { - + uint64_t tls_conn_id; } rsu; // OBU