Signature from store

A forum for questions or concerns related to the PDF-XChange Core API SDK

Moderators: TrackerSupp-Daniel, Tracker Support, Vasyl-Tracker Dev Team, Chris - Tracker Supp, Sean - Tracker, Tracker Supp-Stefan

Forum rules
DO NOT post your license/serial key, or your activation code - these forums, and all posts within, are public and we will be forced to immediately deactivate your license.

When experiencing some errors, use the IAUX_Inst::FormatHRESULT method to see their description and include it in your post along with the error code.
Post Reply
Tom Princen
User
Posts: 83
Joined: Wed Mar 25, 2015 10:15 am

Signature from store

Post by Tom Princen »

How can i add a barcode from the certificate store?

I think I should use the DeferedDigitalSign Method

But it is not documented... How can I set the timestampserver? (I used PXCp_SetTSAServerW in the sdk)
Sasha - Tracker Dev Team
User
Posts: 5522
Joined: Fri Nov 21, 2014 8:27 am
Contact:

Re: Signature from store

Post by Sasha - Tracker Dev Team »

Hello Tom,

As far as I see, the analogy for the PXCp_SetTSAServerW would be this:
https://sdkhelp.pdf-xchange.com/vie ... dTimeStamp

Cheers,
Alex
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
Tom Princen
User
Posts: 83
Joined: Wed Mar 25, 2015 10:15 am

Re: Signature from store

Post by Tom Princen »

and do you have any info about the flags?

nFlags
[in] Value of ULONG.

does not mean a lot...
Tom Princen
User
Posts: 83
Joined: Wed Mar 25, 2015 10:15 am

Re: Signature from store

Post by Tom Princen »

oh and could you give some more info about:
https://sdkhelp.pdf-xchange.com/vie ... RT_CONTEXT
Sasha - Tracker Dev Team
User
Posts: 5522
Joined: Fri Nov 21, 2014 8:27 am
Contact:

Re: Signature from store

Post by Sasha - Tracker Dev Team »

Do you need the PXCp_SetTSAServerW analog or the DeferedDigitalSign method?
Because the right method to use in your case would be
https://sdkhelp.pdf-xchange.com/vie ... dTimeStamp
Anyways, I've updated the description of all of the Core API signature methods - you can check them out.

Cheers,
Alex
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
Tom Princen
User
Posts: 83
Joined: Wed Mar 25, 2015 10:15 am

Re: Signature from store

Post by Tom Princen »

I have this code:
I get memory errors in write to file WriteToFile.
Do you know what's wrong?

uint CERT_FIND_SUBJECT_STR = 0x00080007;
uint CERT_ENCODING = 0x00000001 | 0x00010000;// X509_ASN_ENCODING =0x00000001 | PKCS_7_ASN_ENCODING = 0x00010000
// uint CERT_NAME_SIMPLE_DISPLAY_TYPE = 4;


IntPtr hSysStore = IntPtr.Zero;
IntPtr hCertCntxt = IntPtr.Zero;

hSysStore = CertOpenSystemStore(IntPtr.Zero, "MY");// kijken in 'my' store

if (hSysStore == IntPtr.Zero)
{
Console.WriteLine("Unable to open System certifacte store!");
return 0;
}

// nu nog certificaat ophalen
hCertCntxt = CertFindCertificateInStore(
hSysStore,
CERT_ENCODING,
0,
CERT_FIND_SUBJECT_STR,
lsCertificateSubjectString,
IntPtr.Zero);


if (hCertCntxt == IntPtr.Zero)
{
Console.WriteLine("Certificate not found!");
return 0;
}


// pdf ondertekenen
InitPDFX(MyKey);

CDocAuthCallback clbk = new CDocAuthCallback();
clbk.m_pxsInst = MyIPXS;

IPXC_Document MydocSource = MyPXC.OpenDocumentFromFile(lsSourceFile, clbk);

//
if (lsTimeStampServer.Length != 0)
{
MydocSource.DeferedTimeStamp(lsTimeStampServer,"","");//res = FITdocPdfEditor.PXCp_SetTSAServerW(liPDFSource, lsTimeStampServer, "", "");
}


PXC_Rect rc;
/* res = PXCp_PageGetBox(liPDFSource, 0, PXC_PageBox.PB_CropBox, out rc);
if (res < 0)
res = FITdocPdfEditor.PXCp_PageGetBox(liPDFSource, 0, FITdocPdfEditor.PXC_PageBox.PB_MediaBox, out rc);
*/

rc.left = 0;
rc.top = 0;
rc.right = rc.left + 144;
rc.bottom = rc.top - 36;



uint MyFlags = (uint)(PXC_SignDocumentFlags.Sign_GR_Name | PXC_SignDocumentFlags.Sign_TX_Name | PXC_SignDocumentFlags.Sign_TX_Date |
PXC_SignDocumentFlags.Sign_TX_Location | PXC_SignDocumentFlags.Sign_TX_Reason | PXC_SignDocumentFlags.Sign_TX_DName);

_CERT_CONTEXT MyContext = (_CERT_CONTEXT) System.Runtime.InteropServices.Marshal.PtrToStructure(hCertCntxt, typeof(_CERT_CONTEXT));




MydocSource.DeferedDigitalSign(ref MyContext, MyFlags, 0, ref rc, "Signed by ", "", "Support@...", "");


MydocSource.WriteToFile(lsDestinationFile);
Sasha - Tracker Dev Team
User
Posts: 5522
Joined: Fri Nov 21, 2014 8:27 am
Contact:

Re: Signature from store

Post by Sasha - Tracker Dev Team »

Hello Tom,

I can't say why you are getting errors - I can't try the code that you've provided on my side - there are unimplemented methods there.
Thus, if you want more efficient help - provide us ether with working code or even better - with working project that demonstrates the problem.

Cheers,
Alex
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
Tom Princen
User
Posts: 83
Joined: Wed Mar 25, 2015 10:15 am

Re: Signature from store

Post by Tom Princen »

This is the only method that is missing:
private void InitPDFX(string lsKey)
{
MyPXC.Init(lsKey);
//MyIPXC = (IPXC_Inst)MyPXC.GetExtension("PXC");
MyIPXS = (IPXS_Inst)MyPXC.GetExtension("PXS");
MyIAUX = (IAUX_Inst)MyPXC.GetExtension("AUX");
}

The callback class you should have:
class CDocAuthCallback : IPXC_DocAuthCallback
{
public IPXS_Inst m_pxsInst = null;
public void AuthDoc(IPXC_Document pDoc, uint nFlags)
{
if ((pDoc == null) || (m_pxsInst == null))
{
return;//throw new COMException("", VSConstants.E_INVALIDARG);
}
//Checking whether the document has any security
UInt32 nNameAtom = pDoc.GetSecurityHandlerType(false);
if (nNameAtom == 0)
{
//No security so successfully opened doc
//...
return;//throw new COMException("", VSConstants.S_OK);
}
//Else we must provide a password
UInt32 nStandard = m_pxsInst.StrToAtom("Standard");
PXC_PermStatus status = PXC_PermStatus.Perm_ReqGranted;
//Comparing whether the security handler type is Standard
if (nStandard != nNameAtom)
{
//We do not support this one
return;//throw new COMException("", VSConstants.E_INVALIDARG);
}
status = pDoc.AuthorizeWithPassword("YourPasswordHere");
if (status != PXC_PermStatus.Perm_ReqGranted)
{
//We don't have permission to open doc
return;//throw new COMException("", VSConstants.E_NOTIMPL);
}
}
}


And these are the functions from crypt dll:

[DllImport("crypt32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern IntPtr CertOpenSystemStore(
IntPtr hCryptProv,
string storename);

[DllImport("crypt32.dll", SetLastError = true)]
public static extern bool CertCloseStore(
IntPtr hCertStore,
uint dwFlags);

[DllImport("crypt32.dll", SetLastError = true)]
public static extern IntPtr CertFindCertificateInStore(
IntPtr hCertStore,
uint dwCertEncodingType,
uint dwFindFlags,
uint dwFindType,
[In, MarshalAs(UnmanagedType.LPWStr)]String pszFindString,
IntPtr pPrevCertCntxt);

[DllImport("crypt32.dll", SetLastError = true)]
public static extern bool CertFreeCertificateContext(
IntPtr hCertStore);

[DllImport("crypt32.dll", SetLastError = true)]
public static extern IntPtr CertEnumCertificatesInStore(IntPtr storeProvider, IntPtr prevCertContext);

[DllImport("crypt32.dll", EntryPoint = "CertGetNameString", CharSet = CharSet.Auto, SetLastError = true)]
static extern UInt32 CertGetNameString(IntPtr CertContext, UInt32 lType, UInt32 lFlags, IntPtr pTypeParameter, System.Text.StringBuilder str, UInt32 cch);
Sasha - Tracker Dev Team
User
Posts: 5522
Joined: Fri Nov 21, 2014 8:27 am
Contact:

Re: Signature from store

Post by Sasha - Tracker Dev Team »

After I wrote my message, I've realized that these methods are from DllImport.
Will try to recreate the behavior that you've experienced and will write back.

Cheers,
Alex
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
Tom Princen
User
Posts: 83
Joined: Wed Mar 25, 2015 10:15 am

Re: Signature from store

Post by Tom Princen »

Any news?
Sasha - Tracker Dev Team
User
Posts: 5522
Joined: Fri Nov 21, 2014 8:27 am
Contact:

Re: Signature from store

Post by Sasha - Tracker Dev Team »

We've recreated the issue and told the appropriate developer.
He'll write back as soon as he has managed to investigate the issue thoroughly.

Cheers,
Alex
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
Tom Princen
User
Posts: 83
Joined: Wed Mar 25, 2015 10:15 am

Re: Signature from store

Post by Tom Princen »

Any temporary work around?
User avatar
Tracker Supp-Stefan
Site Admin
Posts: 17820
Joined: Mon Jan 12, 2009 8:07 am
Location: London
Contact:

Re: Signature from store

Post by Tracker Supp-Stefan »

Hello Tom,

Not for the moment. As Sasha said - at this point we will need to wait for the colleague working on signatures to look at it and apply the fix.

Regards,
Stefan
Sasha - Tracker Dev Team
User
Posts: 5522
Joined: Fri Nov 21, 2014 8:27 am
Contact:

Re: Signature from store

Post by Sasha - Tracker Dev Team »

Hello Tom,

We've investigated the problem and even found a solution to it.
Simply speaking the problem was in this piece of code:

Code: Select all

PDFXEdit._CERT_CONTEXT MyContext = (PDFXEdit._CERT_CONTEXT)System.Runtime.InteropServices.Marshal.PtrToStructure(hCertCntxt, typeof(PDFXEdit._CERT_CONTEXT));
Doc.DeferedDigitalSign(ref MyContext, MyFlags, 0, ref rc, "Signed by", "", "Support@...", "");
The thing is that the C# copied the structure into another structure and after this procedure, the pointers inside them became invalid thus resulting the violation. We've managed to see this after we implemented the same code on c++ and found out this:
https://gist.github.com/Polaringu/a9c80 ... 7e07cbdc12
Also the Delphi code behaved like the c++ code did.
In C# we couldn't do the same procedure without structure copying, but we've found a solution using an unsafe (with the /unsafe flag specified in the project's properties) code:

Code: Select all

unsafe private void SignDocument(PDFXEdit.IPXC_Document Doc)
{
...
PDFXEdit._CERT_CONTEXT* pMyContext = (PDFXEdit._CERT_CONTEXT*)hCertCntxt.ToPointer();
Doc.DeferedDigitalSign(ref *pMyContext, MyFlags, 0, ref rc, "Signed by", "", "Support@...", "");
Doc.WriteToFile("D:\\TestFile_res.pdf");
}
Cheers,
Alex
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
jadsonbr
User
Posts: 7
Joined: Tue Sep 13, 2016 11:41 am

Re: Signature from store

Post by jadsonbr »

Would you have this example signature in Delphi?
Sasha - Tracker Dev Team
User
Posts: 5522
Joined: Fri Nov 21, 2014 8:27 am
Contact:

Re: Signature from store

Post by Sasha - Tracker Dev Team »

Hello jadsonbr,

Here's what we have from our experiments:

Code: Select all

procedure TInst.SignDocument(doc: PDFXEdit_TLB.IPXC_Document);
const
  CERT_FIND_SUBJECT_STR = $00080007;
  CERT_ENCODING = $00000001 or $00010000;
  // X509_ASN_ENCODING =0x00000001 | PKCS_7_ASN_ENCODING = 0x00010000
  MyFlags = (PDFXEdit_TLB.Sign_GR_Name or PDFXEdit_TLB.Sign_TX_Name or
    PDFXEdit_TLB.Sign_TX_Date or PDFXEdit_TLB.Sign_TX_Location or
    PDFXEdit_TLB.Sign_TX_Reason or PDFXEdit_TLB.Sign_TX_DName);
var
  strCertSubject: WideString;
  // lpszCertSubject: PWideChar;
  lpszCertSubject: array [0 .. 64] of WideChar;
  hSysStore: HCERTSTORE;
  hCertCntxt: PCCERT_CONTEXT;
  rc: PDFXEdit_TLB.PXC_Rect;
  MyContext: PDFXEdit_TLB.PUserType8;
begin
  hSysStore := CertOpenSystemStore(0, 'MY'); // kijken in 'my' store
  if (hSysStore = nil) then
  begin
    Assert(false, 'CertOpenSystemStore');
    exit;
  end;
  strCertSubject := 'dima';
  StringToWideChar(strCertSubject, lpszCertSubject, Length(lpszCertSubject));
  hCertCntxt := CertFindCertificateInStore(hSysStore, CERT_ENCODING,
    CERT_FIND_ANY, CERT_FIND_SUBJECT_STR, @lpszCertSubject, nil);
  if (hCertCntxt = nil) then
  Begin
    Assert(false, 'CertFindCertificateInStore');
    exit;
  End;

  rc.left := 0;
  rc.top := 36;
  rc.right := rc.left + 144;
  rc.bottom := 0;
  MyContext := PDFXEdit_TLB.PUserType8(hCertCntxt);
  // try
  // Doc.DeferedDigitalSign(MyContext^, MyFlags, 0, rc, 'Signed by', '', 'Support@...', '');
  // Doc.DeferedDigitalSign(PDFXEdit_TLB.PUserType8(hCertCntxt)^, MyFlags, 0, rc, 'Signed by', '', 'Support@...', '');
  { except
    on e: Exception do
    Assert(false, e.Message);
    end; }
  doc.WriteToFile('c:\Users\oliyn\Documents\TestFile_res.pdf', nil, 0);
end;
Cheers,
Alex
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
Post Reply