usercertinmem.c 8.9 KB

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