Error Certificate with label "PriKey" and id "" was not found

306 views
Skip to first unread message

katika.pa...@stream.co.th

unread,
Jun 23, 2017, 12:03:21 AM6/23/17
to Pkcs11Interop
Hi everyone,

I have some problem with example code from here
https://www.pkcs11interop.net/extensions/pdf/_pkcs11_rsa_signature_example_8cs-example.html

I need to sign a pdf document with certificate inside the HSM.

And here are steps what I've done.
1) Create KeyPair and the CKA_Label are PubKey and Prikey
2) Generate CSR and send to CA for signing
3) Import Signed Cert into HSM (CKA_LABEL is ClientCert)

And use this following code to sign the pdf document.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Pkcs;
using Org.BouncyCastle.X509;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.pdf.security;
using System.IO;
using Net.Pkcs11Interop.PDF;

namespace DocumentSigning
{
public class Program
{
public static void SignPdfDocument()
{
//Specify path to the unsigned PDF that will be created by this code
string unsignedPdfPath = @"D:\Hello.pdf";
//Specify path to the signed PDF that will be created by this code
string signedPdfPath = @"D:\Hello_signed.pdf";
//Create simple PDF document with iTextSharp
using (Document document = new Document(PageSize.A4, 50, 50, 50, 50))
{
using (FileStream outputStream = new FileStream(unsignedPdfPath, FileMode.Create))
{
using (PdfWriter pdfWriter = PdfWriter.GetInstance(document, outputStream))
{
document.Open();
document.Add(new Paragraph("Hello World!"));
document.Close();
}
}
}
//Do something interesting with unsigned PDF document
FileInfo unsignedPdfInfo = new FileInfo(unsignedPdfPath);
//Assert.IsTrue(unsignedPdfInfo.Length > 0);

//Specify path to the unmanaged PKCS#11 library
string libraryPath = @"cryptoki.dll";
//specify serial number of the token that contains signing key. May be null if tokenLabel is specified.
string tokenSerial = null;
//Specify label of the token that contains signing key. May be null if tokenSerial is specified.
string tokenLabel = @"Example";
//Specify PIN for the token
string pin = @"1111";
//Specify label (value of CKA_LABEL attribute) of the private key used for signing. May be null if CKA_ID is specified.
string ckalabel = @"PriKey";
//Specify hex encoded string with identifier (value of CKA_ID attribute) of the private key used for signing. May be null if CKA_LABEL is specified.
string ckaId = null;
//Specify hash algorithm used for the signature creation.
HashAlgorithm hashAlgorithm = HashAlgorithm.SHA256;
//Create instance of Pkcs11Signature class that allows iText to create PKCS#1v1.5 RSA signature with the private key stored on PKCS#11 compatible device.
using (Pkcs11RsaSignature pkcs11RsaSignature = new Pkcs11RsaSignature(libraryPath, tokenSerial, tokenLabel, pin, ckalabel, ckaId, hashAlgorithm))
{
//When signing certificate is stored on the token it can be usually read with GetSigningCertificate() method

/////////////////////THE ERROR OCCURED HERE///////////////////////////
byte[] signingCertificate = pkcs11RsaSignature.GetSigningCertificate();
//////////////////////////////////////////////////////////////////////

//All certificates stored on the token can be usually read with GetAllCertificates() method
List<byte[]> otherCertificates = pkcs11RsaSignature.GetAllCertificates();
//Build certificate path for the signing certificate.
ICollection<Org.BouncyCastle.X509.X509Certificate> certPath = CertUtils.BuildCertPath(signingCertificate, otherCertificates);
//Read unsigned PDF document
using (PdfReader pdfReader = new PdfReader(unsignedPdfPath))
{
//Create output stream for signed PDF document.
using (FileStream outputStream = new FileStream(signedPdfPath, FileMode.Create))
{
//Create PdfStamper that applies extra content to the PDF document.
using (PdfStamper pdfStamper = PdfStamper.CreateSignature(pdfReader, outputStream, '\0', Path.GetTempFileName(), true))
{
//Sign PDF document
MakeSignature.SignDetached(pdfStamper.SignatureAppearance, pkcs11RsaSignature, certPath, null, null, null, 0, CryptoStandard.CADES);
}
}
}
}
}
public static void Main(string[] args)
{
SignPdfDocument();
Console.ReadKey();
}
}
}

Jaroslav Imrich

unread,
Jun 23, 2017, 1:36:19 AM6/23/17
to Pkcs11Interop, katika.pa...@stream.co.th
Hello,

pkcs11RsaSignature.GetSigningCertificate() method requires both private key and certificate to have the exact same values of CKA_ID and/or CKA_LABEL attributes. See method implementation at https://github.com/jariq/Pkcs11Interop.PDF/blob/1.3.0/src/Pkcs11Interop.PDF/Pkcs11RsaSignature.cs#L209-L212 for more information.
If you are unable to set those attributes to the same values then you can read the certificate from any other source that suits your use case.

Regards, Jaroslav



--
You received this message because you are subscribed to the Google Groups "Pkcs11Interop" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pkcs11interop+unsubscribe@googlegroups.com.
To post to this group, send email to pkcs11...@googlegroups.com.
Visit this group at https://groups.google.com/group/pkcs11interop.

katika.pa...@stream.co.th

unread,
Jun 23, 2017, 1:57:43 AM6/23/17
to Pkcs11Interop, katika.pa...@stream.co.th
> To unsubscribe from this group and stop receiving emails from it, send an email to pkcs11intero...@googlegroups.com.
>
> To post to this group, send email to pkcs11...@googlegroups.com.
>
> Visit this group at https://groups.google.com/group/pkcs11interop.


Hi Jaroslav,

Thanks for your info.

May I ask another question.

Why the error "Provided certificates do not contain self-signed root certificate" occurred on this line of code

Collection<Org.BouncyCastle.X509.X509Certificate> certPath = CertUtils.BuildCertPath(signingCertificate, otherCertificates);

If you want more information please tell me. please suggest.

Jaroslav Imrich

unread,
Jun 23, 2017, 2:04:12 AM6/23/17
to Pkcs11Interop, katika.pa...@stream.co.th
Why the error "Provided certificates do not contain self-signed root certificate" occurred on this line of code

Collection<Org.BouncyCastle.X509.X509Certificate> certPath = CertUtils.BuildCertPath(signingCertificate, otherCertificates);

CertUtils.BuildCertPath() method builds the whole certificate chain up to a root certificate which is always self-signed. If that is not your case you don't need to use it and you can provide your own chain of certificates.

Regards, Jaroslav
Reply all
Reply to author
Forward
0 new messages