@@ -48,18 +48,18 @@ static const char *SQL_SADB_GET_SA_BY_SPI =
48
48
"SELECT "
49
49
"spi,ekid,akid,sa_state,tfvn,scid,vcid,mapid,lpid,est,ast,shivf_len,shsnf_len,shplf_len,stmacf_len,ecs_len,HEX(ecs)"
50
50
",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'" ;
52
52
static const char * SQL_SADB_GET_SA_BY_GVCID =
53
53
"SELECT "
54
54
"spi,ekid,akid,sa_state,tfvn,scid,vcid,mapid,lpid,est,ast,shivf_len,shsnf_len,shplf_len,stmacf_len,ecs_len,HEX(ecs)"
55
55
",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'" ;
57
57
static const char * SQL_SADB_UPDATE_IV_ARC_BY_SPI =
58
- "UPDATE %ssecurity_associations "
58
+ "UPDATE %s "
59
59
" SET iv=X'%s', arsn=X'%s'"
60
60
" WHERE spi='%d' AND tfvn='%d' AND scid='%d' AND vcid='%d' AND mapid='%d'" ;
61
61
static const char * SQL_SADB_UPDATE_IV_ARC_BY_SPI_NULL_IV =
62
- "UPDATE %ssecurity_associations "
62
+ "UPDATE %s "
63
63
" SET arsn=X'%s'"
64
64
" WHERE spi='%d' AND tfvn='%d' AND scid='%d' AND vcid='%d' AND mapid='%d'" ;
65
65
@@ -187,11 +187,13 @@ static int32_t sa_get_from_spi(uint16_t spi, SecurityAssociation_t **security_as
187
187
int32_t status = CRYPTO_LIB_SUCCESS ;
188
188
189
189
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
+ }
195
197
return status ;
196
198
}
197
199
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
200
202
int32_t status = CRYPTO_LIB_SUCCESS ;
201
203
202
204
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 );
206
205
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 );
208
212
213
+ status = parse_sa_from_mysql_query (& gvcid_query [0 ], security_association );
214
+ }
209
215
return status ;
210
216
}
211
217
static int32_t sa_save_sa (SecurityAssociation_t * sa )
@@ -226,41 +232,44 @@ static int32_t sa_save_sa(SecurityAssociation_t *sa)
226
232
227
233
char * arsn_h = malloc (sa -> arsn_len * 2 + 1 );
228
234
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 )
232
239
{
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 );
235
244
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
+ }
244
253
245
- free (arsn_h );
254
+ free (arsn_h );
246
255
#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 );
248
257
#endif
249
258
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 );
254
272
}
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
-
264
273
return status ;
265
274
}
266
275
// Security Association Utility Functions
@@ -588,4 +597,41 @@ static int32_t finish_with_error(MYSQL **con_loc, int err)
588
597
mysql_close (* con_loc );
589
598
* con_loc = NULL ;
590
599
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 ;
591
637
}
0 commit comments