simplessl.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2011, Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at http://curl.haxx.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. ***************************************************************************/
  22. #include <stdio.h>
  23. #include <curl/curl.h>
  24. /* some requirements for this to work:
  25. 1. set pCertFile to the file with the client certificate
  26. 2. if the key is passphrase protected, set pPassphrase to the
  27. passphrase you use
  28. 3. if you are using a crypto engine:
  29. 3.1. set a #define USE_ENGINE
  30. 3.2. set pEngine to the name of the crypto engine you use
  31. 3.3. set pKeyName to the key identifier you want to use
  32. 4. if you don't use a crypto engine:
  33. 4.1. set pKeyName to the file name of your client key
  34. 4.2. if the format of the key file is DER, set pKeyType to "DER"
  35. !! verify of the server certificate is not implemented here !!
  36. **** This example only works with libcurl 7.9.3 and later! ****
  37. */
  38. int main(void)
  39. {
  40. CURL *curl;
  41. CURLcode res;
  42. FILE *headerfile;
  43. const char *pPassphrase = NULL;
  44. static const char *pCertFile = "testcert.pem";
  45. static const char *pCACertFile="cacert.pem";
  46. const char *pKeyName;
  47. const char *pKeyType;
  48. const char *pEngine;
  49. #ifdef USE_ENGINE
  50. pKeyName = "rsa_test";
  51. pKeyType = "ENG";
  52. pEngine = "chil"; /* for nChiper HSM... */
  53. #else
  54. pKeyName = "testkey.pem";
  55. pKeyType = "PEM";
  56. pEngine = NULL;
  57. #endif
  58. headerfile = fopen("dumpit", "w");
  59. curl_global_init(CURL_GLOBAL_DEFAULT);
  60. curl = curl_easy_init();
  61. if(curl) {
  62. /* what call to write: */
  63. curl_easy_setopt(curl, CURLOPT_URL, "HTTPS://your.favourite.ssl.site");
  64. curl_easy_setopt(curl, CURLOPT_WRITEHEADER, headerfile);
  65. while(1) /* do some ugly short cut... */
  66. {
  67. if (pEngine) /* use crypto engine */
  68. {
  69. if (curl_easy_setopt(curl, CURLOPT_SSLENGINE,pEngine) != CURLE_OK)
  70. { /* load the crypto engine */
  71. fprintf(stderr,"can't set crypto engine\n");
  72. break;
  73. }
  74. if (curl_easy_setopt(curl, CURLOPT_SSLENGINE_DEFAULT,1L) != CURLE_OK)
  75. { /* set the crypto engine as default */
  76. /* only needed for the first time you load
  77. a engine in a curl object... */
  78. fprintf(stderr,"can't set crypto engine as default\n");
  79. break;
  80. }
  81. }
  82. /* cert is stored PEM coded in file... */
  83. /* since PEM is default, we needn't set it for PEM */
  84. curl_easy_setopt(curl,CURLOPT_SSLCERTTYPE,"PEM");
  85. /* set the cert for client authentication */
  86. curl_easy_setopt(curl,CURLOPT_SSLCERT,pCertFile);
  87. /* sorry, for engine we must set the passphrase
  88. (if the key has one...) */
  89. if (pPassphrase)
  90. curl_easy_setopt(curl,CURLOPT_KEYPASSWD,pPassphrase);
  91. /* if we use a key stored in a crypto engine,
  92. we must set the key type to "ENG" */
  93. curl_easy_setopt(curl,CURLOPT_SSLKEYTYPE,pKeyType);
  94. /* set the private key (file or ID in engine) */
  95. curl_easy_setopt(curl,CURLOPT_SSLKEY,pKeyName);
  96. /* set the file with the certs vaildating the server */
  97. curl_easy_setopt(curl,CURLOPT_CAINFO,pCACertFile);
  98. /* disconnect if we can't validate server's cert */
  99. curl_easy_setopt(curl,CURLOPT_SSL_VERIFYPEER,1L);
  100. res = curl_easy_perform(curl);
  101. break; /* we are done... */
  102. }
  103. /* always cleanup */
  104. curl_easy_cleanup(curl);
  105. }
  106. curl_global_cleanup();
  107. return 0;
  108. }