142 lines
4.8 KiB
C
142 lines
4.8 KiB
C
//
|
||
// Created by cfif on 03.06.2024.
|
||
//
|
||
#include "Certs.h"
|
||
#include "string.h"
|
||
#include "AtGsmSimComA7600_SSL_LOAD_CA.h"
|
||
|
||
uint16_t XfcArrayGetBytesFrontToSend(tCerts *certs, tCertsType certsType, tXfcArray *array, uint16_t length) {
|
||
|
||
uint16_t available = XfcArrayGetDataSize(array);
|
||
|
||
uint16_t toGet = available > length ? length : available;
|
||
|
||
|
||
if (certsType == CERT_CA) {
|
||
if (osMutexAcquire(certs->gsmAt->access, 5000) == osOK) {
|
||
|
||
AtGsmSimComA7600_SSL_DEL_ONE_CA(certs->gsmAt, file_ca, strlen(file_ca));
|
||
|
||
AtGsmSimComA7600_SSL_LOAD_CA(certs->gsmAt,
|
||
file_ca, strlen(file_ca),
|
||
array->data + array->begin, toGet, 2000);
|
||
osMutexRelease(certs->gsmAt->access);
|
||
}
|
||
}
|
||
|
||
if (certsType == CERT_CLIENT) {
|
||
if (osMutexAcquire(certs->gsmAt->access, 5000) == osOK) {
|
||
|
||
AtGsmSimComA7600_SSL_DEL_ONE_CA(certs->gsmAt, file_crt, strlen(file_crt));
|
||
|
||
AtGsmSimComA7600_SSL_LOAD_CA(certs->gsmAt,
|
||
file_crt, strlen(file_crt),
|
||
array->data + array->begin, toGet, 2000);
|
||
osMutexRelease(certs->gsmAt->access);
|
||
}
|
||
}
|
||
|
||
if (certsType == CERT_CLIENT_KEY) {
|
||
if (osMutexAcquire(certs->gsmAt->access, 5000) == osOK) {
|
||
|
||
AtGsmSimComA7600_SSL_DEL_ONE_CA(certs->gsmAt, file_key, strlen(file_key));
|
||
|
||
AtGsmSimComA7600_SSL_LOAD_CA(certs->gsmAt,
|
||
file_key, strlen(file_key),
|
||
array->data + array->begin, toGet, 2000);
|
||
osMutexRelease(certs->gsmAt->access);
|
||
}
|
||
}
|
||
|
||
|
||
array->begin += toGet;
|
||
|
||
XfcArraySpaceInv(array);
|
||
|
||
return toGet;
|
||
}
|
||
|
||
static uint8_t XfcProtMethod_CertsSetChange(tXfcArray *request, tXfcArray *response, tCerts *certs) {
|
||
|
||
if (certs->gsmAt == NULL)
|
||
return 0;
|
||
|
||
// Запись избранных параметров
|
||
uint16_t paramCount = 0;
|
||
uint8_t paramValueID_len = 0;
|
||
uint8_t paramValueID_text[64];
|
||
|
||
XFC_CMD_RX_GET_RAW(paramCount);
|
||
|
||
if (!paramCount) {
|
||
return XFC_TRANSPORT_PROTOCOL_RESPONSE_RESULT_OK;
|
||
}
|
||
|
||
for (uint16_t count = 0; count < paramCount; ++count) {
|
||
|
||
// Получение текстового id параметра
|
||
XFC_CMD_RX_GET_RAW(paramValueID_len);
|
||
XFC_CMD_RX_GET_ARR(paramValueID_text, paramValueID_len);
|
||
|
||
uint16_t dataLen;
|
||
XFC_CMD_RX_GET_RAW(dataLen);
|
||
|
||
if (memcmp(paramValueID_text, "CACERT", sizeof("CACERT") - 1) == 0) {
|
||
uint16_t countLen = XfcArrayGetBytesFrontToSend(certs, CERT_CA, request, dataLen);
|
||
if (countLen != dataLen)
|
||
return XFC_TRANSPORT_PROTOCOL_REQUEST_UNEXPECTEDLY_SHORT;
|
||
}
|
||
|
||
if (memcmp(paramValueID_text, "CLIENTCERT", sizeof("CLIENTCERT") - 1) == 0) {
|
||
uint16_t countLen = XfcArrayGetBytesFrontToSend(certs, CERT_CLIENT, request, dataLen);
|
||
if (countLen != dataLen)
|
||
return XFC_TRANSPORT_PROTOCOL_REQUEST_UNEXPECTEDLY_SHORT;
|
||
}
|
||
|
||
if (memcmp(paramValueID_text, "CLIENTKEY", sizeof("CLIENTKEY") - 1) == 0) {
|
||
uint16_t countLen = XfcArrayGetBytesFrontToSend(certs, CERT_CLIENT_KEY, request, dataLen);
|
||
if (countLen != dataLen)
|
||
return XFC_TRANSPORT_PROTOCOL_REQUEST_UNEXPECTEDLY_SHORT;
|
||
}
|
||
|
||
}
|
||
|
||
return XFC_TRANSPORT_PROTOCOL_RESPONSE_RESULT_OK;
|
||
}
|
||
|
||
|
||
uint8_t XfcProtMethod_CertsSet(tXfcArray *request, tXfcArray *response, tCerts *certs) {
|
||
|
||
uint8_t result = XfcProtMethod_CertsSetChange(request, response, certs);
|
||
|
||
tIsFind check;
|
||
memset(&check, 0, sizeof(check));
|
||
|
||
if (osMutexAcquire(certs->gsmAt->access, 5000) == osOK) {
|
||
check = AtGsmSimComA7600_SSL_CHECK_CA(certs->gsmAt,
|
||
file_ca, strlen(file_ca),
|
||
file_crt, strlen(file_crt),
|
||
file_key, strlen(file_key), 2000);
|
||
osMutexRelease(certs->gsmAt->access);
|
||
}
|
||
|
||
if (!check.isFind1) {
|
||
String64Copy(&certs->cacert, "Не загружен", sizeof("Не загружен") - 1);
|
||
} else {
|
||
String64Copy(&certs->cacert, "Загружен", sizeof("Загружен") - 1);
|
||
}
|
||
|
||
if (!check.isFind2) {
|
||
String64Copy(&certs->clientcert, "Не загружен", sizeof("Не загружен") - 1);
|
||
} else {
|
||
String64Copy(&certs->clientcert, "Загружен", sizeof("Загружен") - 1);
|
||
}
|
||
|
||
if (!check.isFind3) {
|
||
String64Copy(&certs->clientkey, "Не загружен", sizeof("Не загружен") - 1);
|
||
} else {
|
||
String64Copy(&certs->clientkey, "Загружен", sizeof("Загружен") - 1);
|
||
}
|
||
|
||
return result;
|
||
} |