public static X509Certificate CreateFromSignedFile (string filename)
{
try {
AuthenticodeDeformatter a = new AuthenticodeDeformatter (filename);
if (a.SigningCertificate != null) {
#if !NET_2_0
// before 2.0 the signing certificate is returned only if the signature is valid
if (a.Reason != 0) {
string msg = String.Format (Locale.GetText (
"Invalid digital signature on {0}, reason #{1}."),
filename, a.Reason);
throw new COMException (msg);
}
#endif
return new X509Certificate (a.SigningCertificate.RawData);
}
}
catch (SecurityException) {
// don't wrap SecurityException into a COMException
throw;
}
#if !NET_2_0
catch (COMException) {
// don't wrap COMException into a COMException
throw;
}
#endif
catch (Exception e) {
string msg = Locale.GetText ("Couldn't extract digital signature from {0}.", filename);
throw new COMException (msg, e);
}
#if NET_2_0
throw new CryptographicException (Locale.GetText ("{0} isn't signed.", filename));
#else
// if no signature is present return an empty certificate
byte[] cert = null; // must not confuse compiler about null ;)
return new X509Certificate (cert);
#endif
}