private void btnRead_Click(object sender, EventArgs e) { byte[] imageBytes; try { imageBytes = File.ReadAllBytes(txtFilePath.Text); } catch { MessageBox.Show("Unable to read file"); return; } X509Certificate2 certificate = null; try { List <byte[]> certificates; if (SecondaryExecutableHeader.IsSecondaryExecutable(imageBytes)) { certificates = SecondaryExecutableHelper.ExtractCertificates(imageBytes); } else { certificates = ApplicationExecutableHelper.ExtractCertificates(imageBytes); } certificate = new X509Certificate2(certificates[0]); } catch { txtSoftwareID.Text = "Error"; txtHardwareID.Text = "Error"; } if (certificate != null) { txtSoftwareID.Text = GetByteArrayString(ReadHexPropertyFromCertificate(certificate, "OU=01", "SW_ID")); txtHardwareID.Text = GetByteArrayString(ReadHexPropertyFromCertificate(certificate, "OU=02", "HW_ID")); } try { byte[] rootCertificateHash; if (SecondaryExecutableHeader.IsSecondaryExecutable(imageBytes)) { rootCertificateHash = SecondaryExecutableVerification.GetRootCertificateHash(imageBytes); } else { rootCertificateHash = ApplicationExecutableVerification.GetRootCertificateHash(imageBytes); } txtOemPKHash.Text = GetByteArrayString(rootCertificateHash); } catch { txtOemPKHash.Text = "Error"; } }
private void Verify() { byte[] hardwareID = null; try { hardwareID = ConvertHexStringToByteArray(txtHardwareID.Text.Replace(" ", String.Empty)); } catch { } if (hardwareID == null || hardwareID.Length != 8) { MessageBox.Show("Invalid HW_ID"); return; } byte[] softwareID = null; try { softwareID = ConvertHexStringToByteArray(txtSoftwareID.Text.Replace(" ", String.Empty)); } catch { } if (softwareID == null || softwareID.Length != 8) { MessageBox.Show("Invalid SW_ID"); return; } byte[] oemPKHash = null; try { oemPKHash = ConvertHexStringToByteArray(txtOemPKHash.Text.Replace(" ", String.Empty)); } catch { } if (oemPKHash == null || (oemPKHash.Length != 20 && oemPKHash.Length != 32)) { MessageBox.Show("Invalid OEM_PK_HASH"); return; } byte[] imageBytes; try { imageBytes = File.ReadAllBytes(txtFilePath.Text); } catch { MessageBox.Show("Unable to read file"); return; } try { bool isRootCertificateValid = SecondaryExecutableVerification.VerifyRootCertificateHash(imageBytes, oemPKHash); lblRootCertificateStatus.Text = isRootCertificateValid ? "OK" : "INVALID"; } catch { lblRootCertificateStatus.Text = "Error"; } try { bool isCertificateStoreValid = SecondaryExecutableVerification.VerifyCertificateStore(imageBytes); lblCertificateStoreStatus.Text = isCertificateStoreValid ? "OK" : "INVALID"; } catch { lblCertificateStoreStatus.Text = "Error"; } try { bool isImageSignatureValid = SecondaryExecutableVerification.VerifyImageSignature(imageBytes, softwareID, hardwareID); lblImageSignatureStatus.Text = isImageSignatureValid ? "OK" : "INVALID"; } catch { lblImageSignatureStatus.Text = "Error"; } }