I am trying to get a certificate signature using CMS/Pkcs, it keeps prompting me a PIN code. How to disable this pin code pop-up or include it within the code (Note it is a fixed pin and it does not change)
Encoding utf8 = Encoding.UTF8;
byte[] bytes = utf8.GetBytes(serializedJson);
Pkcs11InteropFactories factories = new Pkcs11InteropFactories();
using (IPkcs11Library library = factories.Pkcs11LibraryFactory.LoadPkcs11Library(factories, this.DllLibPath, AppType.MultiThreaded))
{
ISlot slot = ((IEnumerable<ISlot>)library.GetSlotList(SlotsType.WithTokenPresent)).FirstOrDefault<ISlot>();
if (slot == null)
{
str = "No slots found";
}
else
{
ITokenInfo tokenInfo = slot.GetTokenInfo();
ISlotInfo slotInfo = slot.GetSlotInfo();
using (ISession session = slot.OpenSession(SessionType.ReadWrite))
{
session.Login(CKU.CKU_USER, utf8.GetBytes(this.TokenPin));
List<IObjectAttribute> list1 = new List<IObjectAttribute>();
list1.Add(session.Factories.ObjectAttributeFactory.Create(CKA.CKA_CLASS, CKO.CKO_CERTIFICATE));
list1.Add(session.Factories.ObjectAttributeFactory.Create(CKA.CKA_TOKEN, true));
list1.Add(session.Factories.ObjectAttributeFactory.Create(CKA.CKA_CERTIFICATE_TYPE, CKC.CKC_X_509));
if (((IEnumerable<IObjectHandle>)session.FindAllObjects(list1)).FirstOrDefault<IObjectHandle>() == null)
{
str = "Certificate not found";
}
else
{
X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
store.Open(OpenFlags.MaxAllowed);
X509Certificate2Collection certificates = store.Certificates.Find(X509FindType.FindByIssuerName, "Egypt Trust Sealing CA", true);
if (certificates.Count == 0)
{
str = "no device detected";
}
else
{
X509Certificate2 certificate = certificates[0];
store.Close();
SignedCms cms = new SignedCms(new ContentInfo(new Oid("1.2.840.113549.1.7.5"), bytes), true);
EssCertIDv2 dv = new EssCertIDv2(new Org.BouncyCastle.Asn1.X509.AlgorithmIdentifier(new DerObjectIdentifier("1.2.840.113549.1.9.16.2.47")), this.HashBytes(certificate.RawData));
EssCertIDv2[] certs = new EssCertIDv2[] { dv };
SigningCertificateV2 ev = new SigningCertificateV2(certs);
CmsSigner signer = new CmsSigner(certificate)
{
DigestAlgorithm = new Oid("2.16.840.1.101.3.4.2.1"),
SignedAttributes = {
new Pkcs9SigningTime(DateTime.UtcNow),
new AsnEncodedData(new Oid("1.2.840.113549.1.9.16.2.47"), ev.GetEncoded())
}
};
cms.ComputeSignature(signer);
Thank you in advance.