cacertinmem.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2014, 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. /* Example using a "in core" PEM certificate to retrieve a https page.
  23. * Written by Theo Borm
  24. */
  25. /* on a netBSD system with OPENSSL& LIBCURL installed from
  26. * pkgsrc (using default paths) this program can be compiled using:
  27. * gcc -I/usr/pkg/include -L/usr/pkg/lib -lcurl -Wl,-R/usr/pkg/lib -lssl
  28. * -lcrypto -lz -o curlcacerttest curlcacerttest.c
  29. * on other operating systems you may want to change paths to headers
  30. * and libraries
  31. */
  32. #include <openssl/ssl.h>
  33. #include <curl/curl.h>
  34. #include <stdio.h>
  35. size_t writefunction( void *ptr, size_t size, size_t nmemb, void *stream)
  36. {
  37. fwrite(ptr,size,nmemb,stream);
  38. return(nmemb*size);
  39. }
  40. static CURLcode sslctx_function(CURL * curl, void * sslctx, void * parm)
  41. {
  42. X509_STORE * store;
  43. X509 * cert=NULL;
  44. BIO * bio;
  45. char * mypem = /* www.cacert.org */
  46. "-----BEGIN CERTIFICATE-----\n"\
  47. "MIIHPTCCBSWgAwIB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A2jxNGJARjtT6ij0rPtmlVOKTV39O9lg\n"\
  77. "18p5aTuxZZKmxoGCXJzN600BiqXfEVWqFcofN8CCmHBh22p8lqOOLlQ+TyGpkO/c\n"\
  78. "gr/c6EWtTZBzCDyUZbAEmXZ/4rzCahWqlwQ3JNgelE5tDlG+1sSPypZt90Pf6DBl\n"\
  79. "Jzt7u0NDY8RD97LsaMzhGY4i+5jhe1o+ATc7iwiwovOVThrLm82asduycPAtStvY\n"\
  80. "sONvRUgzEv/+PDIqVPfE94rwiCPCR/5kenHA0R6mY7AHfqQv0wGP3J8rtsYIqQ+T\n"\
  81. "SCX8Ev2fQtzzxD72V7DX3WnRBnc0CkvSyqD/HMaMyRa+xMwyN2hzXwj7UfdJUzYF\n"\
  82. "CpUCTPJ5GhD22Dp1nPMd8aINcGeGG7MW9S/lpOt5hvk9C8JzC6WZrG/8Z7jlLwum\n"\
  83. "GCSNe9FINSkYQKyTYOGWhlC0elnYjyELn8+CkcY7v2vcB5G5l1YjqrZslMZIBjzk\n"\
  84. "zk6q5PYvCdxTby78dOs6Y5nCpqyJvKeyRKANihDjbPIky/qbn3BHLt4Ui9SyIAmW\n"\
  85. "omTxJBzcoTWcFbLUvFUufQb1nA5V9FrWk9p2rSVzTMVD\n"\
  86. "-----END CERTIFICATE-----\n";
  87. /* get a BIO */
  88. bio=BIO_new_mem_buf(mypem, -1);
  89. /* use it to read the PEM formatted certificate from memory into an X509
  90. * structure that SSL can use
  91. */
  92. PEM_read_bio_X509(bio, &cert, 0, NULL);
  93. if (cert == NULL)
  94. printf("PEM_read_bio_X509 failed...\n");
  95. /* get a pointer to the X509 certificate store (which may be empty!) */
  96. store=SSL_CTX_get_cert_store((SSL_CTX *)sslctx);
  97. /* add our certificate to this store */
  98. if (X509_STORE_add_cert(store, cert)==0)
  99. printf("error adding certificate\n");
  100. /* decrease reference counts */
  101. X509_free(cert);
  102. BIO_free(bio);
  103. /* all set to go */
  104. return CURLE_OK ;
  105. }
  106. int main(void)
  107. {
  108. CURL * ch;
  109. CURLcode rv;
  110. rv=curl_global_init(CURL_GLOBAL_ALL);
  111. ch=curl_easy_init();
  112. rv=curl_easy_setopt(ch,CURLOPT_VERBOSE, 0L);
  113. rv=curl_easy_setopt(ch,CURLOPT_HEADER, 0L);
  114. rv=curl_easy_setopt(ch,CURLOPT_NOPROGRESS, 1L);
  115. rv=curl_easy_setopt(ch,CURLOPT_NOSIGNAL, 1L);
  116. rv=curl_easy_setopt(ch,CURLOPT_WRITEFUNCTION, *writefunction);
  117. rv=curl_easy_setopt(ch,CURLOPT_WRITEDATA, stdout);
  118. rv=curl_easy_setopt(ch,CURLOPT_HEADERFUNCTION, *writefunction);
  119. rv=curl_easy_setopt(ch,CURLOPT_HEADERDATA, stderr);
  120. rv=curl_easy_setopt(ch,CURLOPT_SSLCERTTYPE,"PEM");
  121. rv=curl_easy_setopt(ch,CURLOPT_SSL_VERIFYPEER,1L);
  122. rv=curl_easy_setopt(ch, CURLOPT_URL, "https://www.example.com/");
  123. /* first try: retrieve page without cacerts' certificate -> will fail
  124. */
  125. rv=curl_easy_perform(ch);
  126. if (rv==CURLE_OK)
  127. printf("*** transfer succeeded ***\n");
  128. else
  129. printf("*** transfer failed ***\n");
  130. /* second try: retrieve page using cacerts' certificate -> will succeed
  131. * load the certificate by installing a function doing the nescessary
  132. * "modifications" to the SSL CONTEXT just before link init
  133. */
  134. rv=curl_easy_setopt(ch,CURLOPT_SSL_CTX_FUNCTION, *sslctx_function);
  135. rv=curl_easy_perform(ch);
  136. if (rv==CURLE_OK)
  137. printf("*** transfer succeeded ***\n");
  138. else
  139. printf("*** transfer failed ***\n");
  140. curl_easy_cleanup(ch);
  141. curl_global_cleanup();
  142. return rv;
  143. }