Beispiel #1
0
 public override void DispatchCommand(CommandModifier active, byte[] inBuf, out byte[] outBuf)
 {
     if (CommandCallbacks != null)
     {
         byte[] tempInBuf;
         CommandCallbacks.PreCallback(inBuf, out tempInBuf);
         if (tempInBuf != null)
         {
             inBuf = tempInBuf;
         }
     }
     Device.DispatchCommand(active, inBuf, out outBuf);
     if (CommandCallbacks != null)
     {
         CommandCallbacks.PostCallback(inBuf, outBuf);
     }
 }
Beispiel #2
0
        // Send TPM-command buffer to device
        public override void DispatchCommand(
            CommandModifier mod,
            byte[] cmdBuf,
            out byte[] respBuf)
        {
            if (TrmDevice != null)
            {
                TrmDevice.DispatchCommand(mod, cmdBuf, out respBuf);
                return;
            }

            if (_tpmIO == null)
            {
                throw new InvalidOperationException("TPM context not created.");
            }

            _tpmIO.Write(cmdBuf, 0, cmdBuf.Length);

            int count     = 0;
            int bytesRead = 0;

            do
            {
                bytesRead = _tpmIO.Read(_responseBuffer, 0, _responseBuffer.Length);
                if (bytesRead > 0)
                {
                    break;
                }

#if WINDOWS_UWP
                Task.Delay(TpmIORetryBackoffTime).Wait();
#else
                Thread.Sleep(TpmIORetryBackoffTime);
#endif
                Debug.WriteLine($"TPM {_tpmDevicePath} retry {count}.");
            } while (count++ < TpmIORetryCount);

            if (bytesRead <= 0)
            {
                throw new IOException($"No response from {_tpmDevicePath}");
            }

            respBuf = new byte[bytesRead];
            Array.Copy(_responseBuffer, respBuf, bytesRead);
        }
Beispiel #3
0
        // Send TPM-command buffer to device
        public override void DispatchCommand(
            CommandModifier mod,
            byte[] cmdBuf,
            out byte[] respBuf)
        {
            if (TctiCtx != null && TctiCtxPtr != IntPtr.Zero)
            {
                TpmRc rc = TctiCtx.transmit(TctiCtxPtr, (ulong)cmdBuf.Length, cmdBuf);
                if (rc != TpmRc.Success)
                {
                    throw new TssException($"TCTI_CTX::transmit() failed: error {rc}");
                }

                ulong bytesReceived = (ulong)_responseBuffer.Length;
                rc = TctiCtx.receive(TctiCtxPtr, ref bytesReceived, _responseBuffer, 5 * 60 * 1000);
                if (rc != TpmRc.Success)
                {
                    throw new TssException($"TCTI_CTX::receive() failed: error {rc}");
                }

                respBuf = new byte[bytesReceived];
                Array.Copy(_responseBuffer, respBuf, (int)bytesReceived);
            }
            else if (TrmDevice != null)
            {
                TrmDevice.DispatchCommand(mod, cmdBuf, out respBuf);
                return;
            }
            else if (_tpmIO != null)
            {
                _tpmIO.Write(cmdBuf, 0, cmdBuf.Length);

                int count     = 0;
                int bytesRead = 0;
                do
                {
                    bytesRead = _tpmIO.Read(_responseBuffer, 0, _responseBuffer.Length);
                    if (bytesRead > 0)
                    {
                        break;
                    }

    #if WINDOWS_UWP
                    Task.Delay(TpmIORetryBackoffTime).Wait();
    #else
                    Thread.Sleep(TpmIORetryBackoffTime);
    #endif
                    Debug.WriteLine($"TPM {_tpmDevicePath} retry {count}.");
                } while (count++ < TpmIORetryCount);

                if (bytesRead <= 0)
                {
                    throw new IOException($"No response from {_tpmDevicePath}");
                }

                respBuf = new byte[bytesRead];
                Array.Copy(_responseBuffer, respBuf, bytesRead);
            }
            else
            {
                throw new InvalidOperationException("TPM context is not initialized.");
            }
        }