2
0

usercertinmem.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. * Use an in-memory user certificate and RSA key and retrieve an https page.
  26. * </DESC>
  27. */
  28. /* Written by Ishan SinghLevett, based on Theo Borm's cacertinmem.c.
  29. * Note that to maintain simplicity this example does not use a CA certificate
  30. * for peer verification. However, some form of peer verification
  31. * must be used in real circumstances when a secure connection is required.
  32. */
  33. #ifndef OPENSSL_SUPPRESS_DEPRECATED
  34. #define OPENSSL_SUPPRESS_DEPRECATED
  35. #endif
  36. #include <openssl/ssl.h>
  37. #include <openssl/x509.h>
  38. #include <openssl/pem.h>
  39. #include <curl/curl.h>
  40. #include <stdio.h>
  41. static size_t writefunction(void *ptr, size_t size, size_t nmemb, void *stream)
  42. {
  43. fwrite(ptr, size, nmemb, stream);
  44. return (nmemb*size);
  45. }
  46. static CURLcode sslctx_function(CURL *curl, void *sslctx, void *parm)
  47. {
  48. X509 *cert = NULL;
  49. BIO *bio = NULL;
  50. BIO *kbio = NULL;
  51. RSA *rsa = NULL;
  52. int ret;
  53. #if defined(__GNUC__) || defined(__clang__)
  54. #pragma GCC diagnostic push
  55. #pragma GCC diagnostic ignored "-Woverlength-strings"
  56. #endif
  57. const char *mypem = /* www.cacert.org */
  58. "-----BEGIN CERTIFICATE-----\n"\
  59. "MIIHPTCCBSWgAwIBAgIBADANBgkqhkiG9w0BAQQFADB5MRAwDgYDVQQKEwdSb290\n"\
  60. "IENBMR4wHAYDVQQLExVodHRwOi8vd3d3LmNhY2VydC5vcmcxIjAgBgNVBAMTGUNB\n"\
  61. "IENlcnQgU2lnbmluZyBBdXRob3JpdHkxITAfBgkqhkiG9w0BCQEWEnN1cHBvcnRA\n"\
  62. "Y2FjZXJ0Lm9yZzAeFw0wMzAzMzAxMjI5NDlaFw0zMzAzMjkxMjI5NDlaMHkxEDAO\n"\
  63. "BgNVBAoTB1Jvb3QgQ0ExHjAcBgNVBAsTFWh0dHA6Ly93d3cuY2FjZXJ0Lm9yZzEi\n"\
  64. "MCAGA1UEAxMZQ0EgQ2VydCBTaWduaW5nIEF1dGhvcml0eTEhMB8GCSqGSIb3DQEJ\n"\
  65. "ARYSc3VwcG9ydEBjYWNlcnQub3JnMIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIIC\n"\
  66. "CgKCAgEAziLA4kZ97DYoB1CW8qAzQIxL8TtmPzHlawI229Z89vGIj053NgVBlfkJ\n"\
  67. "8BLPRoZzYLdufujAWGSuzbCtRRcMY/pnCujW0r8+55jE8Ez64AO7NV1sId6eINm6\n"\
  68. "zWYyN3L69wj1x81YyY7nDl7qPv4coRQKFWyGhFtkZip6qUtTefWIonvuLwphK42y\n"\
  69. "fk1WpRPs6tqSnqxEQR5YYGUFZvjARL3LlPdCfgv3ZWiYUQXw8wWRBB0bF4LsyFe7\n"\
  70. "w2t6iPGwcswlWyC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vhC\n"\
  85. "AQ0ESRZHVG8gZ2V0IHlvdXIgb3duIGNlcnRpZmljYXRlIGZvciBGUkVFIGhlYWQg\n"\
  86. "b3ZlciB0byBodHRwOi8vd3d3LmNhY2VydC5vcmcwDQYJKoZIhvcNAQEEBQADggIB\n"\
  87. "ACjH7pyCArpcgBLKNQodgW+JapnM8mgPf6fhjViVPr3yBsOQWqy1YPaZQwGjiHCc\n"\
  88. "nWKdpIevZ1gNMDY75q1I08t0AoZxPuIrA2jxNGJARjtT6ij0rPtmlVOKTV39O9lg\n"\
  89. "18p5aTuxZZKmxoGCXJzN600BiqXfEVWqFcofN8CCmHBh22p8lqOOLlQ+TyGpkO/c\n"\
  90. "gr/c6EWtTZBzCDyUZbAEmXZ/4rzCahWqlwQ3JNgelE5tDlG+1sSPypZt90Pf6DBl\n"\
  91. "Jzt7u0NDY8RD97LsaMzhGY4i+5jhe1o+ATc7iwiwovOVThrLm82asduycPAtStvY\n"\
  92. "sONvRUgzEv/+PDIqVPfE94rwiCPCR/5kenHA0R6mY7AHfqQv0wGP3J8rtsYIqQ+T\n"\
  93. "SCX8Ev2fQtzzxD72V7DX3WnRBnc0CkvSyqD/HMaMyRa+xMwyN2hzXwj7UfdJUzYF\n"\
  94. "CpUCTPJ5GhD22Dp1nPMd8aINcGeGG7MW9S/lpOt5hvk9C8JzC6WZrG/8Z7jlLwum\n"\
  95. "GCSNe9FINSkYQKyTYOGWhlC0elnYjyELn8+CkcY7v2vcB5G5l1YjqrZslMZIBjzk\n"\
  96. "zk6q5PYvCdxTby78dOs6Y5nCpqyJvKeyRKANihDjbPIky/qbn3BHLt4Ui9SyIAmW\n"\
  97. "omTxJBzcoTWcFbLUvFUufQb1nA5V9FrWk9p2rSVzTMVD\n"\
  98. "-----END CERTIFICATE-----\n";
  99. /* replace the XXX with the actual RSA key */
  100. const char *mykey =
  101. "-----BEGIN RSA PRIVATE KEY-----\n"\
  102. "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX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XXXXXXXXXXXXXXXXXXXXXXXXX\n"\
  117. "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"\
  118. "-----END RSA PRIVATE KEY-----\n";
  119. #if defined(__GNUC__) || defined(__clang__)
  120. #pragma GCC diagnostic pop
  121. #endif
  122. (void)curl; /* avoid warnings */
  123. (void)parm; /* avoid warnings */
  124. /* get a BIO */
  125. bio = BIO_new_mem_buf((char *)mypem, -1);
  126. if(!bio) {
  127. printf("BIO_new_mem_buf failed\n");
  128. }
  129. /* use it to read the PEM formatted certificate from memory into an X509
  130. * structure that SSL can use
  131. */
  132. cert = PEM_read_bio_X509(bio, NULL, 0, NULL);
  133. if(!cert) {
  134. printf("PEM_read_bio_X509 failed...\n");
  135. }
  136. /* tell SSL to use the X509 certificate */
  137. ret = SSL_CTX_use_certificate((SSL_CTX*)sslctx, cert);
  138. if(ret != 1) {
  139. printf("Use certificate failed\n");
  140. }
  141. /* create a bio for the RSA key */
  142. kbio = BIO_new_mem_buf((char *)mykey, -1);
  143. if(!kbio) {
  144. printf("BIO_new_mem_buf failed\n");
  145. }
  146. /* read the key bio into an RSA object */
  147. rsa = PEM_read_bio_RSAPrivateKey(kbio, NULL, 0, NULL);
  148. if(!rsa) {
  149. printf("Failed to create key bio\n");
  150. }
  151. /* tell SSL to use the RSA key from memory */
  152. ret = SSL_CTX_use_RSAPrivateKey((SSL_CTX*)sslctx, rsa);
  153. if(ret != 1) {
  154. printf("Use Key failed\n");
  155. }
  156. /* free resources that have been allocated by OpenSSL functions */
  157. if(bio)
  158. BIO_free(bio);
  159. if(kbio)
  160. BIO_free(kbio);
  161. if(rsa)
  162. RSA_free(rsa);
  163. if(cert)
  164. X509_free(cert);
  165. /* all set to go */
  166. return CURLE_OK;
  167. }
  168. int main(void)
  169. {
  170. CURL *ch;
  171. CURLcode rv;
  172. curl_global_init(CURL_GLOBAL_ALL);
  173. ch = curl_easy_init();
  174. curl_easy_setopt(ch, CURLOPT_VERBOSE, 0L);
  175. curl_easy_setopt(ch, CURLOPT_HEADER, 0L);
  176. curl_easy_setopt(ch, CURLOPT_NOPROGRESS, 1L);
  177. curl_easy_setopt(ch, CURLOPT_NOSIGNAL, 1L);
  178. curl_easy_setopt(ch, CURLOPT_WRITEFUNCTION, writefunction);
  179. curl_easy_setopt(ch, CURLOPT_WRITEDATA, stdout);
  180. curl_easy_setopt(ch, CURLOPT_HEADERFUNCTION, writefunction);
  181. curl_easy_setopt(ch, CURLOPT_HEADERDATA, stderr);
  182. curl_easy_setopt(ch, CURLOPT_SSLCERTTYPE, "PEM");
  183. /* both VERIFYPEER and VERIFYHOST are set to 0 in this case because there is
  184. no CA certificate */
  185. curl_easy_setopt(ch, CURLOPT_SSL_VERIFYPEER, 0L);
  186. curl_easy_setopt(ch, CURLOPT_SSL_VERIFYHOST, 0L);
  187. curl_easy_setopt(ch, CURLOPT_URL, "https://www.example.com/");
  188. curl_easy_setopt(ch, CURLOPT_SSLKEYTYPE, "PEM");
  189. /* first try: retrieve page without user certificate and key -> fails */
  190. rv = curl_easy_perform(ch);
  191. if(rv == CURLE_OK) {
  192. printf("*** transfer succeeded ***\n");
  193. }
  194. else {
  195. printf("*** transfer failed ***\n");
  196. }
  197. /* second try: retrieve page using user certificate and key -> succeeds
  198. * load the certificate and key by installing a function doing the necessary
  199. * "modifications" to the SSL CONTEXT just before link init
  200. */
  201. curl_easy_setopt(ch, CURLOPT_SSL_CTX_FUNCTION, sslctx_function);
  202. rv = curl_easy_perform(ch);
  203. if(rv == CURLE_OK) {
  204. printf("*** transfer succeeded ***\n");
  205. }
  206. else {
  207. printf("*** transfer failed ***\n");
  208. }
  209. curl_easy_cleanup(ch);
  210. curl_global_cleanup();
  211. return (int)rv;
  212. }