/// <summary>
/// This TBS returns a random handle value in the desired handle range (ugh).
/// </summary>
/// <param name="owner"></param>
/// <param name="tpmHandle"></param>
/// <returns></returns>
private uint GetFreeHandle(Tbs.TbsContext owner, TpmHandle tpmHandle)
{
Tbs.SlotType neededType = Tbs.SlotTypeFromHandle(tpmHandle);
if (neededType == Tbs.SlotType.NoSlot)
{
return(tpmHandle.handle);
}
int numTries = 0;
while (true)
{
Ht handleType = tpmHandle.GetType();
var randomPos = (uint)Globs.GetRandomInt((int)TpmHandle.GetRangeLength(tpmHandle.GetType()));
uint candidateHandle = ((uint)handleType << 24) + randomPos;
if (!OwnerHandleInUse(owner, candidateHandle))
{
return(candidateHandle);
}
numTries++;
if (numTries >= 1000)
{
break;
}
}
throw new Exception("Too many TBS contexts");
}