internal static void PrintCommandss(Tpm2 tpm) { try { ICapabilitiesUnion caps; tpm.GetCapability(Cap.TpmProperties, (uint)Pt.TotalCommands, 1, out caps); tpm.GetCapability(Cap.Commands, (uint)TpmCc.First, TpmCc.Last - TpmCc.First + 1, out caps); var commands = (CcaArray)caps; Console.WriteLine("Supported commands:"); List <TpmCc> implementedCc = new List <TpmCc>(); foreach (var attr in commands.commandAttributes) { var commandCode = (TpmCc)((uint)attr & 0x0000FFFFU); implementedCc.Add(commandCode); Console.WriteLine(" {0}", commandCode.ToString()); } Console.WriteLine("Commands from spec not implemented:"); foreach (var cc in Enum.GetValues(typeof(TpmCc))) { if (!implementedCc.Contains((TpmCc)cc)) { Console.WriteLine(" {0}", cc.ToString()); } } } catch (Exception ex) { Console.WriteLine(ex); } }
public static List <AsaNvIndex> DumpNV(Tpm2 tpm) { var output = new List <AsaNvIndex>(); if (tpm == null) { return(output); } byte moreData; do { uint maxHandles = ushort.MaxValue; moreData = tpm.GetCapability(Cap.Handles, ((uint)Ht.NvIndex) << 24, maxHandles, out ICapabilitiesUnion cap); HandleArray handles = (HandleArray)cap; foreach (TpmHandle hh in handles.handle) { NvPublic nvPub = tpm.NvReadPublic(hh, out byte[] nvName); var index = new AsaNvIndex() { Index = hh.handle & 0x00FFFFFF, Attributes = nvPub.attributes }; // We can read with just the owner auth if (nvPub.attributes.HasFlag(NvAttr.Ownerread)) { try { index.value = tpm.NvRead(TpmRh.Owner, hh, nvPub.dataSize, 0).ToList(); } catch (TpmException e) { Log.Verbose("Dumping NV {0} failed ({1}:{2})", hh.handle & 0x00FFFFFF, e.GetType(), e.Message); } } // TODO: Attempt with auth values if DA is disabled output.Add(index); } } while (moreData == 1); return(output); }
internal static void PrintAlgorithms(Tpm2 tpm) { try { ICapabilitiesUnion caps; tpm.GetCapability(Cap.Algs, 0, 1000, out caps); var algsx = (AlgPropertyArray)caps; Console.WriteLine("Supported algorithms:"); foreach (var alg in algsx.algProperties) { Console.WriteLine(" {0}", alg.alg.ToString()); } } catch (Exception ex) { Console.WriteLine(ex); } }
public static TpmHandle[] GetLoadedEntities(Tpm2 tpm, Ht rangeToQuery) { uint maxHandles = UInt32.MaxValue; ICapabilitiesUnion h = null; byte moreData = tpm.GetCapability(Cap.Handles, ((uint)rangeToQuery) << 24, maxHandles, out h); if (moreData != 0) { throw new NotImplementedException( "GetLoadedEntities: Too much data returned"); } if (h.GetType() != typeof(HandleArray)) { throw new Exception( "GetLoadedEntities: Incorrect capability type requested"); } return((h as HandleArray).handle); }
public static Dictionary <(TpmAlgId, uint), byte[]> DumpPCRs(Tpm2 tpm) { var output = new Dictionary <(TpmAlgId, uint), byte[]>(); if (tpm == null) { return(output); } // Get which PCRs are supported tpm.GetCapability(Cap.Pcrs, 0, 255, out ICapabilitiesUnion caps); PcrSelection[] pcrs = ((PcrSelectionArray)caps).pcrSelections; foreach (var selection in pcrs) { // Dump each PCR bank foreach (var pcrVal in DumpPCRs(tpm, selection.hash, new PcrSelection[] { new PcrSelection(selection.hash, selection.GetSelectedPcrs()) })) { output.Add(pcrVal.Key, pcrVal.Value); } } return(output); }
/// <summary> /// Executes the GetCapabilities functionality. After parsing arguments, the /// function connects to the selected TPM device and invokes the GetCapabilities /// command on that connection. If the command was successful, the retrieved /// capabilities are displayed. /// </summary> /// <param name="args">Arguments to this program.</param> static void Main(string[] args) { // // Parse the program arguments. If the wrong arguments are given or // are malformed, then instructions for usage are displayed and // the program terminates. // string tpmDeviceName; if (!ParseArguments(args, out tpmDeviceName)) { WriteUsage(); return; } try { // // Create the device according to the selected connection. // Tpm2Device tpmDevice; switch (tpmDeviceName) { case DeviceSimulator: tpmDevice = new TcpTpmDevice(DefaultSimulatorName, DefaultSimulatorPort); break; case DeviceWinTbs: tpmDevice = new TbsDevice(); break; default: throw new Exception("Unknown device selected."); } // // Connect to the TPM device. This function actually establishes the // connection. // tpmDevice.Connect(); // // Pass the device object used for communication to the TPM 2.0 object // which provides the command interface. // var tpm = new Tpm2(tpmDevice); if (tpmDevice is TcpTpmDevice) { // // If we are using the simulator, we have to do a few things the // firmware would usually do. These actions have to occur after // the connection has been established. // tpmDevice.PowerCycle(); tpm.Startup(Su.Clear); } // // Query different capabilities // ICapabilitiesUnion caps; tpm.GetCapability(Cap.Algs, 0, 1000, out caps); var algsx = (AlgPropertyArray)caps; Console.WriteLine("Supported algorithms:"); foreach (var alg in algsx.algProperties) { Console.WriteLine(" {0}", alg.alg.ToString()); } Console.WriteLine("Supported commands:"); tpm.GetCapability(Cap.TpmProperties, (uint)Pt.TotalCommands, 1, out caps); tpm.GetCapability(Cap.Commands, (uint)TpmCc.First + 1, TpmCc.Last - TpmCc.First + 1, out caps); var commands = (CcaArray)caps; List <TpmCc> implementedCc = new List <TpmCc>(); foreach (var attr in commands.commandAttributes) { var commandCode = (TpmCc)((uint)attr & 0x0000FFFFU); // // Filter placehoder(s) // if (commandCode == TpmCc.None) { continue; } implementedCc.Add(commandCode); Console.WriteLine(" {0}", commandCode.ToString()); } Console.WriteLine("Commands from spec not implemented:"); foreach (var cc in Enum.GetValues(typeof(TpmCc))) { if (!implementedCc.Contains((TpmCc)cc) && // // Fiter placeholder(s) // ((TpmCc)cc != TpmCc.None) && ((TpmCc)cc != TpmCc.First) && ((TpmCc)cc != TpmCc.Last)) { Console.WriteLine(" {0}", cc.ToString()); } } // // As an alternative: call GetCapabilities more than once to obtain all values // byte more; var firstCommandCode = (uint)TpmCc.None; do { more = tpm.GetCapability(Cap.Commands, firstCommandCode, 10, out caps); commands = (CcaArray)caps; // // Commands are sorted; getting the last element as it will be the largest. // uint lastCommandCode = (uint)commands.commandAttributes[commands.commandAttributes.Length - 1] & 0x0000FFFFU; firstCommandCode = lastCommandCode; } while (more == 1); // // Read PCR attributes. Cap.Pcrs returns the list of PCRs which are supported // in different PCR banks. The PCR banks are identified by the hash algorithm // used to extend values into the PCRs of this bank. // tpm.GetCapability(Cap.Pcrs, 0, 255, out caps); PcrSelection[] pcrs = ((PcrSelectionArray)caps).pcrSelections; Console.WriteLine(); Console.WriteLine("Available PCR banks:"); foreach (PcrSelection pcrBank in pcrs) { var sb = new StringBuilder(); sb.AppendFormat("PCR bank for algorithm {0} has registers at index:", pcrBank.hash); sb.AppendLine(); foreach (uint selectedPcr in pcrBank.GetSelectedPcrs()) { sb.AppendFormat("{0},", selectedPcr); } Console.WriteLine(sb); } // // Read PCR attributes. Cap.PcrProperties checks for certain properties of each PCR register. // tpm.GetCapability(Cap.PcrProperties, 0, 255, out caps); Console.WriteLine(); Console.WriteLine("PCR attributes:"); TaggedPcrSelect[] pcrProperties = ((TaggedPcrPropertyArray)caps).pcrProperty; foreach (TaggedPcrSelect pcrProperty in pcrProperties) { if ((PtPcr)pcrProperty.tag == PtPcr.None) { continue; } uint pcrIndex = 0; var sb = new StringBuilder(); sb.AppendFormat("PCR property {0} supported by these registers: ", (PtPcr)pcrProperty.tag); sb.AppendLine(); foreach (byte pcrBitmap in pcrProperty.pcrSelect) { for (int i = 0; i < 8; i++) { if ((pcrBitmap & (1 << i)) != 0) { sb.AppendFormat("{0},", pcrIndex); } pcrIndex++; } } Console.WriteLine(sb); } // // Clean up. // tpm.Dispose(); } catch (Exception e) { Console.WriteLine("Exception occurred: {0}", e.Message); } Console.WriteLine("Press Any Key to continue."); Console.ReadLine(); }
internal static void AvailablePCRBanks(Tpm2 tpm) { try { // // Read PCR attributes. Cap.Pcrs returns the list of PCRs which are supported // in different PCR banks. The PCR banks are identified by the hash algorithm // used to extend values into the PCRs of this bank. // ICapabilitiesUnion caps; tpm.GetCapability(Cap.Pcrs, 0, 255, out caps); PcrSelection[] pcrs = ((PcrSelectionArray)caps).pcrSelections; Console.WriteLine(); Console.WriteLine("Available PCR banks:"); foreach (PcrSelection pcrBank in pcrs) { var sb = new StringBuilder(); sb.AppendFormat("PCR bank for algorithm {0} has registers at index:", pcrBank.hash); sb.AppendLine(); foreach (uint selectedPcr in pcrBank.GetSelectedPcrs()) { sb.AppendFormat("{0},", selectedPcr); } Console.WriteLine(sb); } // // Read PCR attributes. Cap.PcrProperties checks for certain properties of each PCR register. // tpm.GetCapability(Cap.PcrProperties, 0, 255, out caps); Console.WriteLine(); Console.WriteLine("PCR attributes:"); TaggedPcrSelect[] pcrProperties = ((TaggedPcrPropertyArray)caps).pcrProperty; foreach (TaggedPcrSelect pcrProperty in pcrProperties) { if ((PtPcr)pcrProperty.tag == PtPcr.None) { continue; } uint pcrIndex = 0; var sb = new StringBuilder(); sb.AppendFormat("PCR property {0} supported by these registers: ", (PtPcr)pcrProperty.tag); sb.AppendLine(); foreach (byte pcrBitmap in pcrProperty.pcrSelect) { for (int i = 0; i < 8; i++) { if ((pcrBitmap & (1 << i)) != 0) { sb.AppendFormat("{0},", pcrIndex); } pcrIndex++; } } Console.WriteLine(sb); } } catch (Exception ex) { Console.WriteLine(ex); } }
private void RetrieveTpmProperties() { WriteConsoleVerbose("Retrieving TPM properties ..."); using (var tpmDevice = new TbsDevice()) { WriteConsoleDebug("Connecting to TPM ..."); tpmDevice.Connect(); using (var tpm = new Tpm2(tpmDevice)) { // ReSharper disable JoinDeclarationAndInitializer uint tpmProperty; TaggedTpmPropertyArray tpmProperties; // ReSharper enable JoinDeclarationAndInitializer WriteConsoleDebug("Retrieving TPM capability: TPM_PROPERTIES (Property: PT_FIXED)"); tpm.GetCapability(Cap.TpmProperties, (uint)Pt.PtFixed, 1000, out var capPropertiesFixed); tpmProperties = (TaggedTpmPropertyArray)capPropertiesFixed; #region Manufacturer ManufacturerId = tpmProperties.tpmProperty[Pt.Manufacturer - Pt.PtFixed].value; var tpmManufacturerBytes = BitConverter.GetBytes(ManufacturerId); Array.Reverse(tpmManufacturerBytes); // Assumes little-endian var tpmManufacturerName = new char[tpmManufacturerBytes.Length]; for (var index = 0; index < tpmManufacturerName.Length; index++) { // Unprintable character or invalid 7-bit ASCII if (tpmManufacturerBytes[index] < 32 || tpmManufacturerBytes[index] > 126) { break; } tpmManufacturerName[index] = Convert.ToChar(tpmManufacturerBytes[index]); } ManufacturerName = new string(tpmManufacturerName).Trim(); ManufacturerModel = tpmProperties.tpmProperty[Pt.VendorTpmType - Pt.PtFixed].value; #endregion #region Specification tpmProperty = tpmProperties.tpmProperty[Pt.FamilyIndicator - Pt.PtFixed].value; var tpmFamilyIndicator = BitConverter.GetBytes(tpmProperty); Array.Reverse(tpmFamilyIndicator); // Assumes little-endian SpecificationVersion = Encoding.ASCII.GetString(tpmFamilyIndicator).Trim('\0'); SpecificationLevel = tpmProperties.tpmProperty[Pt.Level - Pt.PtFixed].value; tpmProperty = tpmProperties.tpmProperty[Pt.Revision - Pt.PtFixed].value; SpecificationRevision = (float)tpmProperty / 100; tpmProperty = tpmProperties.tpmProperty[Pt.Year - Pt.PtFixed].value; SpecificationDate = new DateTime((int)tpmProperty - 1, 12, 31); tpmProperty = tpmProperties.tpmProperty[Pt.DayOfYear - Pt.PtFixed].value; SpecificationDate = SpecificationDate.AddDays(tpmProperty); #endregion #region Platform-specific tpmProperty = tpmProperties.tpmProperty[Pt.PsFamilyIndicator - Pt.PtFixed].value; PlatformSpecificFamily = ((Ps)tpmProperty).ToString(); PlatformSpecificationLevel = tpmProperties.tpmProperty[Pt.PsLevel - Pt.PtFixed].value; tpmProperty = tpmProperties.tpmProperty[Pt.PsRevision - Pt.PtFixed].value; PlatformSpecificationRevision = (float)tpmProperty / 100; tpmProperty = tpmProperties.tpmProperty[Pt.PsYear - Pt.PtFixed].value; PlatformSpecificationDate = new DateTime((int)tpmProperty - 1, 12, 31); tpmProperty = tpmProperties.tpmProperty[Pt.PsDayOfYear - Pt.PtFixed].value; PlatformSpecificationDate = SpecificationDate.AddDays(tpmProperty); #endregion #region Firmware var tpmFirmwareVersion = new uint[2]; tpmFirmwareVersion[0] = tpmProperties.tpmProperty[Pt.FirmwareVersion1 - Pt.PtFixed].value; tpmFirmwareVersion[1] = tpmProperties.tpmProperty[Pt.FirmwareVersion2 - Pt.PtFixed].value; FirmwareVersion = new Version((int)(tpmFirmwareVersion[0] >> 16), (int)(tpmFirmwareVersion[0] & 0xFFFF), (int)tpmFirmwareVersion[1] >> 16, (int)tpmFirmwareVersion[1] & 0xFFFF); #endregion #region Characteristics tpmProperty = tpmProperties.tpmProperty[Pt.Memory - Pt.PtFixed].value; var tpmMemory = (MemoryAttr)tpmProperty; MemoryManagement = tpmMemory.ToString(); tpmProperty = tpmProperties.tpmProperty[Pt.Modes - Pt.PtFixed].value; var tpmModes = (ModesAttr)tpmProperty; SupportedModes = tpmModes.ToString(); #endregion WriteConsoleDebug("Retrieving TPM capability: TPM_PROPERTIES (Property: PT_VAR)"); tpm.GetCapability(Cap.TpmProperties, (uint)Pt.PtVar, 1000, out var capPropertiesVar); tpmProperties = (TaggedTpmPropertyArray)capPropertiesVar; #region Configuration tpmProperty = tpmProperties.tpmProperty[Pt.Permanent - Pt.PtVar].value; var tpmPermanent = (PermanentAttr)tpmProperty; PermanentAttributes = tpmPermanent.ToString(); tpmProperty = tpmProperties.tpmProperty[Pt.StartupClear - Pt.PtVar].value; var tpmStartupClear = (StartupClearAttr)tpmProperty; StartupAttributes = tpmStartupClear.ToString(); #endregion } } }
static void Main(string[] args) { Console.WriteLine("Hello World!"); using (var client = TpmClient.CreateSimulatorClient()) { var helloWorld = Encoding.UTF8.GetBytes("Hello World"); var cipher = RsaExamples.RsaEncrypt(client.Tpm, helloWorld); var decipher = RsaExamples.RsaDecrypt(client.Tpm, cipher); var helloWorld2 = Encoding.UTF8.GetString(decipher); return; Examples.PrintCommandss(client.Tpm); Examples.CreateTwoPrimaries(client.Tpm); Examples.EncryptDecrypt(client.Tpm); return; var r = client.Tpm.GetRandom(10); Console.WriteLine(client.Tpm._GetUnderlyingDevice().HasRM()); CreateRsaPrimaryKey(client.Tpm, false); Examples.NVReadWrite(client.Tpm); Examples.NVCounter(client.Tpm); Examples.AvailablePCRBanks(client.Tpm); Examples.PrintAlgorithms(client.Tpm); } return; using (var client = TpmClient.CreateDeviceClient()) { Examples.AvailablePCRBanks(client.Tpm); Examples.PrintAlgorithms(client.Tpm); Examples.PrintCommandss(client.Tpm); } return; Sign(); return; var data = Encoding.UTF8.GetBytes("Hello World"); Examples.SaveValueIntoTpm(3001, data, data.Length, _authValue); Examples.ReadValueFromTpm(3001, data.Length, _authValue); return; Examples.GetHardwareDeviceName(); return; Examples.GetDeviceId(); return; Examples.ConnectLocal(); Examples.ConnectSimulator(); using (var device = Examples.Connect(useSimulator)) { //Examples.AvailablePCRBanks(device); } return; ReadPcr(); Sign(); try { using (Tpm2Device tpmDevice = new TbsDevice()) { tpmDevice.Connect(); using (var tpm = new Tpm2(tpmDevice)) { ICapabilitiesUnion caps; tpm.GetCapability(Cap.Algs, 0, 1000, out caps); var algsx = (AlgPropertyArray)caps; Console.WriteLine("Supported algorithms:"); foreach (var alg in algsx.algProperties) { Console.WriteLine(" {0}", alg.alg.ToString()); } Console.WriteLine("Supported commands:"); tpm.GetCapability(Cap.TpmProperties, (uint)Pt.TotalCommands, 1, out caps); tpm.GetCapability(Cap.Commands, (uint)TpmCc.First, TpmCc.Last - TpmCc.First + 1, out caps); var commands = (CcaArray)caps; List <TpmCc> implementedCc = new List <TpmCc>(); foreach (var attr in commands.commandAttributes) { var commandCode = (TpmCc)((uint)attr & 0x0000FFFFU); implementedCc.Add(commandCode); Console.WriteLine(" {0}", commandCode.ToString()); } Console.WriteLine("Commands from spec not implemented:"); foreach (var cc in Enum.GetValues(typeof(TpmCc))) { if (!implementedCc.Contains((TpmCc)cc)) { Console.WriteLine(" {0}", cc.ToString()); } } // // As an alternative: call GetCapabilities more than once to obtain all values // byte more; var firstCommandCode = (uint)TpmCc.First; do { more = tpm.GetCapability(Cap.Commands, firstCommandCode, 10, out caps); commands = (CcaArray)caps; // // Commands are sorted; getting the last element as it will be the largest. // uint lastCommandCode = (uint)commands.commandAttributes[commands.commandAttributes.Length - 1] & 0x0000FFFFU; firstCommandCode = lastCommandCode; } while (more == 1); // // Read PCR attributes. Cap.Pcrs returns the list of PCRs which are supported // in different PCR banks. The PCR banks are identified by the hash algorithm // used to extend values into the PCRs of this bank. // tpm.GetCapability(Cap.Pcrs, 0, 255, out caps); PcrSelection[] pcrs = ((PcrSelectionArray)caps).pcrSelections; Console.WriteLine(); Console.WriteLine("Available PCR banks:"); foreach (PcrSelection pcrBank in pcrs) { var sb = new StringBuilder(); sb.AppendFormat("PCR bank for algorithm {0} has registers at index:", pcrBank.hash); sb.AppendLine(); foreach (uint selectedPcr in pcrBank.GetSelectedPcrs()) { sb.AppendFormat("{0},", selectedPcr); } Console.WriteLine(sb); } // // Read PCR attributes. Cap.PcrProperties checks for certain properties of each PCR register. // tpm.GetCapability(Cap.PcrProperties, 0, 255, out caps); Console.WriteLine(); Console.WriteLine("PCR attributes:"); TaggedPcrSelect[] pcrProperties = ((TaggedPcrPropertyArray)caps).pcrProperty; foreach (TaggedPcrSelect pcrProperty in pcrProperties) { if ((PtPcr)pcrProperty.tag == PtPcr.None) { continue; } uint pcrIndex = 0; var sb = new StringBuilder(); sb.AppendFormat("PCR property {0} supported by these registers: ", (PtPcr)pcrProperty.tag); sb.AppendLine(); foreach (byte pcrBitmap in pcrProperty.pcrSelect) { for (int i = 0; i < 8; i++) { if ((pcrBitmap & (1 << i)) != 0) { sb.AppendFormat("{0},", pcrIndex); } pcrIndex++; } } Console.WriteLine(sb); } } } } catch (Exception ex) { Console.WriteLine(ex); } Console.ReadKey(); }