public SessionOut(SessionOut the_SessionOut) { if((Object) the_SessionOut == null ) throw new ArgumentException(Globs.GetResourceString("parmError")); nonceTpm = the_SessionOut.nonceTpm; attributes = the_SessionOut.attributes; auth = the_SessionOut.auth; }
public static byte[] CreateResponse( TpmRc responseCode, TpmHandle[] handles, SessionOut[] sessions, byte[] responseParmsNoHandles) { var m = new Marshaller(); TpmSt tag = sessions.Length == 0 ? TpmSt.NoSessions : TpmSt.Sessions; m.Put(tag, "tag"); m.PushLength(4); m.Put(responseCode, "responseCode"); foreach (TpmHandle h in handles) { m.Put(h, "handle"); } if (tag == TpmSt.Sessions) { m.Put((uint)responseParmsNoHandles.Length, "parmsLenght"); } m.Put(responseParmsNoHandles, "parms"); foreach (SessionOut s in sessions) m.Put(s, "session"); m.PopAndSetLengthToTotalLength(); return m.GetBytes(); }
} // ProcessResponseSessions private void ValidateResponseSessions( TpmHandle[] outHandles, SessionOut[] outSessions, TpmCc commandCode, TpmRc responseCode, byte[] outParmsNoHandles) { int numSessions = Sessions.Length; if (numSessions == 0 || numSessions > outSessions.Length) { return; } int outSessionCount = 0; foreach (SessionBase s in Sessions) { SessionOut outSess = outSessions[outSessionCount++]; if (s is Pwap) { if (outSess.nonceTpm.Length != 0) { throw new TpmFailure("PWAP returned non-empty nonce"); } if (outSess.auth.Length != 0) { throw new TpmFailure("PWAP returned non-empty auth value"); } } // ReSharper disable once CanBeReplacedWithTryCastAndCheckForNull else if (s is AuthSession) { var sess = s as AuthSession; byte[] parmsHash = GetExpectedResponseHash(sess.AuthHash, outParmsNoHandles, commandCode, responseCode); byte[] expectedHmac = sess.GetAuthHmac(parmsHash, Direction.Response); if (outSess.auth.Length != 0) { if (!Globs.ArraysAreEqual(outSess.auth, expectedHmac)) { throw new TpmFailure("Bad response HMAC"); } } } else { throw new TpmFailure("Invalid response session type"); } } } // ValidateResponseSessions
public static void SplitResponse( byte[] response, uint numHandles, out TpmSt tag, out uint paramSize, out TpmRc responseCode, out TpmHandle[] handles, out SessionOut[] sessions, out byte[] responseParmsNoHandles, out byte[] responseParmsWithHandles) { var m = new Marshaller(response); tag = m.Get<TpmSt>(); paramSize = m.Get<uint>(); responseCode = m.Get<TpmRc>(); // If error we only get the header if (responseCode != TpmRc.Success) { handles = new TpmHandle[0]; sessions = new SessionOut[0]; responseParmsNoHandles = new byte[0]; responseParmsWithHandles = new byte[0]; return; } handles = new TpmHandle[numHandles]; for (int j = 0; j < numHandles; j++) { handles[j] = m.Get<TpmHandle>(); } uint parmsEnd = m.GetValidLength(); if (tag == TpmSt.Sessions) { var sessionOffset = m.Get<uint>(); uint startOfParmsX = m.GetGetPos(); parmsEnd = startOfParmsX + sessionOffset; m.SetGetPos(parmsEnd); var sessX = new List<SessionOut>(); while (m.GetGetPos() < m.GetValidLength()) { var s = m.Get<SessionOut>(); sessX.Add(s); } sessions = sessX.ToArray(); m.SetGetPos(startOfParmsX); } else { sessions = new SessionOut[0]; } uint startOfParms = m.GetGetPos(); uint parmsLength = parmsEnd - m.GetGetPos(); // Get the response buf with no handles responseParmsNoHandles = new byte[parmsLength]; Array.Copy(response, (int)startOfParms, responseParmsNoHandles, 0, (int)parmsLength); // Get the response buf with handles responseParmsWithHandles = new byte[parmsLength + numHandles * 4]; Array.Copy(response, 10, responseParmsWithHandles, 0, (int)numHandles * 4); Array.Copy(response, (int)startOfParms, responseParmsWithHandles, (int)numHandles * 4, (int)parmsLength); }
/// <summary> /// Validate the output sessions and update the session state as needed /// </summary> /// <param name="outHandles"></param> /// <param name="commandCode"></param> /// <param name="responseCode"></param> /// <param name="outSessions"></param> /// <param name="outParmsNoHandles"></param> // ReSharper disable once UnusedParameter.Local private void ProcessResponseSessions(SessionOut[] outSessions) { int numSessions = Sessions.Length; if (numSessions == 0 || numSessions > outSessions.Length) { return; } int outSessionCount = 0; foreach (SessionBase s in Sessions) { var outSess = outSessions[outSessionCount++]; if (s is AuthSession) { var sess = (AuthSession)s; sess.SetNonceTpm(outSess.nonceTpm); sess.Attrs = outSess.attributes; } } } // ProcessResponseSessions