/// <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"); }
internal ObjectContext GetContext(Tbs.TbsContext caller, TpmHandle callerHandle) { if (Tbs.SlotTypeFromHandle(callerHandle) == Tbs.SlotType.NoSlot) { // Indicates that this is a TPM resident object (NV-slot, primary-handle, PWAP-handle, etc.) var temp = new ObjectContext { TheTpmHandle = callerHandle }; return(temp); } ObjectContext x = ObjectContexts.Find(item => (item.Owner == caller) && item.OwnerHandle.handle == callerHandle.handle); // Note that x may be null return(x); }
internal ObjectContext CreateObjectContext(Tbs.TbsContext owner, TpmHandle tpmHandle) { Tbs.SlotType newSlotType = Tbs.SlotTypeFromHandle(tpmHandle); if (newSlotType == Tbs.SlotType.NoSlot) { throw new Exception("should not be here"); } // Make a new slot context of the requisite type uint tbsHandle = GetFreeHandle(owner, tpmHandle); var newContext = new ObjectContext { OwnerHandle = new TpmHandle(tbsHandle), TheTpmHandle = tpmHandle, TheSlotType = newSlotType, LastUseCount = GetUseCount(), Loaded = true, Owner = owner }; ObjectContexts.Add(newContext); return(newContext); }
/// <summary> /// Remove all contexts associated with a client (to support client disconnect). /// </summary> /// <param name="owner"></param> public void RemoveAll(Tbs.TbsContext owner) { ObjectContexts.RemoveAll(item => item.Owner == owner); }
private bool OwnerHandleInUse(Tbs.TbsContext owner, uint ownerHandle) { return(ObjectContexts.Find(item => (item.Owner == owner && item.OwnerHandle.handle == ownerHandle)) != null); }