cacertinmem.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2019, 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. static 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. /** This example uses two (fake) certificates **/
  39. static const char mypem[] =
  40. "-----BEGIN CERTIFICATE-----\n"
  41. "MIIH0zCCBbugAwIBAgIIXsO3pkN/pOAwDQYJKoZIhvcNAQEFBQAwQjESMBAGA1UE\n"
  42. "AwwJQUNDVlJBSVoxMRAwDgYDVQQLDAdQS0lBQ0NWMQ0wCwYDVQQKDARBQ0NWMQs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gJHnyqobalbz+xF\n"
  58. "d3+YJ5oyXSrjhO7FmGYvliAd3djDJ9ew+f7Zfc3Qn48LFFhRny+Lwzgt3uiP1o2H\n"
  59. "pPVWQxaZLPSkVrQ0uGE3ycJYgBugl6H8WY3pEfbRD0tVNEYqi4Y7\n"
  60. "-----END CERTIFICATE-----\n"
  61. "-----BEGIN CERTIFICATE-----\n"
  62. "MIIFtTCCA52gAwIBAgIIYY3HhjsBggUwDQYJKoZIhvcNAQEFBQAwRDEWMBQGA1UE\n"
  63. "AwwNQUNFRElDT00gUm9vdDEMMAoGA1UECwwDUEtJMQ8wDQYDVQQKDAZFRElDT00x\n"
  64. "CzAJBgNVBAYTAkVTMB4XDTA4MDQxODE2MjQyMloXDTI4MDQxMzE2MjQyMlowRDEW\n"
  65. "MBQGA1UEAwwNQUNFRElDT00gUm9vdDEMMAoGA1UECwwDUEtJMQ8wDQYDVQQKDAZF\n"
  66. "RElDT00xCzAJBgNVBAYTAkVTMIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKC\n"
  67. "AgEA/5KV4WgGdrQsyFhIyv2AVClVYyT/kGWbEHV7w2rbYgIB8hiGtXxaOLHkWLn7\n"
  68. "09gtn70yN78sFW2+tfQh0hOR2QetAQXW8713zl9CgQr5auODAKgrLlUTY4HKRxx7\n"
  69. "XBZXehuDYAQ6PmXDzQHe3qTWDLqO3tkE7hdWIpuPY/1NFgu3e3eM+SW10W2ZEi5P\n"
  70. "gvoFNTPhNahXwOf9jU8/kzJPeGYDdwdY6ZXIfj7QeQCM8htRM5u8lOk6e25SLTKe\n"
  71. "I6RF+7YuE7CLGLHdztUdp0J/Vb77W7tH1PwkzQSulgUV1qzOMPPKC8W64iLgpq0i\n"
  72. "5ALudBF/TP94HTXa5gI06xgSYXcGCRZj6hitoocf8seACQl1ThCojz2GuHURwCRi\n"
  73. "ipZ7SkXp7FnFvmuD5uHorLUwHv4FB4D54SMNUI8FmP8sX+g7tq3PgbUhh8oIKiMn\n"
  74. "MCArz+2UW6yyetLHKKGKC5tNSixthT8Jcjxn4tncB7rrZXtaAWPWkFtPF2Y9fwsZ\n"
  75. "o5NjEFIqnxQWWOLcpfShFosOkYuByptZ+thrkQdlVV9SH686+5DdaaVbnG0OLLb6\n"
  76. "zqylfDJKZ0DcMDQj3dcEI2bw/FWAp/tmGYI1Z2JwOV5vx+qQQEQIHriy1tvuWacN\n"
  77. "GHk0vFQYXlPKNFHtRQrmjseCNj6nOGOpMCwXEGCSn1WHElkQwg9naRHMTh5+Spqt\n"
  78. "r0CodaxWkHS4oJyleW/c6RrIaQXpuvoDs3zk4E7Czp3otkYNbn5XOmeUwssfnHdK\n"
  79. "Z05phkOTOPu220+DkdRgfks+KzgHVZhepA==\n"
  80. "-----END CERTIFICATE-----\n";
  81. BIO *cbio = BIO_new_mem_buf(mypem, sizeof(mypem));
  82. X509_STORE *cts = SSL_CTX_get_cert_store((SSL_CTX *)sslctx);
  83. int i;
  84. STACK_OF(X509_INFO) *inf;
  85. (void)curl;
  86. (void)parm;
  87. if(!cts || !cbio) {
  88. return rv;
  89. }
  90. inf = PEM_X509_INFO_read_bio(cbio, NULL, NULL, NULL);
  91. if(!inf) {
  92. BIO_free(cbio);
  93. return rv;
  94. }
  95. for(i = 0; i < sk_X509_INFO_num(inf); i++) {
  96. X509_INFO *itmp = sk_X509_INFO_value(inf, i);
  97. if(itmp->x509) {
  98. X509_STORE_add_cert(cts, itmp->x509);
  99. }
  100. if(itmp->crl) {
  101. X509_STORE_add_crl(cts, itmp->crl);
  102. }
  103. }
  104. sk_X509_INFO_pop_free(inf, X509_INFO_free);
  105. BIO_free(cbio);
  106. rv = CURLE_OK;
  107. return rv;
  108. }
  109. int main(void)
  110. {
  111. CURL *ch;
  112. CURLcode rv;
  113. curl_global_init(CURL_GLOBAL_ALL);
  114. ch = curl_easy_init();
  115. curl_easy_setopt(ch, CURLOPT_VERBOSE, 0L);
  116. curl_easy_setopt(ch, CURLOPT_HEADER, 0L);
  117. curl_easy_setopt(ch, CURLOPT_NOPROGRESS, 1L);
  118. curl_easy_setopt(ch, CURLOPT_NOSIGNAL, 1L);
  119. curl_easy_setopt(ch, CURLOPT_WRITEFUNCTION, *writefunction);
  120. curl_easy_setopt(ch, CURLOPT_WRITEDATA, stdout);
  121. curl_easy_setopt(ch, CURLOPT_HEADERFUNCTION, *writefunction);
  122. curl_easy_setopt(ch, CURLOPT_HEADERDATA, stderr);
  123. curl_easy_setopt(ch, CURLOPT_SSLCERTTYPE, "PEM");
  124. curl_easy_setopt(ch, CURLOPT_SSL_VERIFYPEER, 1L);
  125. curl_easy_setopt(ch, CURLOPT_URL, "https://www.example.com/");
  126. /* Turn off the default CA locations, otherwise libcurl will load CA
  127. * certificates from the locations that were detected/specified at
  128. * build-time
  129. */
  130. curl_easy_setopt(ch, CURLOPT_CAINFO, NULL);
  131. curl_easy_setopt(ch, CURLOPT_CAPATH, NULL);
  132. /* first try: retrieve page without ca certificates -> should fail
  133. * unless libcurl was built --with-ca-fallback enabled at build-time
  134. */
  135. rv = curl_easy_perform(ch);
  136. if(rv == CURLE_OK)
  137. printf("*** transfer succeeded ***\n");
  138. else
  139. printf("*** transfer failed ***\n");
  140. /* use a fresh connection (optional)
  141. * this option seriously impacts performance of multiple transfers but
  142. * it is necessary order to demonstrate this example. recall that the
  143. * ssl ctx callback is only called _before_ an SSL connection is
  144. * established, therefore it will not affect existing verified SSL
  145. * connections already in the connection cache associated with this
  146. * handle. normally you would set the ssl ctx function before making
  147. * any transfers, and not use this option.
  148. */
  149. curl_easy_setopt(ch, CURLOPT_FRESH_CONNECT, 1L);
  150. /* second try: retrieve page using cacerts' certificate -> will succeed
  151. * load the certificate by installing a function doing the necessary
  152. * "modifications" to the SSL CONTEXT just before link init
  153. */
  154. curl_easy_setopt(ch, CURLOPT_SSL_CTX_FUNCTION, *sslctx_function);
  155. rv = curl_easy_perform(ch);
  156. if(rv == CURLE_OK)
  157. printf("*** transfer succeeded ***\n");
  158. else
  159. printf("*** transfer failed ***\n");
  160. curl_easy_cleanup(ch);
  161. curl_global_cleanup();
  162. return rv;
  163. }