public override void Execute(string[] commandline) { if (commandline.Length < 2) { _console.Out.WriteLine("Error: [local_session_alias] not specified"); return; } else if (commandline.Length < 3) { _console.Out.WriteLine("Error: [command] not specified"); return; } ClientContext ctx = _console.GetValue <ClientContext> ("client_context", null); if (ctx == null) { _console.Out.WriteLine("No active connection was found"); return; } string localAlias = commandline[1]; IDictionary <string, TPMSession> tpmSessions = _console.GetValue <IDictionary <string, TPMSession> > ("tpm_sessions", null); if (tpmSessions == null || tpmSessions.ContainsKey(localAlias) == false) { _console.Out.WriteLine("Error: Specified local alias was not found"); return; } if (tpmSessions[localAlias].Keystore == null) { _console.Out.WriteLine("Error: No keystore was opened"); return; } string subCommand = commandline[2]; IDictionary <string, string> arguments = _console.SplitArguments(commandline[3], 0); if (arguments.ContainsKey("name") == false) { _console.Out.WriteLine("Error: no key name was specified"); return; } // if(arguments.ContainsKey("pcr") == false) // { // _console.Out.WriteLine("Error: no pcr values where specified"); // return; // } if (arguments.ContainsKey("data_input") == false) { _console.Out.WriteLine("Error: no data input source specified"); return; } DataInputMode dataInputMode; try { dataInputMode = (DataInputMode)Enum.Parse(typeof(DataInputMode), arguments["data_input"], true); } catch (Exception) { _console.Out.WriteLine("Error: Invalid data input source"); return; } DataFormat inputDataFormat = DataFormat.Raw; if (arguments.ContainsKey("input_data_format")) { try { inputDataFormat = (DataFormat)Enum.Parse(typeof(DataFormat), arguments["input_data_format"], true); } catch (Exception) { _console.Out.WriteLine("Error: Invalid input data format"); return; } } if (dataInputMode == DataInputMode.File && arguments.ContainsKey("file") == false) { _console.Out.WriteLine("Error: data_input=file requires file argument!"); return; } ClientKeyHandle keyHandle = tpmSessions[localAlias].KeyClient.GetKeyHandleByFriendlyName(arguments["name"]); Stream inputStream = null; if (dataInputMode == DataInputMode.Console) { inputStream = new TextReaderStream(_console.In); } else if (dataInputMode == DataInputMode.Embedded) { if (commandline.Length <= 3) { _console.Out.WriteLine("Error: no embedded data"); return; } StringBuilder embeddedData = new StringBuilder(); for (int i = 3; i < commandline.Length; i++) { embeddedData.Append(commandline[i]); if (i + 1 < commandline.Length) { embeddedData.Append(" "); } } inputStream = new TextReaderStream(new StringReader(embeddedData.ToString())); } else if (dataInputMode == DataInputMode.File) { inputStream = new FileStream(arguments["file"], FileMode.Open, FileAccess.Read); } if (inputDataFormat == DataFormat.Hex) { inputStream = new HexFilterStream(inputStream); } ISigner signatureGenerator = null; if (subCommand == "verify") { signatureGenerator = keyHandle.CreateSigner(); signatureGenerator.Init(false, null); } else if (subCommand == "generate") { signatureGenerator = keyHandle.CreateSigner(); signatureGenerator.Init(true, null); } else if (subCommand == "generate_quote" || subCommand == "verify_quote") { if (arguments.ContainsKey("pcr") == false) { _console.Out.WriteLine("Error: No pcrs specified!"); return; } TPMPCRSelection pcrSelection = tpmSessions[localAlias].CreateEmptyPCRSelection(); foreach (string pcr in arguments["pcr"].Split('|')) { int pcrValue = int.Parse(pcr); pcrSelection.PcrSelection.SetBit(pcrValue - 1, true); } signatureGenerator = keyHandle.CreateQuoter(pcrSelection); signatureGenerator.Init(subCommand == "generate_quote", null); } byte[] buffer = new byte[1024]; int read = 0; do { read = inputStream.Read(buffer, 0, buffer.Length); signatureGenerator.BlockUpdate(buffer, 0, read); }while(read > 0); _console.Out.WriteLine(ByteHelper.ByteArrayToHexString(signatureGenerator.GenerateSignature())); _console.Out.WriteLine(); inputStream.Dispose(); }
public override void Execute(string[] commandline) { if (commandline.Length < 2) { _console.Out.WriteLine("Error: [local_session_alias] not specified"); return; } else if (commandline.Length < 3) { _console.Out.WriteLine("Error: [command] not specified"); return; } ClientContext ctx = _console.GetValue <ClientContext> ("client_context", null); if (ctx == null) { _console.Out.WriteLine("No active connection was found"); return; } string localAlias = commandline[1]; IDictionary <string, TPMSession> tpmSessions = _console.GetValue <IDictionary <string, TPMSession> > ("tpm_sessions", null); if (tpmSessions == null || tpmSessions.ContainsKey(localAlias) == false) { _console.Out.WriteLine("Error: Specified local alias was not found"); return; } if (tpmSessions[localAlias].Keystore == null) { _console.Out.WriteLine("Error: No keystore was opened"); return; } IDictionary <string, string> arguments = _console.SplitArguments(commandline[2], 0); if (arguments.ContainsKey("name") == false) { _console.Out.WriteLine("Error: no key name was specified"); return; } if (arguments.ContainsKey("data_input") == false) { _console.Out.WriteLine("Error: no data input source specified"); return; } TPMSessionSealCommand.DataInputMode dataInputMode; try { dataInputMode = (TPMSessionSealCommand.DataInputMode)Enum.Parse(typeof(TPMSessionSealCommand.DataInputMode), arguments["data_input"], true); } catch (Exception) { _console.Out.WriteLine("Error: Invalid data input source"); return; } TPMSessionSealCommand.DataOutputMode dataOutputMode; try { dataOutputMode = (TPMSessionSealCommand.DataOutputMode)Enum.Parse(typeof(TPMSessionSealCommand.DataOutputMode), arguments["data_output"], true); } catch (Exception) { _console.Out.WriteLine("Error: Invalid data output destination"); return; } TPMSessionSealCommand.DataFormat inputDataFormat = TPMSessionSealCommand.DataFormat.Raw; if (arguments.ContainsKey("input_data_format")) { try { inputDataFormat = (TPMSessionSealCommand.DataFormat)Enum.Parse(typeof(TPMSessionSealCommand.DataFormat), arguments["input_data_format"], true); } catch (Exception) { _console.Out.WriteLine("Error: Invalid input data format"); return; } } TPMSessionSealCommand.DataFormat outputDataFormat = TPMSessionSealCommand.DataFormat.Raw; if (arguments.ContainsKey("output_data_format")) { try { outputDataFormat = (TPMSessionSealCommand.DataFormat)Enum.Parse(typeof(TPMSessionSealCommand.DataFormat), arguments["output_data_format"], true); } catch (Exception) { _console.Out.WriteLine("Error: Invalid output data format"); return; } } if (dataInputMode == TPMSessionSealCommand.DataInputMode.File && arguments.ContainsKey("file") == false) { _console.Out.WriteLine("Error: data_input=file requires file argument!"); return; } if (dataOutputMode == TPMSessionSealCommand.DataOutputMode.File && arguments.ContainsKey("output_file") == false) { _console.Out.WriteLine("Error: data_output=file requires output_file argument!"); return; } ClientKeyHandle keyHandle = tpmSessions[localAlias].KeyClient.GetKeyHandleByFriendlyName(arguments["name"]); Stream inputStream = null; if (dataInputMode == TPMSessionSealCommand.DataInputMode.Console) { inputStream = new TextReaderStream(_console.In); } else if (dataInputMode == TPMSessionSealCommand.DataInputMode.Embedded) { if (commandline.Length <= 3) { _console.Out.WriteLine("Error: no embedded data"); return; } StringBuilder embeddedData = new StringBuilder(); for (int i = 3; i < commandline.Length; i++) { embeddedData.Append(commandline[i]); if (i + 1 < commandline.Length) { embeddedData.Append(" "); } } inputStream = new TextReaderStream(new StringReader(embeddedData.ToString())); } else if (dataInputMode == TPMSessionSealCommand.DataInputMode.File) { inputStream = new FileStream(arguments["file"], FileMode.Open, FileAccess.Read); } if (inputDataFormat == TPMSessionSealCommand.DataFormat.Hex) { inputStream = new HexFilterStream(inputStream); } Stream outputStream = null; if (dataOutputMode == TPMSessionSealCommand.DataOutputMode.Console) { outputStream = new TextWriterStream(_console.Out); } else if (dataOutputMode == TPMSessionSealCommand.DataOutputMode.File) { outputStream = new FileStream(arguments["output_file"], FileMode.OpenOrCreate, FileAccess.Write); } if (outputDataFormat == TPMSessionSealCommand.DataFormat.Hex) { outputStream = new HexFilterStream(outputStream); } IAsymmetricBlockCipher bindCipher = keyHandle.CreateBindBlockCipher(); bindCipher.Init(false, null); int read; byte[] buffer = new byte[bindCipher.GetInputBlockSize()]; do { read = inputStream.Read(buffer, 0, buffer.Length); if (read > 0) { byte[] encrypted = bindCipher.ProcessBlock(buffer, 0, read); outputStream.Write(encrypted, 0, encrypted.Length); } }while(read > 0); _console.Out.WriteLine(); outputStream.Dispose(); inputStream.Dispose(); }
public override void Execute(string[] commandline) { if (commandline.Length < 2) { _console.Out.WriteLine("Error: [local_alias] not specified"); } else if (commandline.Length < 3) { _console.Out.WriteLine("Error: [pcr_subcommand] not specified"); } ClientContext ctx = _console.GetValue <ClientContext> ("client_context", null); if (ctx == null) { _console.Out.WriteLine("No active connection was found"); return; } string localAlias = commandline[1]; string pcrCommand = commandline[2]; IDictionary <string, TPMSession> tpmSessions = _console.GetValue <IDictionary <string, TPMSession> > ("tpm_sessions", null); if (tpmSessions == null || tpmSessions.ContainsKey(localAlias) == false) { _console.Out.WriteLine("Error: Specified local alias was not found"); return; } if (pcrCommand == "report") { uint pcrCount = tpmSessions[localAlias].CapabilityClient.GetPCRCount(); for (uint i = 0; i < pcrCount; i++) { _console.Out.WriteLine("#{0}: {1}", i, ByteHelper.ByteArrayToHexString(tpmSessions[localAlias].IntegrityClient.PCRValue(i))); } } else if (pcrCommand == "extend") { if (commandline.Length < 4) { _console.Out.WriteLine("Error: 'extend' requires some arguments"); return; } IDictionary <string, string> arguments = _console.SplitArguments(commandline[3], 0); if (arguments.ContainsKey("pcr") == false) { _console.Out.WriteLine("Error: 'extend' requires parameter 'pcr' to be specified"); return; } uint pcr = 0; if (uint.TryParse(arguments["pcr"], out pcr) == false) { _console.Out.WriteLine("Error: 'pcr' could not be parsed, is it a valid pcr specified?"); return; } if (arguments.ContainsKey("data_input") == false) { _console.Out.WriteLine("Error: 'extend' requires parameter 'data_input' to be specified"); return; } TPMSessionSealCommand.DataInputMode dataInput = (TPMSessionSealCommand.DataInputMode)Enum.Parse(typeof(TPMSessionSealCommand.DataInputMode), arguments["data_input"], true); if (dataInput != TPMSessionSealCommand.DataInputMode.Embedded && dataInput != TPMSessionSealCommand.DataInputMode.File) { _console.Out.WriteLine("Error: 'data_input' has an invalid value"); return; } byte[] digest; if (dataInput == TPMSessionSealCommand.DataInputMode.File && arguments.ContainsKey("file") == false) { _console.Out.WriteLine("Error: file-data_input require 'file' argument to be specified"); return; } else if (dataInput == TPMSessionSealCommand.DataInputMode.File) { FileInfo myFile = new FileInfo(arguments["file"]); using (FileStream src = myFile.OpenRead()) { digest = new HashProvider().Hash( new HashStreamDataProvider(src, null, null, false)); } } else if (dataInput == TPMSessionSealCommand.DataInputMode.Embedded) { using (Stream src = new HexFilterStream(new TextReaderStream(new StringReader(commandline[4])))) { digest = new byte[20]; if (src.Length != 20) { throw new ArgumentException("Error: The embedded digest must be 20 bytes long"); } src.Read(digest, 0, 20); } } else { throw new ArgumentException(String.Format("data input mode '{0}' is not supported", dataInput)); } _console.Out.WriteLine("Doing extension with digest: '{0}'", ByteHelper.ByteArrayToHexString(digest)); byte[] newDigest = tpmSessions[localAlias].IntegrityClient.Extend(pcr, digest); _console.Out.WriteLine("Extension successful, new pcr value: {0}", ByteHelper.ByteArrayToHexString(newDigest)); } else if (pcrCommand == "quote") { if (commandline.Length < 4) { _console.Out.WriteLine("Error: 'quote' requires some arguments"); return; } IDictionary <string, string> arguments = _console.SplitArguments(commandline[3], 0); if (arguments.ContainsKey("pcr") == false) { _console.Out.WriteLine("Error: 'quote' requires parameter 'pcr' to be specified"); return; } if (arguments.ContainsKey("name") == false) { _console.Out.WriteLine("Error: no key name was specified"); return; } ClientKeyHandle keyHandle = tpmSessions[localAlias].KeyClient.GetKeyHandleByFriendlyName(arguments["name"]); TPMPCRSelection pcrSelection = tpmSessions[localAlias].CreateEmptyPCRSelection(); foreach (string pcr in arguments["pcr"].Split('|')) { int pcrValue = int.Parse(pcr); pcrSelection.PcrSelection.SetBit(pcrValue - 1, true); } TPMPCRComposite quoted = keyHandle.SimpleQuote(pcrSelection); IList <int> selectedPCRs = quoted.PCRSelection.SelectedPCRs; for (int i = 0; i < selectedPCRs.Count; i++) { _console.Out.WriteLine("#{0}: {1}", selectedPCRs[i], ByteHelper.ByteArrayToHexString(quoted.PCRValues[i])); } } else { _console.Out.WriteLine("Error, unknown pcr_subcommand '{0}'", commandline[1]); } }