I am trying to use BouncyCastle library for generating X509Certificate in a .Net application, meanwhile i want to use keys stored in HSM.
My solution is generating EC key-pair in HSM, returning ECPoint and key lable to .Net application, and regenerate an elliptic key for signature generation.
After generating elliptic key, While checking its validity i got this error: UnManagedException: Public key presented not for certificate signature. This is the part of code from which Error raises:
X509Certificate rootCertificate = new X509Certificate (
new X509CertificateStructure (
TBS_Structure,
AlgorithmID,
new BitDERString(signature));
rootCertificate.Verify(PublicKeyParam);
PublicKeyParam is RSAKeyParameter and rebuilt based on Exponent and Modolus extracted from the library which made key on HSM (and returned key parameters). rootCertificate's algorithm is SHA256WithRSAandMGF1.
The problem is that for X509 certificates, elliptic key-point with separated
randsparts are required. The key-point whichPKCS11Interopreturns from HSM is a concatenatedbyte[]ofrands. So you should break it into twobyte[]and use following code instead ofsignatureinnew BitDERString(signature):This issue was explained in this question unintentionally but i couldn't get the point.