Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use OpenSSL in C++Builder

I'm compiling an app in C++Builder 10 Seattle, and trying to use OpenSSL for RSA work.

I followed this tutorial:

How to Use OpenSSL to Generate RSA Keys in C/C++

Here is the code:

#include <stdio.h>
#include <openssl/rsa.h>
#include <openssl/pem.h>

bool generate_key()
{
    int             ret = 0;
    RSA             *r = NULL;
    BIGNUM          *bne = NULL;
    BIO             *bp_public = NULL, *bp_private = NULL;

    int             bits = 2048;
    unsigned long   e = RSA_F4;

    // 1. generate rsa key
    bne = BN_new();
    ret = BN_set_word(bne,e);
    if(ret != 1){
        goto free_all;
    }

    r = RSA_new();
    ret = RSA_generate_key_ex(r, bits, bne, NULL);
    if(ret != 1){
        goto free_all;
    }

    // 2. save public key
    bp_public = BIO_new_file("public.pem", "w+");
    ret = PEM_write_bio_RSAPublicKey(bp_public, r);
    if(ret != 1){
        goto free_all;
    }

    // 3. save private key
    bp_private = BIO_new_file("private.pem", "w+");
    ret = PEM_write_bio_RSAPrivateKey(bp_private, r, NULL, NULL, 0, NULL, NULL);

    // 4. free
    free_all:

    BIO_free_all(bp_public);
    BIO_free_all(bp_private);
    RSA_free(r);
    BN_free(bne);

    return (ret == 1);
}

int main(int argc, char* argv[]) 
{
    generate_key();
    return 0;
}

When I added libeay32.lib and ssleay32.lib to my project, I got an error message:

[ilink32 Error] Error: 'C:\USERS\ERICWANG\DESKTOP\TESTOPENSSL2\LIB\LIBEAY32.LIB' contains invalid OMF record, type 0x21 (possibly COFF)

I've seen some tips like using coff2omf and implib tools, but both didn't work.

  1. I used coff2omf.exe to convert libeay32.lib. I put coff2omf.exe and libeay32.lib in the same folder, and entered this command:

    coff2omf libeay32.lib Blibeay32.lib
    

    It said:

    ERROR: COFF error: libeay32.lib : invalid machine type detected

  2. I tried to convert libeay32.lib to a .dll file using implib.exe. I entered this command:

    implib libeay32.lib Blibeay32.dll
    

    It said:

    Error : unable to open file

    And my libeay32.lib change its size to 1KB file. Which means the file was wrong.

like image 628
kim Avatar asked Mar 11 '23 14:03

kim


1 Answers

OpenSSL works just fine in C++Builder. I use it in my own C++Builder apps.

You cannot use the .lib files that are included with the pre-compiled OpenSSL DLLs. Those .lib files are meant for Visual Studio.

In C++Builder, you need to use C++Builder's implib or mkexp command-line utility to create C++Builder-compatible import libs from the DLLs. Use implib for 32bit DLLs and mkexp for 64bit DLLs.

It works fine, I have been using this technique for years when new OpenSSL versions are released.

Your implib command is wrong. You cannot "convert libeay32.lib to a .dll file". The first parameter is an output parameter, the second parameter is an input parameter. You need to create a .lib file for an existing DLL. There is no Blibeay32.dll file, which is why you are getting the "unable to open file" error. Drop the B and use the correct DLL filenames:

implib libeay32.lib libeay32.dll
implib ssleay32.lib ssleay32.dll

mkexp libeay32.a libeay32.dll
mkexp ssleay32.a ssleay32.dll

This will create .lib or .a files containing references for importing the functions from libeay32.dll and ssleay32.dll, respectively.

like image 94
Remy Lebeau Avatar answered Mar 19 '23 04:03

Remy Lebeau