Hi Rohan,

Why do you need "if (!bio_err)" at the start if program?


rohan shrivastava wrote:
Hello,

I have written a program in C & C++, for testing OpenSSL.
The C program works fine, whereas in C++ I receive
segmentation fault error while executing SSL_CTX_use_certificate_file() function.

Any idea why I am getting this error?

---------------
C Pgm Start
---------------

#include "openssl/ssl.h"

#define CERTFILE "server_crt.pem"
#define PRIVKEYFILE "server.pem"
    SSL_CTX *ctx;
    BIO *bio_err;

void init_ctx(const char *certfile, char *privkey) {
    SSL_METHOD *meth;

    if (!bio_err) {
        SSL_library_init();
        SSL_load_error_strings();
    }

    meth = SSLv3_method();
    ctx = SSL_CTX_new(meth);

    if (!(SSL_CTX_use_certificate_file(ctx,certfile,SSL_FILETYPE_PEM))) {
        printf("\n Error in reading Certificate file");
        exit(0);
    }
    else
        printf("\n Certificate file is loaded \n");

    if (!(SSL_CTX_use_PrivateKey_file(ctx,privkey,SSL_FILETYPE_PEM))) {
        printf("\n Error in reading Private key file");
        exit(0);
    }
    else
        printf("\n Privatekey file is loaded \n");
}

main() {
    init_ctx(CERTFILE,PRIVKEYFILE);

}

---------------
C Pgm End
---------------

compiled as gcc testssl.c -lssl -L/usr/lib/

--------------
C++ Pgm Start
---------------
#include <iostream.h>
#include "openssl/ssl.h"

#define CERTFILE "server_crt.pem"
#define PRIVKEYFILE "server.pem"

class TestSSL {

private:
    SSL_CTX *ctx;
    BIO *bio_err;

public:
       void init_ctx(const char *certfile, char *privkey) {
        SSL_METHOD *meth;
        if (!bio_err) {
            SSL_library_init();
            SSL_load_error_strings();
        }

        meth = SSLv3_method();
        ctx = SSL_CTX_new(meth);

if (!(SSL_CTX_use_certificate_file(ctx,certfile,SSL_FILETYPE_PEM))) {
            printf("\n Error in reading Certificate file");
            exit(0);
        }
        else {
            printf("\n Certificate file is loaded \n");
         }

        if (!(SSL_CTX_use_PrivateKey_file(ctx,privkey,SSL_FILETYPE_PEM))) {
            printf("\n Error in reading Private key file");
            exit(0);
        }
        else {
            printf("\n Privatekey file is loaded \n");
         }

    }

};

main() {
    TestSSL ts;
    ts.init_ctx(CERTFILE,PRIVKEYFILE);
}

---------------
C++ Pgm End
---------------

compiled as g++ testssl.cpp -lssl -L/usr/lib/


Any help will be highly appreciated.

Thanks
-Rohan

_________________________________________________________________
Millions of marriage proposals. http://www.bharatmatrimony.com/cgi-bin/bmclicks1.cgi?74 Find your match on BharatMatrimony.com

______________________________________________________________________
OpenSSL Project                                 http://www.openssl.org
User Support Mailing List                    openssl-users@openssl.org
Automated List Manager                           [EMAIL PROTECTED]


______________________________________________________________________
OpenSSL Project                                 http://www.openssl.org
User Support Mailing List                    openssl-users@openssl.org
Automated List Manager                           [EMAIL PROTECTED]

Reply via email to