in TPM Parser/Tpm2Lib/Tpm2.cs [1068:1155]
public static string ParseResponse(string commandCode, byte[] buf)
{
TpmHandle[] outHandles;
SessionOut[] outSessions;
byte[] responseParmsNoHandles;
byte[] responseParmsWithHandles;
string response = "";
if (1 != CommandInformation.Info.Count(item => item.CommandCode.ToString() == commandCode))
{
response = "Command code not recognized. Defined command codes are:\n";
// ReSharper disable once LoopCanBeConvertedToQuery
foreach (CommandInfo info in CommandInformation.Info)
{
response += info.CommandCode.ToString() + " ";
}
return response;
}
CommandInfo command = CommandInformation.Info.First(item => item.CommandCode.ToString() == commandCode);
TpmSt tag;
uint paramSize;
TpmRc responseCode;
SplitResponse(buf,
command.HandleCountOut,
out tag,
out paramSize,
out responseCode,
out outHandles,
out outSessions,
out responseParmsNoHandles,
out responseParmsWithHandles);
if (responseCode != TpmRc.Success)
{
TpmRc resultCode;
response += "Error:\n";
response += Tpm2.GetErrorString(command.InStructType, (uint)responseCode, out resultCode);
return response;
}
// At this point in the processing stack we cannot deal with encrypted responses
bool responseIsEncrypted = false;
foreach (SessionOut s in outSessions)
{
if (s.attributes.HasFlag(SessionAttr.Encrypt)
&&
(command.TheParmCryptInfo.HasFlag(ParmCryptInfo.DecOut2) ||
command.TheParmCryptInfo.HasFlag(ParmCryptInfo.DecOut2))
)
responseIsEncrypted = true;
}
response += "Response Header:\n";
response += " Tag=" + tag.ToString() + "\n";
response += " Response code=" + responseCode.ToString() + "\n";
response += "Response Parameters:\n";
if (!responseIsEncrypted)
{
var m2 = new Marshaller(responseParmsWithHandles);
Object inParms = m2.Get(command.OutStructType, "");
response += inParms + "\n";
}
else
{
var m2 = new Marshaller(responseParmsWithHandles);
Object encOutParms = null;
switch (command.TheParmCryptInfo)
{
// TODO: this is not the right type if we ever do size-checks
case ParmCryptInfo.DecOut2:
encOutParms = m2.Get(typeof (Tpm2bMaxBuffer), "");
break;
default:
Globs.Throw<NotImplementedException>("NOT IMPLEMENTED");
break;
}
response += "Encrypted: " + encOutParms + "\n";
}
response += "Sessions [" + outSessions.Length + "]\n";
for (int j = 0; j < outSessions.Length; j++)
{
// ReSharper disable once FormatStringProblem
response += String.Format("{0}: 0x{1:x}\n", j, outSessions[j]);
}
return response;
}