user1583007
user1583007

Reputation: 507

Using LibCurl in C++ and self signed certificate

I've got a problem using libcurl and ssl.

If I try to connect to my site using the following curl command :

curl -q --cert client-2048.crt --key client-2048.key https://****** -d "username=&password=" -H "X-Application: curlCommandLineTest"

everything works well(the certificate is self signed by the way)

How can I do the same usign libcurl?

I tried to follow the libcurl ssl sample but the certificates and private key have different extensions so I don't know where to start.

So far I tried the following(and many other combinations) :

static const char *pCertFile = "client-2048.crt";
static const char *pCACertFile = "client-2048.pem";
static const char *pKeyName = "client-2048.key";

curl_global_init(CURL_GLOBAL_DEFAULT);

curl = curl_easy_init();
if (curl) {
    /* what call to write: */
    curl_easy_setopt(curl, CURLOPT_URL, "https://*****");
    curl_easy_setopt(curl, CURLOPT_HEADERDATA, headerfile);

        /* cert is stored PEM coded in file... */
        /* since PEM is default, we needn't set it for PEM */
        curl_easy_setopt(curl, CURLOPT_SSLCERTTYPE, "PEM");

        /* set the cert for client authentication */
        curl_easy_setopt(curl, CURLOPT_SSLCERT, pCertFile);

        /* set the private key (file or ID in engine) */
        curl_easy_setopt(curl, CURLOPT_SSLKEY, pKeyName);

        /* disconnect if we can't validate server's cert */
        curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1L);

        /* Perform the request, res will get the return code */
        res = curl_easy_perform(curl);
        /* Check for errors */
        if (res != CURLE_OK)
            fprintf(stderr, "curl_easy_perform() failed: %s\n",
                curl_easy_strerror(res));

        /* we are done... */
    } while (0);
    /* always cleanup */
    curl_easy_cleanup(curl);
    return 0;

But I get the message :

curl_easy_perform() failed: Peer certificate cannot be authenticated with 
given CA certificates

So what would it be the Libcurl code to mirror the call that succeed?

Thanks

Upvotes: 3

Views: 9362

Answers (1)

user1583007
user1583007

Reputation: 507

Self Signed Certificate :

curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);

Upvotes: 5

Related Questions