delphimvcframework icon indicating copy to clipboard operation
delphimvcframework copied to clipboard

Request with certificate "Error opening certificate file" in Delphi 12.3

Open Alyssonpp opened this issue 6 months ago • 1 comments

The error only occurs in Delphi 12.3. When reverting to version 12.1 it works.

When making a request that requires sending the Certificate, the following message is displayed:

First chance exception at $00007FF9FA109F0A. Exception class ENetHTTPRequestException with message 'Error opening certificate file: (-2146885629) Erro ao ler ou gravar no arquivo'.

Below I demonstrate the code that aims to get the Token from a bank statement, this occurred in all requests involving a certificate to which I send the certificate "certificate.pfx"

The issue I have is that this only occurs in Delphi 12.3, in version 12.1 everything works fine, but I don't know if it's a DMVC problem or Delphi because they are different libraries to use. I had to migrate everything to Indy and then it worked.

To simulate the error you can make a request to another web service, the only need is to pass the certificate in the request as I do, I believe you will be able to simulate it.

procedure TR028extService.GerarToken; var fRESTClient: IMVCRESTClient; Res: IMVCRESTResponse; CertStream: TSmartPointer<TFileStream>; JSONObject: TSmartPointer<TJSONObject>; Const Url_Token = '/auth/realms/cooperado/protocol/openid-connect/token'; begin fRESTClient := ConexaoRestJson(pUrlAuth);

// Carrega o certificado em um TFileStream CertStream := TFileStream.Create(TConstantes.PathCertificadoA1, fmOpenRead or fmShareDenyWrite);

Res:= fRESTClient .AddHeader('Content-Type', 'application/x-www-form-urlencoded') .AddBodyFieldURLEncoded('grant_type', 'client_credentials') .AddBodyFieldURLEncoded('client_id', BuscarParametroDinamico(1,0,'API.SICOOB.CONTA.CORRENTE').ClientID) .AddBodyFieldURLEncoded('scope', 'cco_transferencias cco_consulta openid') .SetClientCertificate(CertStream, TConstantes.SenhaCertificadoA1) .Post(Url_Token); //HERE THE PROBLEM!!!!!! ENetHTTPRequestException with message 'Error opening certificate file: (-2146885629)

Try if Res.StatusCode = 200 Then Begin JSONObject := TJSONObject.ParseJSONValue(Res.Content) as TJSONObject; if Assigned(JSONObject.Value) then begin Token := JSONObject.Value.GetValue('access_token').Value; ExpireToken := IncSecond(Now, StrToIntDef(JSONObject.Value.GetValue('expires_in').Value, 0)); end;

End;

except on E: Exception do begin raise EMVCException.Create(500,'Erro:' + E.Message); end else raise; end;

end;

Alyssonpp avatar Jun 06 '25 20:06 Alyssonpp

The problem only occurs when passing the certificate in "Stream"

Example:

Var CertStream: TFileStream; CertStream := TFileStream.Create(TConstantes.PathCertificadoA1, fmOpenRead or fmShareDenyWrite);

this causes an error .SetClientCertificate(CertStream, TConstantes.SenhaCertificadoA1)

This is how it works by passing the Path .SetClientCertificate('certificado.pfx', TConstantes.SenhaCertificadoA1)

Alyssonpp avatar Jun 07 '25 12:06 Alyssonpp