/// <summary> /// Modifies the handles and sessions arrays so that they contain the translated handles. /// </summary> /// <param name="handles"></param> /// <param name="sessions"></param> /// <param name="theObjects"></param> /// <param name="theSessions"></param> private void ReplaceHandlesIn(TpmHandle[] handles, SessionIn[] sessions, ObjectContext[] theObjects, ObjectContext[] theSessions) { for (int j = 0; j < handles.Length; j++) { handles[j] = theObjects[j].TheTpmHandle; } for (int j = 0; j < sessions.Length; j++) { sessions[j].handle = theSessions[j].TheTpmHandle; } }
/// <summary> /// Get the TBS ObjectContext given SessionIn objects collected from the inputs stream. /// </summary> /// <param name="caller"></param> /// <param name="inSessions"></param> /// <returns></returns> private ObjectContext[] GetSessions(TbsContext caller, SessionIn[] inSessions) { var contexts = new ObjectContext[inSessions.Length]; for (int j = 0; j < inSessions.Length; j++) { contexts[j] = ContextManager.GetContext(caller, inSessions[j].handle); if (contexts[j] == null) { return null; } } return contexts; }
/// <summary> /// Create a TPM command byte stream from constituent components /// </summary> /// <param name="commandCode"></param> /// <param name="handles"></param> /// <param name="sessions"></param> /// <param name="parmsWithoutHandles"></param> /// <returns></returns> public static byte[] CreateCommand( TpmCc commandCode, TpmHandle[] handles, SessionIn[] sessions, byte[] parmsWithoutHandles) { // ReSharper disable once UnusedVariable CommandInfo commandInfo = Tpm2.CommandInfoFromCommandCode(commandCode); var m = new Marshaller(); TpmSt tag = sessions.Length == 0 ? TpmSt.NoSessions : TpmSt.Sessions; m.Put(tag, "tag"); m.PushLength(4); m.Put(commandCode, "commandCode"); foreach (TpmHandle h in handles) { m.Put(h, "handle"); } if (tag == TpmSt.Sessions) { var m2 = new Marshaller(); foreach (SessionIn s in sessions) { m2.Put(s, "session"); } m.PutUintPrependedArray(m2.GetBytes(), "sessions"); } m.Put(parmsWithoutHandles, "parms"); m.PopAndSetLengthToTotalLength(); return m.GetBytes(); }
public SessionIn(SessionIn the_SessionIn) { if((Object) the_SessionIn == null ) throw new ArgumentException(Globs.GetResourceString("parmError")); handle = the_SessionIn.handle; nonceCaller = the_SessionIn.nonceCaller; attributes = the_SessionIn.attributes; auth = the_SessionIn.auth; }
/// <summary> /// Opens a properly-formed TPM command stream into its constituent components. /// Note: commandParams does NOT include handles. /// </summary> /// <param name="command"></param> /// <param name="header"></param> /// <param name="handles"></param> /// <param name="sessions"></param> /// <param name="commandParms"></param> public static bool CrackCommand( byte[] command, out CommandHeader header, out TpmHandle[] handles, out SessionIn[] sessions, out byte[] commandParms) { var m = new Marshaller(command); header = m.Get<CommandHeader>(); CommandInfo commandInfo = Tpm2.CommandInfoFromCommandCode(header.CommandCode); if (header.Tag == TpmSt.Null) { // A diagnostics command. Pass through unmodified handles = null; sessions = null; commandParms = null; return false; } handles = new TpmHandle[commandInfo.HandleCountIn]; for (int j = 0; j < handles.Length; j++) { handles[j] = m.Get<TpmHandle>(); } // Note sessions are only present if the command tag indicates sessions if (header.Tag == TpmSt.Sessions) { var sessionLength = m.Get<uint>(); uint sessionEnd = m.GetGetPos() + sessionLength; var inSessions = new List<SessionIn>(); while (m.GetGetPos() < sessionEnd) { var s = m.Get<SessionIn>(); inSessions.Add(s); } sessions = inSessions.ToArray(); } else { sessions = new SessionIn[0]; } // And finally parameters commandParms = m.GetArray<byte>((int)(m.GetValidLength() - m.GetGetPos())); if (m.GetValidLength() != header.CommandSize) { throw new Exception("Command length in header does not match input byte-stream"); } return true; }