Esempio n. 1
0
        public static void Main(string[] args)
        {
            string quoteMe = "Hallo IAIK!";

            byte[] quoteMeBytes = System.Text.Encoding.ASCII.GetBytes(quoteMe);

            // Establish Connections
            IDictionary <string, TPMSession> sessions =
                XMLConfiguration.EstablischConnection(base_path + "ClientConfigXml/UnixSocketDeviceLin.xml");

            // Create one keystore per opened session
            foreach (TPMSession tpmSes in sessions.Values)
            {
                tpmSes.Keystore = new InMemoryKeystore();
            }

            TPMSession sessionToUse = sessions["local0"];

            sessionToUse.SetRequestSecretCallback(RequestSecret);

            ClientKeyHandle myFirstSignKey =
                sessionToUse.KeyClient.GetSrkKeyHandle().CreateKey("my_first_sign_key", TPMKeyUsage.TPM_KEY_SIGNING);

            ISigner signer = myFirstSignKey.CreateSigner();

            signer.Init(true, null);
            signer.BlockUpdate(quoteMeBytes, 0, quoteMeBytes.Length);

            byte[] quote = signer.GenerateSignature();

            Console.WriteLine("Sign of \"Hallo IAIK\" is:\n" + ByteHelper.ByteArrayToHexString(quote));

            Console.WriteLine();
            Console.WriteLine("Now we would verify this sign.");

            signer.Reset();
            signer.Init(false, null);
            signer.BlockUpdate(quoteMeBytes, 0, quoteMeBytes.Length);

            if (signer.VerifySignature(quote) == true)
            {
                Console.WriteLine("Sign is OK!");
            }
            else
            {
                Console.WriteLine("UUUUPPPPSSS something went wrong!");
            }
        }
Esempio n. 2
0
        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();
        }