示例#1
0
        public SecurityProviderTpmSimulator(string registrationId)
            : base(registrationId)
        {
            _tcpTpmDevice = new TcpTpmDevice(SimulatorAddress, SimulatorPort);
            _tcpTpmDevice.Connect();
            _tcpTpmDevice.PowerCycle();

            _tpm2 = new Tpm2(_tcpTpmDevice);
            _tpm2.Startup(Su.Clear);

            _innerClient = new SecurityProviderTpmHsm(GetRegistrationID(), _tcpTpmDevice);
        }
        public SecurityProviderTpmSimulator(string registrationId) : base(registrationId)
        {
            _tpmDevice = new TcpTpmDevice(SimulatorAddress, SimulatorPort);
            _tpmDevice.Connect();
            _tpmDevice.SetSocketTimeout(TcpTpmDeviceTimeoutSeconds);
            _tpmDevice.PowerCycle();

            using (var tpm2 = new Tpm2(_tpmDevice))
            {
                tpm2.Startup(Su.Clear);
            }

            _innerClient = new SecurityProviderTpmHsm(GetRegistrationID(), _tpmDevice);
        }
示例#3
0
        public SecurityClientTpmSimulator(string registrationId) : base(registrationId)
        {
            var tpmDevice = new TcpTpmDevice(SimulatorAddress, SimulatorPort);

            tpmDevice.Connect();
            tpmDevice.PowerCycle();

            using (var tpm2 = new Tpm2(tpmDevice))
            {
                tpm2.Startup(Su.Clear);
            }

            _innerClient = new SecurityClientTpm(GetRegistrationID(), tpmDevice);
        }
示例#4
0
        internal static TpmClient CreateSimulatorClient()
        {
            Tpm2Device tpmDevice = new TcpTpmDevice(DefaultSimulatorName, DefaultSimulatorPort);

            tpmDevice.Connect();

            var tpm = new Tpm2(tpmDevice);

            tpmDevice.PowerCycle();
            tpm.Startup(Su.Clear);

            TpmClient client = new TpmClient(tpmDevice, tpm);

            return(client);
        }
示例#5
0
        private static Tpm2Device ConnectToTpmSimulator(string simulatorHost = "127.0.0.1", int simulatorPort = 2321)
        {
            var tpmDevice = new TcpTpmDevice(simulatorHost, simulatorPort);

            tpmDevice.Connect();
            tpmDevice.SetSocketTimeout(10);
            tpmDevice.PowerCycle();

            using (var tpm2 = new Tpm2(tpmDevice))
            {
                tpm2.Startup(Su.Clear);
            }

            return(tpmDevice);
        }
示例#6
0
        /// <summary>
        /// Executes the hashing functionality. After parsing arguments, the
        /// function connects to the selected TPM device and invokes the TPM
        /// commands on that connection.
        /// </summary>
        static void Main()
        {
            try
            {
                //
                // Create the device according to the selected connection.
                //
                Tpm2Device tpmDevice = new TcpTpmDevice(DefaultSimulatorName, DefaultSimulatorPort);
                //
                // 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 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);

                ResetDALogic(tpm);
                ResourceManager(tpm);
                PowerAndLocality(tpm);

                //
                // Clean up.
                //
                tpm.Dispose();
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception occurred: {0}", e.Message);
            }

            Console.WriteLine("Press Any Key to continue.");
            Console.ReadLine();
        }
示例#7
0
        /// <summary>
        /// Executes the hashing functionality. After parsing arguments, the 
        /// function connects to the selected TPM device and invokes the TPM
        /// commands on that connection.
        /// </summary>
        static void Main()
        {
            try
            {
                //
                // Create the device according to the selected connection.
                // 
                Tpm2Device tpmDevice = new TcpTpmDevice(DefaultSimulatorName, DefaultSimulatorPort);
                //
                // 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 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);

                ResetDALogic(tpm);
                ResourceManager(tpm);
                PowerAndLocality(tpm);

                //
                // Clean up.
                // 
                tpm.Dispose();
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception occurred: {0}", e.Message);
            }

            Console.WriteLine("Press Any Key to continue.");
            Console.ReadLine();
        }
        public void TestTpmCollector()
        {
            var PcrAlgorithm = TpmAlgId.Sha256;

            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                var process = TpmSim.GetTpmSimulator();
                process.Start();

                var  nvData  = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7 };
                uint nvIndex = 3001;

                var tpmc = new TpmCollector(new CollectorOptions()
                {
                    Verbose = true
                }, null, TestMode: true);
                // Prepare to write to NV 3001
                TpmHandle nvHandle = TpmHandle.NV(nvIndex);

                TcpTpmDevice tcpTpmDevice = new TcpTpmDevice("127.0.0.1", 2321, stopTpm: false);
                tcpTpmDevice.Connect();

                using var tpm = new Tpm2(tcpTpmDevice);
                tcpTpmDevice.PowerCycle();
                tpm.Startup(Su.Clear);

                try
                {
                    tpm._AllowErrors()
                    .NvUndefineSpace(TpmRh.Owner, nvHandle);

                    tpm.NvDefineSpace(TpmRh.Owner, null,
                                      new NvPublic(nvHandle, TpmAlgId.Sha1,
                                                   NvAttr.NoDa | NvAttr.Ownerread | NvAttr.Ownerwrite,
                                                   null, 32));

                    // Write to NV 3001
                    tpm.NvWrite(TpmRh.Owner, nvHandle, nvData, 0);

                    var nvOut = tpm.NvRead(TpmRh.Owner, nvHandle, (ushort)nvData.Length, 0);

                    Assert.IsTrue(nvOut.SequenceEqual(nvData));
                }
                catch (TpmException e)
                {
                    Log.Debug(e, "Failed to Write to NV.");
                    Assert.Fail();
                }

                // Verify that all the PCRs are blank to start with
                var pcrs = TpmCollector.DumpPCRs(tpm, PcrAlgorithm, new PcrSelection[] { new PcrSelection(PcrAlgorithm, new uint[] { 15, 16 }) });
                Assert.IsTrue(pcrs.All(x => x.Value.SequenceEqual(new byte[x.Value.Length])));

                // Measure to PCR 16
                try
                {
                    tpm.PcrExtend(TpmHandle.Pcr(16), tpm.PcrEvent(TpmHandle.Pcr(16), nvData));
                }
                catch (TpmException e)
                {
                    Log.Debug(e, "Failed to Write PCR.");
                }

                // Verify that we extended the PCR
                var pcrs2 = TpmCollector.DumpPCRs(tpm, PcrAlgorithm, new PcrSelection[] { new PcrSelection(PcrAlgorithm, new uint[] { 15, 16 }, 24) });
                Assert.IsTrue(pcrs2[(PcrAlgorithm, 15)].SequenceEqual(pcrs[(PcrAlgorithm, 15)]));