Skip to content

Commit 127a86f

Browse files
committed
[#344] Added a way to parse if using TC/TM/AOS table by querying each of them with the managed parameters values
1 parent 274bc84 commit 127a86f

File tree

2 files changed

+111
-65
lines changed

2 files changed

+111
-65
lines changed

src/core/crypto.c

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1216,27 +1216,27 @@ uint32_t Crypto_Get_FSR()
12161216
* @param algo: uint8_t
12171217
* @return int32: Key Length
12181218
**/
1219-
char* Crypto_Get_MariaDB_Table_Prefix(uint8_t table)
1220-
{
1221-
char *retval = "";
1222-
1223-
switch (table)
1224-
{
1225-
case MARIADB_TC_TABLE:
1226-
retval = MARIADB_TC_TABLE_PREFIX;
1227-
break;
1228-
case MARIADB_TM_TABLE:
1229-
retval = MARIADB_TM_TABLE_PREFIX;
1230-
break;
1231-
case MARIADB_AOS_TABLE:
1232-
retval = MARIADB_AOS_TABLE_PREFIX;
1233-
break;
1234-
case MARIADB_LEGACY_TABLE:
1235-
retval = MARIADB_LEGACY_TABLE_PREFIX;
1236-
break;
1237-
default:
1238-
break;
1239-
}
1240-
1241-
return retval;
1242-
}
1219+
// char* Crypto_Get_MariaDB_Table_Prefix(uint8_t table)
1220+
// {
1221+
// char retval[25];
1222+
1223+
// switch (table)
1224+
// {
1225+
// case MARIADB_TC_TABLE:
1226+
// retval = MARIADB_TC_TABLE_PREFIX;
1227+
// break;
1228+
// case MARIADB_TM_TABLE:
1229+
// retval = MARIADB_TM_TABLE_PREFIX;
1230+
// break;
1231+
// case MARIADB_AOS_TABLE:
1232+
// retval = MARIADB_AOS_TABLE_PREFIX;
1233+
// break;
1234+
// case MARIADB_LEGACY_TABLE:
1235+
// retval = MARIADB_LEGACY_TABLE_PREFIX;
1236+
// break;
1237+
// default:
1238+
// break;
1239+
// }
1240+
1241+
// return retval;
1242+
// }

src/sa/mariadb/sa_interface_mariadb.template.c

Lines changed: 87 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -48,18 +48,18 @@ static const char *SQL_SADB_GET_SA_BY_SPI =
4848
"SELECT "
4949
"spi,ekid,akid,sa_state,tfvn,scid,vcid,mapid,lpid,est,ast,shivf_len,shsnf_len,shplf_len,stmacf_len,ecs_len,HEX(ecs)"
5050
",HEX(iv),iv_len,acs_len,HEX(acs),abm_len,HEX(abm),arsn_len,HEX(arsn),arsnw"
51-
" FROM %ssecurity_associations WHERE spi='%d'";
51+
" FROM %s WHERE spi='%d'";
5252
static const char *SQL_SADB_GET_SA_BY_GVCID =
5353
"SELECT "
5454
"spi,ekid,akid,sa_state,tfvn,scid,vcid,mapid,lpid,est,ast,shivf_len,shsnf_len,shplf_len,stmacf_len,ecs_len,HEX(ecs)"
5555
",HEX(iv),iv_len,acs_len,HEX(acs),abm_len,HEX(abm),arsn_len,HEX(arsn),arsnw"
56-
" FROM %ssecurity_associations WHERE tfvn='%d' AND scid='%d' AND vcid='%d' AND mapid='%d' AND sa_state='%d'";
56+
" FROM %s WHERE tfvn='%d' AND scid='%d' AND vcid='%d' AND mapid='%d' AND sa_state='%d'";
5757
static const char *SQL_SADB_UPDATE_IV_ARC_BY_SPI =
58-
"UPDATE %ssecurity_associations"
58+
"UPDATE %s"
5959
" SET iv=X'%s', arsn=X'%s'"
6060
" WHERE spi='%d' AND tfvn='%d' AND scid='%d' AND vcid='%d' AND mapid='%d'";
6161
static const char *SQL_SADB_UPDATE_IV_ARC_BY_SPI_NULL_IV =
62-
"UPDATE %ssecurity_associations"
62+
"UPDATE %s"
6363
" SET arsn=X'%s'"
6464
" WHERE spi='%d' AND tfvn='%d' AND scid='%d' AND vcid='%d' AND mapid='%d'";
6565

@@ -187,11 +187,13 @@ static int32_t sa_get_from_spi(uint16_t spi, SecurityAssociation_t **security_as
187187
int32_t status = CRYPTO_LIB_SUCCESS;
188188

189189
char spi_query[2048];
190-
char* table_prefix = Crypto_Get_MariaDB_Table_Prefix(MARIADB_TC_TABLE);
191-
snprintf(spi_query, sizeof(spi_query), SQL_SADB_GET_SA_BY_SPI, table_prefix, spi);
192-
193-
status = parse_sa_from_mysql_query(&spi_query[0], security_association);
194-
190+
char table[25];
191+
status = query_all_tables(&table);
192+
if (status == CRYPTO_LIB_SUCCESS)
193+
{
194+
snprintf(spi_query, sizeof(spi_query), SQL_SADB_GET_SA_BY_SPI, table, spi);
195+
status = parse_sa_from_mysql_query(&spi_query[0], security_association);
196+
}
195197
return status;
196198
}
197199
static int32_t sa_get_operational_sa_from_gvcid(uint8_t tfvn, uint16_t scid, uint16_t vcid, uint8_t mapid,
@@ -200,12 +202,16 @@ static int32_t sa_get_operational_sa_from_gvcid(uint8_t tfvn, uint16_t scid, uin
200202
int32_t status = CRYPTO_LIB_SUCCESS;
201203

202204
char gvcid_query[2048];
203-
char* table_prefix = Crypto_Get_MariaDB_Table_Prefix(MARIADB_TC_TABLE);
204-
snprintf(gvcid_query, sizeof(gvcid_query), SQL_SADB_GET_SA_BY_GVCID, table_prefix, tfvn, scid, vcid, mapid,
205-
SA_OPERATIONAL);
206205

207-
status = parse_sa_from_mysql_query(&gvcid_query[0], security_association);
206+
char table[25];
207+
status = query_all_tables(&table);
208+
if (status == CRYPTO_LIB_SUCCESS)
209+
{
210+
snprintf(gvcid_query, sizeof(gvcid_query), SQL_SADB_GET_SA_BY_GVCID, table_prefix, tfvn, scid, vcid, mapid,
211+
SA_OPERATIONAL);
208212

213+
status = parse_sa_from_mysql_query(&gvcid_query[0], security_association);
214+
}
209215
return status;
210216
}
211217
static int32_t sa_save_sa(SecurityAssociation_t *sa)
@@ -226,41 +232,44 @@ static int32_t sa_save_sa(SecurityAssociation_t *sa)
226232

227233
char *arsn_h = malloc(sa->arsn_len * 2 + 1);
228234
convert_byte_array_to_hexstring(sa->arsn, sa->arsn_len, arsn_h);
229-
char* table_prefix = Crypto_Get_MariaDB_Table_Prefix(MARIADB_TC_TABLE);
230-
231-
if (sa->iv != NULL)
235+
// insert table queries here, store in variable = table that returned correct response
236+
char table[25];
237+
status = query_all_tables(&table);
238+
if (status == CRYPTO_LIB_SUCCESS)
232239
{
233-
snprintf(update_sa_query, sizeof(update_sa_query), SQL_SADB_UPDATE_IV_ARC_BY_SPI, table_prefix, iv_h, arsn_h,
234-
sa->spi, sa->gvcid_blk.tfvn, sa->gvcid_blk.scid, sa->gvcid_blk.vcid, sa->gvcid_blk.mapid);
240+
if (sa->iv != NULL)
241+
{
242+
snprintf(update_sa_query, sizeof(update_sa_query), SQL_SADB_UPDATE_IV_ARC_BY_SPI, table_prefix, iv_h, arsn_h,
243+
sa->spi, sa->gvcid_blk.tfvn, sa->gvcid_blk.scid, sa->gvcid_blk.vcid, sa->gvcid_blk.mapid);
235244

236-
free(iv_h);
237-
}
238-
else
239-
{
240-
snprintf(update_sa_query, sizeof(update_sa_query), SQL_SADB_UPDATE_IV_ARC_BY_SPI_NULL_IV, table_prefix, arsn_h,
241-
sa->spi, sa->gvcid_blk.tfvn, sa->gvcid_blk.scid, sa->gvcid_blk.vcid, sa->gvcid_blk.mapid);
242-
free(iv_h);
243-
}
245+
free(iv_h);
246+
}
247+
else
248+
{
249+
snprintf(update_sa_query, sizeof(update_sa_query), SQL_SADB_UPDATE_IV_ARC_BY_SPI_NULL_IV, table_prefix, arsn_h,
250+
sa->spi, sa->gvcid_blk.tfvn, sa->gvcid_blk.scid, sa->gvcid_blk.vcid, sa->gvcid_blk.mapid);
251+
free(iv_h);
252+
}
244253

245-
free(arsn_h);
254+
free(arsn_h);
246255
#ifdef SA_DEBUG
247-
fprintf(stderr, "MySQL Insert SA Query: %s \n", update_sa_query);
256+
fprintf(stderr, "MySQL Insert SA Query: %s \n", update_sa_query);
248257
#endif
249258

250-
// Crypto_saPrint(sa);
251-
if (mysql_query(con, update_sa_query))
252-
{
253-
status = finish_with_error(&con, SADB_QUERY_FAILED);
259+
// Crypto_saPrint(sa);
260+
if (mysql_query(con, update_sa_query))
261+
{
262+
status = finish_with_error(&con, SADB_QUERY_FAILED);
263+
}
264+
// todo - if query fails, need to push failure message to error stack instead of just return code.
265+
266+
// We free the allocated SA memory in the save function.
267+
if (sa->ek_ref[0] != '\0')
268+
clean_ekref(sa);
269+
if (sa->ak_ref[0] != '\0')
270+
clean_akref(sa);
271+
free(sa);
254272
}
255-
// todo - if query fails, need to push failure message to error stack instead of just return code.
256-
257-
// We free the allocated SA memory in the save function.
258-
if (sa->ek_ref[0] != '\0')
259-
clean_ekref(sa);
260-
if (sa->ak_ref[0] != '\0')
261-
clean_akref(sa);
262-
free(sa);
263-
264273
return status;
265274
}
266275
// Security Association Utility Functions
@@ -588,4 +597,41 @@ static int32_t finish_with_error(MYSQL **con_loc, int err)
588597
mysql_close(*con_loc);
589598
*con_loc = NULL;
590599
return err;
600+
}
601+
602+
static int32_t query_all_tables(char* table)
603+
{
604+
int32_t status = 0;
605+
char gvcid_query[2048];
606+
607+
char *tables[] = {MARIADB_TC_TABLE_PREFIX, MARIADB_TM_TABLE_PREFIX, MARIADB_AOS_TABLE_PREFIX};
608+
char *mapid[] = {TYPE_TC , TYPE_TM , TYPE_AOS};
609+
for (int i = 0; i <= 2; i++)
610+
{
611+
snprintf(gvcid_query, sizeof(gvcid_query), SQL_SADB_GET_SA_BY_GVCID, tables[i], current_managed_parameters_struct.tfvn, current_managed_parameters_struct.scid, current_managed_parameters_struct.vcid, mapid[i],
612+
SA_OPERATIONAL);
613+
614+
MYSQL_RES *result = mysql_store_result(con);
615+
616+
int num_rows = mysql_num_rows(result);
617+
if (num_rows == 0)
618+
{
619+
continue;
620+
}
621+
else
622+
{
623+
if (status == CRYPTO_LIB_SUCCESS)
624+
{
625+
//Collision
626+
return CRYPTO_LIB_ERROR;
627+
}
628+
else
629+
{
630+
status = CRYPTO_LIB_SUCCESS;
631+
table = tables[i];
632+
}
633+
}
634+
}
635+
636+
return status;
591637
}

0 commit comments

Comments
 (0)