C & C++ difference.

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

C & C++ difference.

rohan shrivastava-2
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                    [hidden email]
Automated List Manager                           [hidden email]
Reply | Threaded
Open this post in threaded view
|

Re: C & C++ difference.

Tan Eng Ten
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                    [hidden email]
> Automated List Manager                           [hidden email]
>
>
______________________________________________________________________
OpenSSL Project                                 http://www.openssl.org
User Support Mailing List                    [hidden email]
Automated List Manager                           [hidden email]
Reply | Threaded
Open this post in threaded view
|

RE: C & C++ difference.

Usman Riaz
In reply to this post by rohan shrivastava-2

ALWAYS intialise your pointers :).

TestSSL()
{
      ctx = NULL;
      bio_err = NULL;
}

HTH,
Usman.

_________________________________________________________________
FREE pop-up blocking with the new MSN Toolbar - get it now!
http://toolbar.msn.click-url.com/go/onm00200415ave/direct/01/

______________________________________________________________________
OpenSSL Project                                 http://www.openssl.org
User Support Mailing List                    [hidden email]
Automated List Manager                           [hidden email]
Reply | Threaded
Open this post in threaded view
|

Re: C & C++ difference.

Gianni Mariani
Usman Riaz wrote:

>
> ALWAYS intialise your pointers :).
>
> TestSSL()
> {
>      ctx = NULL;
>      bio_err = NULL;
> }


A much better habbit to get into is to use the initialization syntax.

TestSSL()
  : ctx(),
    bio_err()
{
}

The advantage is that if you change your code so that ctx or bio_err is
a different (non POD) object, as happens when you templatize code, this
code will still do what you intended.

>
> HTH,
> Usman.
>

______________________________________________________________________
OpenSSL Project                                 http://www.openssl.org
User Support Mailing List                    [hidden email]
Automated List Manager                           [hidden email]
Reply | Threaded
Open this post in threaded view
|

RE: C & C++ difference.

rohan shrivastava-2
In reply to this post by Usman Riaz
Usman,
Thanks a lot it worked :)

-Rohan

>From: "Usman Riaz" <[hidden email]>
>Reply-To: [hidden email]
>To: [hidden email]
>Subject: RE: C & C++ difference.
>Date: Fri, 01 Jul 2005 15:48:24 +0500
>
>
>ALWAYS intialise your pointers :).
>
>TestSSL()
>{
>      ctx = NULL;
>      bio_err = NULL;
>}
>
>HTH,
>Usman.
>
>_________________________________________________________________
>FREE pop-up blocking with the new MSN Toolbar - get it now!
>http://toolbar.msn.click-url.com/go/onm00200415ave/direct/01/
>
>______________________________________________________________________
>OpenSSL Project                                 http://www.openssl.org
>User Support Mailing List                    [hidden email]
>Automated List Manager                           [hidden email]

_________________________________________________________________
Adore Arshad Warsi? http://server1.msn.co.in/sp05/iifa/ Think he is the
funniest?

______________________________________________________________________
OpenSSL Project                                 http://www.openssl.org
User Support Mailing List                    [hidden email]
Automated List Manager                           [hidden email]