Skip to content

Commit 5f8a9a4

Browse files
committed
refactor credmgmt
1 parent 8aa1f4a commit 5f8a9a4

File tree

1 file changed

+16
-62
lines changed

1 file changed

+16
-62
lines changed

fido2/ctap.c

Lines changed: 16 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1524,46 +1524,6 @@ static int credentialId_to_rk_index(CredentialId * credId){
15241524
return -1;
15251525
}
15261526

1527-
// Return 1 if Left(rpIdHash, 16) has been counted in rpHashes.
1528-
static int8_t _rk_counted(uint8_t rpHashes [50][16], uint8_t * hash, int unique_count)
1529-
{
1530-
int i = 0;
1531-
for (; i < unique_count; i++)
1532-
{
1533-
if (memcmp(rpHashes[i], hash, 16) == 0) {
1534-
return 1;
1535-
}
1536-
}
1537-
return 0;
1538-
}
1539-
1540-
static uint8_t count_unique_rks()
1541-
{
1542-
CTAP_residentKey rk;
1543-
unsigned int unique_count = 0;
1544-
unsigned int i;
1545-
uint8_t rpHashes [50][16];
1546-
memset(rpHashes, 0, sizeof(rpHashes));
1547-
1548-
for(i = 0; i < ctap_rk_size(); i++)
1549-
{
1550-
ctap_load_rk(i, &rk);
1551-
if ( ctap_rk_is_valid(&rk) )
1552-
{
1553-
if (! _rk_counted(rpHashes, rk.id.rpIdHash, unique_count))
1554-
{
1555-
memmove(rpHashes[unique_count], rk.id.rpIdHash, 16);
1556-
unique_count += 1;
1557-
if (unique_count >= ctap_rk_size())
1558-
{
1559-
return unique_count;
1560-
}
1561-
}
1562-
}
1563-
}
1564-
return unique_count;
1565-
}
1566-
15671527
// Load the next valid resident key of a different rpIdHash
15681528
static int scan_for_next_rp(int index){
15691529
CTAP_residentKey rk;
@@ -1661,13 +1621,11 @@ uint8_t ctap_cred_mgmt(CborEncoder * encoder, uint8_t * request, int length)
16611621
static int curr_rp_ind = 0;
16621622
static int curr_rk_ind = 0;
16631623

1664-
// flag that authenticated RPBegin was received
1624+
// flags that authenticate whether *Begin was before *Next
16651625
static bool rp_auth = false;
1666-
// flag that authenticated RKBegin was received
16671626
static bool rk_auth = false;
1668-
// number of stored RPs
1627+
16691628
int rp_count = 0;
1670-
// number of RKs with the specified rpIdHash
16711629
int rk_count = 0;
16721630

16731631
int ret = ctap_parse_cred_mgmt(&CM, request, length);
@@ -1686,10 +1644,20 @@ uint8_t ctap_cred_mgmt(CborEncoder * encoder, uint8_t * request, int length)
16861644
if (CM.cmd == CM_cmdRPBegin)
16871645
{
16881646
curr_rk_ind = -1;
1689-
curr_rp_ind = scan_for_next_rp(-1);
1690-
rp_count = count_unique_rks();
16911647
rp_auth = true;
16921648
rk_auth = false;
1649+
curr_rp_ind = scan_for_next_rp(-1);
1650+
1651+
// Count total unique RP's
1652+
while (curr_rp_ind >= 0)
1653+
{
1654+
curr_rp_ind = scan_for_next_rp(curr_rp_ind);
1655+
rp_count++;
1656+
}
1657+
1658+
// Reset scan
1659+
curr_rp_ind = scan_for_next_rp(-1);
1660+
16931661
printf1(TAG_MC, "RP Begin @%d. %d total.\n", curr_rp_ind, rp_count);
16941662
}
16951663
else if (CM.cmd == CM_cmdRKBegin)
@@ -1716,17 +1684,6 @@ uint8_t ctap_cred_mgmt(CborEncoder * encoder, uint8_t * request, int length)
17161684
curr_rp_ind = -1;
17171685
}
17181686

1719-
if (CM.cmd == CM_cmdRPNext && !rp_auth)
1720-
{
1721-
printf2(TAG_ERR, "RPNext without RPBegin\n");
1722-
return CTAP2_ERR_NO_CREDENTIALS;
1723-
}
1724-
if (CM.cmd == CM_cmdRKNext && !rk_auth)
1725-
{
1726-
printf2(TAG_ERR, "RKNext without RKBegin\n");
1727-
return CTAP2_ERR_NO_CREDENTIALS;
1728-
}
1729-
17301687
switch (CM.cmd)
17311688
{
17321689
case CM_cmdMetadata:
@@ -1737,7 +1694,7 @@ uint8_t ctap_cred_mgmt(CborEncoder * encoder, uint8_t * request, int length)
17371694
case CM_cmdRPBegin:
17381695
case CM_cmdRPNext:
17391696
printf1(TAG_CM, "Get RP %d\n", curr_rp_ind);
1740-
if (curr_rp_ind < 0) {
1697+
if (curr_rp_ind < 0 || !rp_auth) {
17411698
rp_auth = false;
17421699
rk_auth = false;
17431700
return CTAP2_ERR_NO_CREDENTIALS;
@@ -1751,7 +1708,7 @@ uint8_t ctap_cred_mgmt(CborEncoder * encoder, uint8_t * request, int length)
17511708
case CM_cmdRKBegin:
17521709
case CM_cmdRKNext:
17531710
printf1(TAG_CM, "Get Cred %d\n", curr_rk_ind);
1754-
if (curr_rk_ind < 0) {
1711+
if (curr_rk_ind < 0 || !rk_auth) {
17551712
rp_auth = false;
17561713
rk_auth = false;
17571714
return CTAP2_ERR_NO_CREDENTIALS;
@@ -1764,9 +1721,6 @@ uint8_t ctap_cred_mgmt(CborEncoder * encoder, uint8_t * request, int length)
17641721

17651722
break;
17661723
case CM_cmdRKDelete:
1767-
rp_auth = false;
1768-
rk_auth = false;
1769-
17701724
printf1(TAG_CM, "CM_cmdRKDelete\n");
17711725
i = credentialId_to_rk_index(&CM.subCommandParams.credentialDescriptor.credential.id);
17721726
if (i >= 0) {

0 commit comments

Comments
 (0)