This commit is contained in:
emanuel 2022-01-18 17:52:46 +00:00
parent e9dfc7764f
commit 385e965c6f
5 changed files with 123 additions and 10 deletions

View File

@ -345,7 +345,7 @@ lightship_t* lightship_init() {
lightship->use_obd = 0;
}
else{
lightship->use_obd = 1;
lightship->use_obd = 0; // TODO
close(shm_fd);
}

View File

@ -649,11 +649,11 @@ int main() {
memset(&facilities, 0x00, sizeof(facilities_t));
facilities.zmq.ctx = zmq_ctx_new();
facilities.lightship = lightship_init();
facilities.dissemination = dissemination_init();
facilities.tx_queue = queue_init();
facilities.lightship = lightship_init();
facilities.den = calloc(1, sizeof(den_t));
facilities.infrastructure = calloc(1, sizeof(infrastructure_t));
facilities.dissemination = dissemination_init();
bulletin_init(&facilities.bulletin);
time_t t;
@ -675,12 +675,15 @@ int main() {
// Infrastructure
pthread_create(&facilities.infrastructure_service, NULL, infrastructure_service, (void*) &facilities);
// CPM
// CP
pthread_create(&facilities.cp_service, NULL, cp_service, (void*) &facilities);
// SA
pthread_create(&facilities.sa_service, NULL, sa_service, (void*) &facilities);
// Tolling
tolling_init(&facilities.tolling, facilities.zmq.ctx, facilities.zmq.security_address);
void* security_socket = zmq_socket(facilities.zmq.ctx, ZMQ_REQ);
int wait_ms = 1000;
zmq_setsockopt(security_socket, ZMQ_RCVTIMEO, &wait_ms, sizeof(int));

View File

@ -86,7 +86,6 @@ SAEM_CODE_R saem_check(void* fc, bulletin_t* bulletin, SAEM_t* saem, uint8_t* ne
si->chOptions.extensions->list.array[e]->choice.applicationDataSAM.buf,
si->chOptions.extensions->list.array[e]->choice.applicationDataSAM.size
);
syslog_err("dec.code:: %d", dec.code);
if (!dec.code) {
bulletin->to_consume[bulletin->to_provide_len]->info.internal_p = facilities->tolling.infos.z[facilities->tolling.infos.length];
++facilities->tolling.infos.length;
@ -358,7 +357,7 @@ void *sa_service(void *fc) {
((TollingPaymentInfo_t*)bulletin->to_consume[a]->info.internal_p)->zone.a/10.0,
((TollingPaymentInfo_t*)bulletin->to_consume[a]->info.internal_p)->zone.b/10.0,
((TollingPaymentInfo_t*)bulletin->to_consume[a]->info.internal_p)->zone.angle/10.0,
DCM_HAVERSINE
DCM_VINCENTY
)) {
continue;
}

113
src/tpm.c
View File

@ -1,6 +1,8 @@
#include "tpm.h"
#include "facilities.h"
#include <itss-security/SecurityRequest.h>
#include <itss-security/SecurityReply.h>
#include <it2s-tender/time.h>
#include <itss-transport/TransportRequest.h>
#include <it2s-tender/space.h>
@ -16,6 +18,8 @@ int tpm_pay(void* fc, uint8_t* neighbour) {
tolling->tz = it2s_tender_get_clock(&facilities->epv) ;
TransportRequest_t* tr = NULL;
SecurityRequest_t* sreq = NULL;
SecurityReply_t* srep = NULL;
TPM_t* tpm = NULL;
const size_t buf_len = 2048;
@ -97,8 +101,53 @@ int tpm_pay(void* fc, uint8_t* neighbour) {
tpm->tpm.tollingFlow.choice.request.paymentMethod.choice.fiat.fiatId = FiatId_eur;
tpm->tpm.tollingFlow.choice.request.transactionNonce = tolling->nonce;
// encode TPM
asn_enc_rval_t enc = uper_encode_to_buffer(&asn_DEF_TPM, NULL, tpm, buf, buf_len);
// Encode TollingPaymentMessage
asn_enc_rval_t enc = oer_encode_to_buffer(&asn_DEF_TollingPaymentMessage, NULL, &tpm->tpm, buf, buf_len);
if (enc.encoded == -1) {
syslog_err("[facilities] [tolling] error encoding TollingPaymentMessage (%s)", enc.failed_type->name);
rv = 1;
goto cleanup;
}
// Sign
sreq = calloc(1, sizeof(SecurityRequest_t));
sreq->present = SecurityRequest_PR_sign;
sreq->choice.sign.data.size = enc.encoded;
sreq->choice.sign.data.buf = malloc(enc.encoded);
memcpy(sreq->choice.sign.data.buf, buf, enc.encoded);
buf[0] = 4;
enc = oer_encode_to_buffer(&asn_DEF_SecurityRequest, NULL, sreq, buf+1, 2047);
syslog_debug("[facilities]->[security] SecurityRequest.sign (%ldB)", enc.encoded+1);
zmq_send(tolling->security_socket, buf, enc.encoded+1, 0);
int32_t rl = zmq_recv(tolling->security_socket, buf, buf_len, 0);
syslog_debug("[facilities]<-[security] SecurityReply.sign (%dB)", rl);
if (oer_decode(NULL, &asn_DEF_SecurityReply, (void**) &srep, buf, rl).code) {
syslog_err("[facilities] SecurityReply.sign decode failure");
rv = 1;
goto cleanup;
}
if (srep->returnCode == SecurityReplyReturnCode_rejected) {
syslog_err("[facilities] SecurityReply.sign rejected");
rv = 1;
goto cleanup;
}
tpm->signature.r.size = srep->data->choice.sign.r.size;
tpm->signature.r.buf = malloc(srep->data->choice.sign.r.size);
memcpy(tpm->signature.r.buf, srep->data->choice.sign.r.buf, srep->data->choice.sign.r.size);
tpm->signature.s.size = srep->data->choice.sign.s.size;
tpm->signature.s.buf = malloc(srep->data->choice.sign.s.size);
memcpy(tpm->signature.s.buf, srep->data->choice.sign.s.buf, srep->data->choice.sign.s.size);
tpm->signature.signer.size = srep->data->choice.sign.signer.size;
tpm->signature.signer.buf = malloc(srep->data->choice.sign.signer.size);
memcpy(tpm->signature.signer.buf, srep->data->choice.sign.signer.buf, srep->data->choice.sign.signer.size);
tpm->signature.type = srep->data->choice.sign.type;
// Encode TPM
enc = uper_encode_to_buffer(&asn_DEF_TPM, NULL, tpm, buf, buf_len);
if (enc.encoded == -1) {
syslog_err("[facilities] [tolling] error encoding TPM.request (%s)", enc.failed_type->name);
rv = 1;
@ -148,6 +197,8 @@ int tpm_pay(void* fc, uint8_t* neighbour) {
cleanup:
ASN_STRUCT_FREE(asn_DEF_TPM, tpm);
ASN_STRUCT_FREE(asn_DEF_TransportRequest, tr);
ASN_STRUCT_FREE(asn_DEF_SecurityRequest, sreq);
ASN_STRUCT_FREE(asn_DEF_SecurityReply, srep);
return rv;
}
@ -166,6 +217,8 @@ static void rsu_handle_recv(facilities_t* facilities, TollRequest_t* req, uint8_
uint8_t buf[buf_len];
TransportRequest_t* tr = NULL;
SecurityRequest_t* sreq = NULL;
SecurityReply_t* srep = NULL;
TPM_t* tpm = NULL;
// TPM
@ -203,8 +256,50 @@ static void rsu_handle_recv(facilities_t* facilities, TollRequest_t* req, uint8_
// TODO check clientId
// TODO dlt: check transaction
// Encode TollingPaymentMessage
asn_enc_rval_t enc = oer_encode_to_buffer(&asn_DEF_TollingPaymentMessage, NULL, &tpm->tpm, buf, buf_len);
if (enc.encoded == -1) {
syslog_err("[facilities] [tolling] error encoding TollingPaymentMessage (%s)", enc.failed_type->name);
goto cleanup;
}
// Sign
sreq = calloc(1, sizeof(SecurityRequest_t));
sreq->present = SecurityRequest_PR_sign;
sreq->choice.sign.data.size = enc.encoded;
sreq->choice.sign.data.buf = malloc(enc.encoded);
memcpy(sreq->choice.sign.data.buf, buf, enc.encoded);
buf[0] = 4;
enc = oer_encode_to_buffer(&asn_DEF_SecurityRequest, NULL, sreq, buf+1, 2047);
syslog_debug("[facilities]->[security] SecurityRequest.sign (%ldB)", enc.encoded+1);
zmq_send(facilities->tolling.security_socket, buf, enc.encoded+1, 0);
int32_t rl = zmq_recv(facilities->tolling.security_socket, buf, buf_len, 0);
syslog_debug("[facilities]<-[security] SecurityReply.sign (%dB)", rl);
if (oer_decode(NULL, &asn_DEF_SecurityReply, (void**) &srep, buf, rl).code) {
syslog_err("[facilities] SecurityReply.sign decode failure");
goto cleanup;
}
if (srep->returnCode == SecurityReplyReturnCode_rejected) {
syslog_err("[facilities] SecurityReply.sign rejected");
goto cleanup;
}
tpm->signature.r.size = srep->data->choice.sign.r.size;
tpm->signature.r.buf = malloc(srep->data->choice.sign.r.size);
memcpy(tpm->signature.r.buf, srep->data->choice.sign.r.buf, srep->data->choice.sign.r.size);
tpm->signature.s.size = srep->data->choice.sign.s.size;
tpm->signature.s.buf = malloc(srep->data->choice.sign.s.size);
memcpy(tpm->signature.s.buf, srep->data->choice.sign.s.buf, srep->data->choice.sign.s.size);
tpm->signature.signer.size = srep->data->choice.sign.signer.size;
tpm->signature.signer.buf = malloc(srep->data->choice.sign.signer.size);
memcpy(tpm->signature.signer.buf, srep->data->choice.sign.signer.buf, srep->data->choice.sign.signer.size);
tpm->signature.type = srep->data->choice.sign.type;
// encode TPM
asn_enc_rval_t enc = uper_encode_to_buffer(&asn_DEF_TPM, NULL, tpm, buf, buf_len);
enc = uper_encode_to_buffer(&asn_DEF_TPM, NULL, tpm, buf, buf_len);
if (enc.encoded == -1) {
syslog_err("[facilities] [tolling] error encoding TPM.reply (%s)", enc.failed_type->name);
goto cleanup;
@ -254,6 +349,8 @@ static void rsu_handle_recv(facilities_t* facilities, TollRequest_t* req, uint8_
cleanup:
ASN_STRUCT_FREE(asn_DEF_TPM, tpm);
ASN_STRUCT_FREE(asn_DEF_TransportRequest, tr);
ASN_STRUCT_FREE(asn_DEF_SecurityRequest, sreq);
ASN_STRUCT_FREE(asn_DEF_SecurityReply, srep);
}
static void veh_handle_recv(tolling_s* tolling, TollReply_t* rep, uint8_t* neighbour) {
@ -310,3 +407,13 @@ cleanup:
return rv;
}
int tolling_init(tolling_s* tolling, void* zmq_ctx, char* security_address) {
tolling->security_socket = zmq_socket(zmq_ctx, ZMQ_REQ);
int wait_ms = 1000;
zmq_setsockopt(tolling->security_socket, ZMQ_RCVTIMEO, &wait_ms, sizeof(int));
zmq_connect(tolling->security_socket, security_address);
return 0;
}

View File

@ -28,7 +28,11 @@ typedef struct tolling {
uint8_t length;
} infos;
// Security socket
void* security_socket;
} tolling_s;
int tolling_init(tolling_s* tolling, void* zmq_ctx, char* security_address);
int tpm_pay(void* fc, uint8_t* neighbour);
int tpm_recv(void* fc, TPM_t* tpm_rx, uint8_t* neighbour);