usercertinmem.c 9.0 KB

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