cacertinmem.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2018, 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.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. /* <DESC>
  23. * CA cert in memory with OpenSSL to get a HTTPS page.
  24. * </DESC>
  25. */
  26. #include <openssl/err.h>
  27. #include <openssl/ssl.h>
  28. #include <curl/curl.h>
  29. #include <stdio.h>
  30. size_t writefunction(void *ptr, size_t size, size_t nmemb, void *stream)
  31. {
  32. fwrite(ptr, size, nmemb, (FILE *)stream);
  33. return (nmemb*size);
  34. }
  35. static CURLcode sslctx_function(CURL *curl, void *sslctx, void *parm)
  36. {
  37. CURLcode rv = CURLE_ABORTED_BY_CALLBACK;
  38. X509_STORE *store = NULL;
  39. X509 *cert = NULL;
  40. BIO *bio = NULL;
  41. char *mypem =
  42. /* CA for example.com. CN = DigiCert High Assurance EV Root CA */
  43. "-----BEGIN CERTIFICATE-----\n"
  44. "MIIDxTCCAq2gAwIBAgIQAqxcJmoLQJuPC3nyrkYldzANBgkqhkiG9w0BAQUFADBs\n"
  45. "MQswCQ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Uu4kehDLI6z\n"
  60. "eM7b41N5cdblIZQB2lWHmiRk9opmzN6cN82oNLFpmyPInngiK3BD41VHMWEZ71jF\n"
  61. "hS9OMPagMRYjyOfiZRYzy78aG6A9+MpeizGLYAiJLQwGXFK3xPkKmNEVX58Svnw2\n"
  62. "Yzi9RKR/5CYrCsSXaQ3pjOLAEFe4yHYSkVXySGnYvCoCWw9E1CAx2/S6cCZdkGCe\n"
  63. "vEsXCS+0yx5DaMkHJ8HSXPfqIbloEpw8nL+e/IBcm2PN7EeqJSdnoDfzAIJ9VNep\n"
  64. "+OkuE6N36B9K\n"
  65. "-----END CERTIFICATE-----\n";
  66. /* clear the current thread's OpenSSL error queue */
  67. ERR_clear_error();
  68. /* get a BIO */
  69. bio = BIO_new_mem_buf(mypem, -1);
  70. if(!bio)
  71. goto err;
  72. /* use it to read the PEM formatted certificate from memory into an X509
  73. * structure that SSL can use
  74. */
  75. if(!PEM_read_bio_X509(bio, &cert, 0, NULL))
  76. goto err;
  77. /* get a pointer to the X509 certificate store (which may be empty!) */
  78. store = SSL_CTX_get_cert_store((SSL_CTX *)sslctx);
  79. if(!store)
  80. goto err;
  81. /* add our certificate to this store */
  82. if(!X509_STORE_add_cert(store, cert)) {
  83. unsigned long error = ERR_peek_last_error();
  84. /* Ignore error X509_R_CERT_ALREADY_IN_HASH_TABLE which means the
  85. * certificate is already in the store. That could happen if
  86. * libcurl already loaded the certificate from a ca cert bundle
  87. * set at libcurl build-time or runtime.
  88. */
  89. if(ERR_GET_LIB(error) != ERR_LIB_X509 ||
  90. ERR_GET_REASON(error) != X509_R_CERT_ALREADY_IN_HASH_TABLE)
  91. goto err;
  92. ERR_clear_error();
  93. }
  94. rv = CURLE_OK;
  95. err:
  96. if(rv != CURLE_OK) {
  97. char errbuf[256];
  98. unsigned long error = ERR_peek_last_error();
  99. fprintf(stderr, "error adding certificate\n");
  100. if(error) {
  101. ERR_error_string_n(error, errbuf, sizeof(errbuf));
  102. fprintf(stderr, "%s\n", errbuf);
  103. }
  104. }
  105. X509_free(cert);
  106. BIO_free(bio);
  107. ERR_clear_error();
  108. return rv;
  109. }
  110. int main(void)
  111. {
  112. CURL *ch;
  113. CURLcode rv;
  114. rv = curl_global_init(CURL_GLOBAL_ALL);
  115. ch = curl_easy_init();
  116. rv = curl_easy_setopt(ch, CURLOPT_VERBOSE, 0L);
  117. rv = curl_easy_setopt(ch, CURLOPT_HEADER, 0L);
  118. rv = curl_easy_setopt(ch, CURLOPT_NOPROGRESS, 1L);
  119. rv = curl_easy_setopt(ch, CURLOPT_NOSIGNAL, 1L);
  120. rv = curl_easy_setopt(ch, CURLOPT_WRITEFUNCTION, *writefunction);
  121. rv = curl_easy_setopt(ch, CURLOPT_WRITEDATA, stdout);
  122. rv = curl_easy_setopt(ch, CURLOPT_HEADERFUNCTION, *writefunction);
  123. rv = curl_easy_setopt(ch, CURLOPT_HEADERDATA, stderr);
  124. rv = curl_easy_setopt(ch, CURLOPT_SSLCERTTYPE, "PEM");
  125. rv = curl_easy_setopt(ch, CURLOPT_SSL_VERIFYPEER, 1L);
  126. rv = curl_easy_setopt(ch, CURLOPT_URL, "https://www.example.com/");
  127. /* turn off the default CA locations (optional)
  128. * otherwise libcurl will load CA certificates from the locations that
  129. * were detected/specified at build-time
  130. */
  131. rv = curl_easy_setopt(ch, CURLOPT_CAINFO, NULL);
  132. rv = curl_easy_setopt(ch, CURLOPT_CAPATH, NULL);
  133. /* first try: retrieve page without ca certificates -> should fail
  134. * unless libcurl was built --with-ca-fallback enabled at build-time
  135. */
  136. rv = curl_easy_perform(ch);
  137. if(rv == CURLE_OK)
  138. printf("*** transfer succeeded ***\n");
  139. else
  140. printf("*** transfer failed ***\n");
  141. /* use a fresh connection (optional)
  142. * this option seriously impacts performance of multiple transfers but
  143. * it is necessary order to demonstrate this example. recall that the
  144. * ssl ctx callback is only called _before_ an SSL connection is
  145. * established, therefore it will not affect existing verified SSL
  146. * connections already in the connection cache associated with this
  147. * handle. normally you would set the ssl ctx function before making
  148. * any transfers, and not use this option.
  149. */
  150. rv = curl_easy_setopt(ch, CURLOPT_FRESH_CONNECT, 1L);
  151. /* second try: retrieve page using cacerts' certificate -> will succeed
  152. * load the certificate by installing a function doing the necessary
  153. * "modifications" to the SSL CONTEXT just before link init
  154. */
  155. rv = curl_easy_setopt(ch, CURLOPT_SSL_CTX_FUNCTION, *sslctx_function);
  156. rv = curl_easy_perform(ch);
  157. if(rv == CURLE_OK)
  158. printf("*** transfer succeeded ***\n");
  159. else
  160. printf("*** transfer failed ***\n");
  161. curl_easy_cleanup(ch);
  162. curl_global_cleanup();
  163. return rv;
  164. }