Verify integrity Ceritifcate { RSACryptoServiceProvider - SHA1 - thumbprint }
-
06-07-2019 - |
Question
sorry for my english.
I have a little problem. I want to verify the integrity of my certificat.
I make this code:
using System.Security.Cryptography;
using System.Runtime.InteropServices;
using System.Security.Cryptography.X509Certificates;
SHA1Managed sha1 = new SHA1Managed();
RSACryptoServiceProvider csp = null;
AsymmetricAlgorithm rsaAlgo = certificatEnCours.PublicKey.Key;
byte[] data = null;
byte[] hash = null;
string keyPublic = "";
string signatureNumérique = "";
bool verif = false;
// ------------- PART 1 -------------
signatureNumérique = certificatEnCours.Thumbprint;
data = Convert.FromBase64String(signatureNumérique);
// ------------- PART 2 -------------
hash = sha1.ComputeHash(certificatEnCours.RawData);
keyPublic = rsaAlgo.ToXmlString(false);
csp = new RSACryptoServiceProvider();
csp.FromXmlString(keyPublic);
// ------------------------------
verif = csp.VerifyData(hash, CryptoConfig.MapNameToOID("SHA1"), data);
but i have already value "false
" for the var "verif
"
Solution
There is no actual question here. You are right that you are unconditionally ignoring the initial value of verif. More importantly, have you considered using X509Certificate2 to do verification?:
X509Certificate2 x2 = new X509Certificate2(certificatEnCours);
bool verif = x2.Verify();
I think this is wiser than re-inventing the wheel.
EDIT: If you are verifying a chain of certificates I believe you want to use X509Chain and in particular the ChainStatus property.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow