simplessl.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 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 https://curl.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. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. /* <DESC>
  25. * Shows HTTPS usage with client certs and optional ssl engine use.
  26. * </DESC>
  27. */
  28. #include <stdio.h>
  29. #include <curl/curl.h>
  30. /* some requirements for this to work:
  31. 1. set pCertFile to the file with the client certificate
  32. 2. if the key is passphrase protected, set pPassphrase to the
  33. passphrase you use
  34. 3. if you are using a crypto engine:
  35. 3.1. set a #define USE_ENGINE
  36. 3.2. set pEngine to the name of the crypto engine you use
  37. 3.3. set pKeyName to the key identifier you want to use
  38. 4. if you do not use a crypto engine:
  39. 4.1. set pKeyName to the filename of your client key
  40. 4.2. if the format of the key file is DER, set pKeyType to "DER"
  41. !! verify of the server certificate is not implemented here !!
  42. **** This example only works with libcurl 7.9.3 and later! ****
  43. */
  44. int main(void)
  45. {
  46. CURL *curl;
  47. CURLcode res;
  48. FILE *headerfile;
  49. const char *pPassphrase = NULL;
  50. static const char *pCertFile = "testcert.pem";
  51. static const char *pCACertFile = "cacert.pem";
  52. static const char *pHeaderFile = "dumpit";
  53. const char *pKeyName;
  54. const char *pKeyType;
  55. const char *pEngine;
  56. #ifdef USE_ENGINE
  57. pKeyName = "rsa_test";
  58. pKeyType = "ENG";
  59. pEngine = "chil"; /* for nChiper HSM... */
  60. #else
  61. pKeyName = "testkey.pem";
  62. pKeyType = "PEM";
  63. pEngine = NULL;
  64. #endif
  65. headerfile = fopen(pHeaderFile, "wb");
  66. curl_global_init(CURL_GLOBAL_DEFAULT);
  67. curl = curl_easy_init();
  68. if(curl) {
  69. /* what call to write: */
  70. curl_easy_setopt(curl, CURLOPT_URL, "HTTPS://your.favourite.ssl.site");
  71. curl_easy_setopt(curl, CURLOPT_HEADERDATA, headerfile);
  72. #ifdef _MSC_VER
  73. #pragma warning(push)
  74. #pragma warning(disable:4127) /* conditional expression is constant */
  75. #endif
  76. do { /* dummy loop, just to break out from */
  77. if(pEngine) {
  78. /* use crypto engine */
  79. if(curl_easy_setopt(curl, CURLOPT_SSLENGINE, pEngine) != CURLE_OK) {
  80. /* load the crypto engine */
  81. fprintf(stderr, "cannot set crypto engine\n");
  82. break;
  83. }
  84. if(curl_easy_setopt(curl, CURLOPT_SSLENGINE_DEFAULT, 1L) != CURLE_OK) {
  85. /* set the crypto engine as default */
  86. /* only needed for the first time you load
  87. an engine in a curl object... */
  88. fprintf(stderr, "cannot set crypto engine as default\n");
  89. break;
  90. }
  91. }
  92. /* cert is stored PEM coded in file... */
  93. /* since PEM is default, we needn't set it for PEM */
  94. curl_easy_setopt(curl, CURLOPT_SSLCERTTYPE, "PEM");
  95. /* set the cert for client authentication */
  96. curl_easy_setopt(curl, CURLOPT_SSLCERT, pCertFile);
  97. /* sorry, for engine we must set the passphrase
  98. (if the key has one...) */
  99. if(pPassphrase)
  100. curl_easy_setopt(curl, CURLOPT_KEYPASSWD, pPassphrase);
  101. /* if we use a key stored in a crypto engine,
  102. we must set the key type to "ENG" */
  103. curl_easy_setopt(curl, CURLOPT_SSLKEYTYPE, pKeyType);
  104. /* set the private key (file or ID in engine) */
  105. curl_easy_setopt(curl, CURLOPT_SSLKEY, pKeyName);
  106. /* set the file with the certs validating the server */
  107. curl_easy_setopt(curl, CURLOPT_CAINFO, pCACertFile);
  108. /* disconnect if we cannot validate server's cert */
  109. curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1L);
  110. /* Perform the request, res gets the return code */
  111. res = curl_easy_perform(curl);
  112. /* Check for errors */
  113. if(res != CURLE_OK)
  114. fprintf(stderr, "curl_easy_perform() failed: %s\n",
  115. curl_easy_strerror(res));
  116. /* we are done... */
  117. } while(0);
  118. #ifdef _MSC_VER
  119. #pragma warning(pop)
  120. #endif
  121. /* always cleanup */
  122. curl_easy_cleanup(curl);
  123. }
  124. curl_global_cleanup();
  125. return 0;
  126. }