cURL / Mailing Lists / curl-library / Single Mail

curl-library

Sending client certificate not working in simplessl.c example

From: Jaakko Pasanen <jasakki_at_gmail.com>
Date: Wed, 3 Jun 2009 09:31:41 +0300

Hi!

I am currently trying to make https-client which would use client
certificate for authenticating. From under examples folder I found
simplessl.c code snippet which I tried to use for connecting with my server,
but the example client doesn't seem to send any certificate to server.
Server says that the peer did not send any certificate and with Wireshark I
checked that client really doesn't send any certificate.

With command "curl -k -v --cert ./testcert.pem https://127.0.0.1" everything
works fine. Certificate is send and connection established correctly.

From simplessl.c I changed filenames for certificate and key (they both are
in the same file, I tried to put them into separate files, but didn't help
anything) and also changed CURLOPT_SSL_VERIFYPEER so that peer certificate
is not verified because both, server and client, are using self signed
certificates.

Is there something missing from code or what is the problem here?

-Jaakko

int main(int argc, char **argv)
{
  CURL *curl;
  CURLcode res;
  FILE *headerfile;
  const char *pPassphrase = NULL;

  static const char *pCertFile = "testcert.pem";
  static const char *pCACertFile="cacert.pem";

  const char *pKeyName;
  const char *pKeyType;

  const char *pEngine;

#if USE_ENGINE
  pKeyName = "rsa_test";
  pKeyType = "ENG";
  pEngine = "chil"; /* for nChiper HSM... */
#else
  pKeyName = "testcert.pem";
  pKeyType = "PEM";
  pEngine = NULL;
#endif

  headerfile = fopen("dumpit", "w");

  curl_global_init(CURL_GLOBAL_DEFAULT);

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

    while(1) /* do some ugly short cut... */
    {
      if (pEngine) /* use crypto engine */
      {
        if (curl_easy_setopt(curl, CURLOPT_SSLENGINE,pEngine) != CURLE_OK)
        { /* load the crypto engine */
          fprintf(stderr,"can't set crypto engine\n");
          break;
        }
        if (curl_easy_setopt(curl, CURLOPT_SSLENGINE_DEFAULT,1L) !=
CURLE_OK)
        { /* set the crypto engine as default */
          /* only needed for the first time you load
             a engine in a curl object... */
          fprintf(stderr,"can't set crypto engine as default\n");
          break;
        }
      }
      /* 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);

      /* sorry, for engine we must set the passphrase
         (if the key has one...) */
      if (pPassphrase)
        curl_easy_setopt(curl,CURLOPT_KEYPASSWD,pPassphrase);

      /* if we use a key stored in a crypto engine,
         we must set the key type to "ENG" */
      curl_easy_setopt(curl,CURLOPT_SSLKEYTYPE,pKeyType);

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

      /* set the file with the certs vaildating the server */
      curl_easy_setopt(curl,CURLOPT_CAINFO,pCACertFile);

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

      res = curl_easy_perform(curl);
      break; /* we are done... */
    }
    /* always cleanup */
    curl_easy_cleanup(curl);
  }

  curl_global_cleanup();

  return 0;
}
Received on 2009-06-03