Check public key of MSI

For a project I have to test the signing of a MSI. The MSI has to be uploaded to a web server. The web server has to test the signing status of the MSI. The code below saves the MSI file (in the upload control) local. With help of the X509Certificate class a certificate object is instantiated. The SigningStatus is a enum.

internal static SigningStatus GetSigningStatus(FileUpload fileupload)
{
string fileName = Path.Combine(
Context.Server.MapPath(WebConfigurationManager.AppSettings["IprTempDir"]),
fileupload.FileName);

try
{
// Save file so it can be uses in constructor for the X509 certificate (constructor does
// not handle streams!)
fileupload.SaveAs(fileName);
13: X509Certificate2 x509msi = new X509Certificate2(fileName);
if (x509msi.GetHashCode() != 0)
{
X509Certificate2 key = new
X509Certificate2(StrToByteArray(WebConfigurationManager.AppSettings["PublicKey"]));

if (string.Compare(
x509msi.PublicKey.EncodedKeyValue.Format(false),
key.PublicKey.EncodedKeyValue.Format(false)) == 0)
{
// Public key web.config equal to public key of uploaded file => Company signed
return SigningStatus.CompanySigned;
}

// File is signed but not with the Company public key
return SigningStatus.Signed;
}
else
return SigningStatus.NotSigned;
}
catch (CryptographicException)
{
return SigningStatus.NotSigned;
}
finally
{
if (File.Exists(fileName))
{
File.Delete(fileName);
}
}
}
Share

Leave a Reply

Your email address will not be published. Required fields are marked *