/// <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 static SlotType SlotTypeFromHandle(TpmHandle h) { switch (h.GetType()) { case Ht.Transient: return(SlotType.ObjectSlot); case Ht.PolicySession: case Ht.HmacSession: return(SlotType.SessionSlot); default: return(SlotType.NoSlot); } }
/// <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 static SlotType SlotTypeFromHandle(TpmHandle h) { switch (h.GetType()) { case Ht.Transient: return SlotType.ObjectSlot; case Ht.PolicySession: case Ht.HmacSession: return SlotType.SessionSlot; default: return SlotType.NoSlot; } }
internal void Print(string name, string type, Object o) { if (o == null) { // E.g. inPrivate null SomeStruct AddLine(B, "{0}@{1}#{2}", name, "null", type); return; } // ReSharper disable once CanBeReplacedWithTryCastAndCheckForNull if (o is TpmStructureBase) { string ss = type; if (ss.StartsWith("I")) { // If the member is an interface, also print the type of entity being dumped string intType = o.GetType().ToString(); intType = intType.Substring(intType.LastIndexOf('.') + 1); type = intType; } if (o is TpmHandle) { TpmHandle tpmHandle = o as TpmHandle; string handleName = tpmHandle.GetType().ToString(); if (tpmHandle.GetType() == Ht.Permanent) { handleName += " -" + ((TpmRh)tpmHandle.handle).ToString(); } name = string.Format("({0})", handleName); } // Print name and type but not the contents (printed recursively later) AddLine(B, "{0}@-#{1}", name, type); // Recurse Indent++; ((TpmStructureBase)o).ToStringInternal(this); Indent--; return; } // ReSharper disable once CanBeReplacedWithTryCastAndCheckForNull if (o is Enum) { var en = (Enum)o; string s = Enum.Format(en.GetType(), en, "g"); s = s.Replace(',', '|'); // name Elem1|Elem2 AddLine(B, "{0}@{1}#{2}", name, s, type); return; } if (o is ValueType) { //checked that this actually works with Int64, etc. var val = o is UInt64 ? (Int64)Convert.ToUInt64(o) : Convert.ToInt64(o); string hexString = Convert.ToString(val, 16); // ReSharper disable once SpecifyACultureInStringConversionExplicitly AddLine(B, "{0}@{1} (0x{2})#{3}", name, o.ToString(), hexString, type); return; } // ReSharper disable once CanBeReplacedWithTryCastAndCheckForNull if (o is Array) { var a = (Array)o; Type elementType = o.GetType().GetElementType(); if (elementType == typeof(byte)) { // Byte arrays as special - string hexString = "0x" + Globs.HexFromByteArray((byte[])a, 8); string typeString = String.Format("byte[{0}]", a.Length); AddLine(B, "{0}@{1}#{2}", name, hexString, typeString); return; } // ReSharper disable once RedundantIfElseBlock else { B.AppendFormat("{0}Array - {1}[{2}]\n", Spaces(), type, a.Length); Indent++; for (int j = 0; j < a.Length; j++) { Object elem = a.GetValue(j); // ReSharper disable once SpecifyACultureInStringConversionExplicitly Print(elem.GetType().ToString(), j.ToString(), elem); } Indent--; return; } } Globs.Throw <NotImplementedException>("Print: Unknown type " + o.GetType()); }