I Have a delphi 7 CGI webService (in windows XP but windows 7 is not out of the table) application in witch i need to access digital certificate to sign a XML document.
I Have imported CApicom_TLB and successfuly got to instanciate the certificate, but with some problems...
the apache server that runs my app runs it with a different windows user in wich i installed the certificate, wich leaves the Certificate Store empty when i query with CAPICOM_CURRENT_USER_STORE flag. I worked-arround it by installing the A1 certificate (Pfx with privatekey) in the local machine (Via MMC console, add new snap-in) and accessing the Certificate Store with the CAPICOM_LOCAL_MACHINE_STORE flag. I get the certificate (i can read its serial number, friendly name, valid to date) but when i try to sign a document, i get "Key Pair does not exist" error.
The same code works (successfuly sign XML) in a normal APP (Not-cgi) with the same PFx.
Code i use to get the certificate:
Store := CoStore.Create;
Store.Open(CAPICOM_LOCAL_MACHINE_STORE, CAPICOM_STORE_NAME, CAPICOM_STORE_OPEN_MAXIMUM_ALLOWED);
Certs := Store.Certificates as ICertificates2;
for i:= 1 to Certs.Count do
begin
Cert := IInterface(Certs.Item[i]) as ICertificate2;
if Cert.SerialNumber = FNumeroSerie
then begin
if DFeUtil.EstaVazio(NumCertCarregado)
then NumCertCarregado := Cert.SerialNumber;
if CertStoreMem = nil
then begin
CertStoreMem := CoStore.Create;
CertStoreMem.Open(CAPICOM_MEMORY_STORE, 'Memoria', CAPICOM_STORE_OPEN_MAXIMUM_ALLOWED);
CertStoreMem.Add(Cert);
end;
Then i use the CertStoreMem to sign usign the folowing
OleCheck(IDispatch(Certificado.PrivateKey).QueryInterface(IPrivateKey,PrivateKey));
xmldsig.store := CertStoreMem;
dsigKey := xmldsig.createKeyFromCSP(PrivateKey.ProviderType, PrivateKey.ProviderName, PrivateKey.ContainerName, 0);
The error (Key pair) is in the last line of code.
There are two approaches: Make the CGI application read the certificate in the same user that installed it (code that works in non-cgi) OR make this work-arround with the localmachine-installed certificate work without key error.
If anyone could help would be much appreciated