Jump to content
JavierBechir

Loading p12 certificate fails

Recommended Posts

I've installed component suit from overbyteics 8.16 in XE8 Delphi (Rad Studio).
When i want to open a p12 file in developer computer, it loads, but when application runs on Windows Server 2012 R2 raises "\r\nError on opening file \"c:\\Files\\certif\\clientkstore.p12\"\r\nerror:02001005:system library:fopen:Input\/output error\r\nerror:2006D002:BIO routines:BIO_new_file:system lib\r\n" message.
Development pc has openssl64 installed. W2012 R2 server: not.


c:\Files folder has high permissions for application users, for reading and writing.

 

The error is raised from OverbyteIcsWSocket unit:
 

2procedure myprocedure(myKey: string; var X: PPKC512; var Y: PX509);
var path: string;
    FileBio : PBIO;
    PKey  : PEVP_PKEY;
    ca: PSTACK;
begin
  path := 'c:\Files\certif\clientkstore.p12'; // read from ini or register
  InitializeSsl;
  FileBio := OpenFileBio(path, bomRead);		//<-- here raises. See next function down
  try
    if not Assigned(FileBio) then
        raise EX509Exception.Create('BIO not assigned');
    X := f_d2i_PKCS12_bio(FileBio, nil);
    if not Assigned(X) then
        RaiseLastOpenSslError(EX509Exception, TRUE,
                              'Error reading certificate from BIO PKC512');
    try
        if IncludePrivateKey then begin
            f_BIO_ctrl(FileBio, BIO_CTRL_RESET, 0, nil);
            PKey := f_PEM_read_bio_PrivateKey(FileBio, nil, nil,
                                              PAnsiChar(AnsiString(Password)));
            if not Assigned(PKey) then
                RaiseLastOpenSslError(EX509Exception, TRUE,
                                      'Error reading private key from BIO');
            try
                 X509       := X;
                 PrivateKey := PKey;
            finally
                f_EVP_PKEY_free(PKey);
            end;
        end else
            P12 := X;
    f_PKCS12_parse(P12, myKey, PKey, Y, ca);
    X509 := Y;
    finally
         f_PKCS12_free(X);
    end;
    finally
        f_bio_free(FileBio);
    end;
  end;
//........................................................................................//

function TX509Base.OpenFileBio(
    const FileName    : String;
    Methode           : TBioOpenMethode): PBIO;
begin
    if (Filename = '') then
        raise EX509Exception.Create('File name not specified');
    if (Methode = bomRead) and (not FileExists(Filename)) then
        raise EX509Exception.Create('File not found "' +
                                          Filename + '"');
    if Methode = bomRead then
        Result := f_BIO_new_file(PAnsiChar(AnsiString(Filename)), PAnsiChar('r+')) //<------ here raises
    else
        Result := f_BIO_new_file(PAnsiChar(AnsiString(Filename)), PAnsiChar('w+'));

    if (Result = nil) then
        RaiseLastOpenSslError(EX509Exception, TRUE,
                             'Error on opening file "' + Filename + '"');
end;

ssleay32.dll and libeay32.dll are in same folder than app.
How can solve it?

Share this post


Link to post
17 minutes ago, JavierBechir said:

overbyteics 8.16

This is an old version. Download the latest from http://wiki.overbyte.eu/wiki/index.php/ICS_Download  and don't forget to use the OpenSSL libraries included in that version.

 

Before trying your program, try with a sample program included with ICS to verify your setup is correct. I suggest you try with OverbyteIcsHttpsTst.dproj.

 

 

21 minutes ago, JavierBechir said:

"c:\\Files\\certif\\clientkstore.p12\"\r\nerror:02001005:system library:fopen:Input\/output error\r\nerror:2006D002:BIO routines:BIO_new_file:system lib\r\n" message.

This error comes from OpenSSL. The message means something like "file not found" or "Permission denied".

Share this post


Link to post

I've downloaded the 8.65 version, installed and runed...
libeay32 and ssleay32 are replaced by libcrypto-1_1 and  libssl-1_1 dlls.
Now is worst. Both in development pc, and server same exception is raised "Failed to load OpenSSL file libcrypto-1_1.dll ".
Both dll are in same folder than my application!! 😞

Share this post


Link to post
1 hour ago, JavierBechir said:

Both in development pc, and server same exception is raised "Failed to load OpenSSL file libcrypto-1_1.dll ".

With your own program of with the ICS demo OverbyteIcsHttpsTst.dproj?

1 hour ago, JavierBechir said:

Both dll are in same folder than my application!!

The DLL must be in the same folder as the EXE file, not the source code. Usually the EXE file is in a different folder. See project option "Output directory" in Delphi compiler option page.

 

To be sure the EXE is where you think it is, with Windows Explorer, go to that folder and delete the EXE file. Let the Explorer open on that folder. Then using Delphi, rebuilt the EXE file. You should see it appearing in Explorer window. Just to be sure.

 

You can also put a breakpoint in LibeayLoad  and ssleayLoad on the line having LoadLibrary called and see which filename is used.

 

 

 

Edited by FPiette

Share this post


Link to post

Why are writing your own code to read  P12 files, this was added years ago?   The PemTool sample opens and saves lots of formats. LoadFromP12File or just LoadFromFile will work.

 

Angus

 

Share this post


Link to post

I use LoadFromP12File.

I see a new variable:   GSSL_DLL_DIR;
GSSL_DLL_DIR := Copy(ExtractFileDir(GetModuleName(HInstance)),5,255) +'\';

Now it's ok, but raises error when load certificate: access denied, but folder has R/W permissions ... I'll be investigating more.
Thanks

Share this post


Link to post

GSSL_DLL_DIR is used when you want to access OpenSSL DLLs in non-standard locations.  It is normally left blank if the DLLs are in the same directory as the EXE or in the system path (not recommended due to multiple versions).

 

Angus

 

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×