示例#1
0
        /// <summary>
        /// Reads the selected PCR Index of the TPM.
        /// </summary>
        /// <param name="pcrIndex">PCR Index 0 - 23</param>
        /// <returns>20-byte hex value in a string</returns>
        internal string ReadPCR(int pcrIndex)
        {
            if (pcrIndex < 0 || pcrIndex > 23)
            {
                throw new ArgumentOutOfRangeException("pcrIndex", "Invalid TPM PCR Index");
            }

            string cmd = @"00c1"        // tag = TPM_TAG_RQU_COMMAND
                         + @"0000000e"  // paramSize
                         + @"00000015"  // ordinal = TPM_ORD_PCRRead
                         + pcrIndex.ToString("X8");

            byte[] res = this.SubmitCommand(HexFormat.StringToByteArray(cmd), 30);

            if (res != null)
            {
                string sRes = HexFormat.ByteArrayToString(res);

                //string tag = sRes.Substring(0, 4);
                //string paramSize = sRes.Substring(4, 8);
                string returnCode = sRes.Substring(12, 8);
                string outDigest  = sRes.Substring(20, 40);

                int rc = Int32.Parse(returnCode, System.Globalization.NumberStyles.HexNumber);

                if (rc == 0)
                {
                    return(outDigest);
                }
            }

            return(null);
        }
        internal string Generate()
        {
            if (bParamsChanged)
            {
                string        value    = null;
                StringBuilder sbValues = new StringBuilder();

                foreach (WMIQueryObj wmiQuery in this.queryItems)
                {
                    ManagementObjectSearcher oWMI = new ManagementObjectSearcher(wmiQuery.QueryString);

                    foreach (ManagementObject mo in oWMI.Get())
                    {
                        value = (string)mo[wmiQuery.Property];
                        if (!String.IsNullOrEmpty(value))
                        {
                            sbValues.AppendLine(value);
                        }
                    }
                }

                if (MeasureTpm)
                {
                    value = TpmWrapper.Instance.ReadPCR(0);
                    if (!String.IsNullOrEmpty(value))
                    {
                        sbValues.AppendLine(value);
                    }
                    else
                    {
                        this.bMeasureTpm = false;
                    }
                }

                if (MeasureHDDSerialNumber)
                {
                    string logicalDiskId      = Path.GetPathRoot(Environment.GetFolderPath(Environment.SpecialFolder.System)).Substring(0, 2);
                    string deviceSerialNumber = string.Empty;

                    var query        = "ASSOCIATORS OF {Win32_LogicalDisk.DeviceID='" + logicalDiskId + "'} WHERE AssocClass = Win32_LogicalDiskToPartition";
                    var queryResults = new ManagementObjectSearcher(query);
                    var partitions   = queryResults.Get();

                    foreach (var partition in partitions)
                    {
                        query        = "ASSOCIATORS OF {Win32_DiskPartition.DeviceID='" + partition["DeviceID"] + "'} WHERE AssocClass = Win32_DiskDriveToDiskPartition";
                        queryResults = new ManagementObjectSearcher(query);
                        var drives = queryResults.Get();

                        foreach (var drive in drives)
                        {
                            deviceSerialNumber = drive["SerialNumber"].ToString();
                        }
                    }

                    if (!String.IsNullOrEmpty(deviceSerialNumber))
                    {
                        sbValues.AppendLine(deviceSerialNumber);
                    }
                    else
                    {
                        this.bMeasureHDDSerialNumber = false;
                    }
                }

                ASCIIEncoding asciEncoding  = new ASCIIEncoding();
                byte[]        btAsciEncoded = asciEncoding.GetBytes(sbValues.ToString());

                SHA256 sha = new SHA256CryptoServiceProvider();
                btAsciEncoded = sha.ComputeHash(btAsciEncoded);

                this.result = HexFormat.ByteArrayToString(btAsciEncoded);
            }

            return(this.result);
        }