usercertinmem.c
Go to the documentation of this file.
00001 /***************************************************************************
00002  *                                  _   _ ____  _
00003  *  Project                     ___| | | |  _ \| |
00004  *                             / __| | | | |_) | |
00005  *                            | (__| |_| |  _ <| |___
00006  *                             \___|\___/|_| \_\_____|
00007  *
00008  * Copyright (C) 2013 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al.
00009  *
00010  * This software is licensed as described in the file COPYING, which
00011  * you should have received as part of this distribution. The terms
00012  * are also available at https://curl.haxx.se/docs/copyright.html.
00013  *
00014  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
00015  * copies of the Software, and permit persons to whom the Software is
00016  * furnished to do so, under the terms of the COPYING file.
00017  *
00018  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
00019  * KIND, either express or implied.
00020  *
00021  ***************************************************************************/
00022 /* <DESC>
00023 /* Example using an in memory PEM user certificate and RSA key to retrieve an
00024  * https page.
00025  * </DESC>
00026  */
00027 /* Written by Ishan SinghLevett, based on Theo Borm's cacertinmem.c.
00028  * Note that to maintain simplicity this example does not use a CA certificate
00029  * for peer verification.  However, some form of peer verification
00030  * must be used in real circumstances when a secure connection is required.
00031  */
00032 
00033 #include <openssl/ssl.h>
00034 #include <openssl/x509.h>
00035 #include <openssl/pem.h>
00036 #include <curl/curl.h>
00037 #include <stdio.h>
00038 
00039 static size_t writefunction(void *ptr, size_t size, size_t nmemb, void *stream)
00040 {
00041   fwrite(ptr, size, nmemb, stream);
00042   return (nmemb*size);
00043 }
00044 
00045 static CURLcode sslctx_function(CURL *curl, void *sslctx, void *parm)
00046 {
00047   X509 *cert = NULL;
00048   BIO *bio = NULL;
00049   BIO *kbio = NULL;
00050   RSA *rsa = NULL;
00051   int ret;
00052 
00053   const char *mypem = /* www.cacert.org */
00054     "-----BEGIN CERTIFICATE-----\n"\
00055     "MIIHPTCCBSWgAwIBAgIBADANBgkqhkiG9w0BAQQFADB5MRAwDgYDVQQKEwdSb290\n"\
00056     "IENBMR4wHAYDVQQLExVodHRwOi8vd3d3LmNhY2VydC5vcmcxIjAgBgNVBAMTGUNB\n"\
00057     "IENlcnQgU2lnbmluZyBBdXRob3JpdHkxITAfBgkqhkiG9w0BCQEWEnN1cHBvcnRA\n"\
00058     "Y2FjZXJ0Lm9yZzAeFw0wMzAzMzAxMjI5NDlaFw0zMzAzMjkxMjI5NDlaMHkxEDAO\n"\
00059     "BgNVBAoTB1Jvb3QgQ0ExHjAcBgNVBAsTFWh0dHA6Ly93d3cuY2FjZXJ0Lm9yZzEi\n"\
00060     "MCAGA1UEAxMZQ0EgQ2VydCBTaWduaW5nIEF1dGhvcml0eTEhMB8GCSqGSIb3DQEJ\n"\
00061     "ARYSc3VwcG9ydEBjYWNlcnQub3JnMIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIIC\n"\
00062     "CgKCAgEAziLA4kZ97DYoB1CW8qAzQIxL8TtmPzHlawI229Z89vGIj053NgVBlfkJ\n"\
00063     "8BLPRoZzYLdufujAWGSuzbCtRRcMY/pnCujW0r8+55jE8Ez64AO7NV1sId6eINm6\n"\
00064     "zWYyN3L69wj1x81YyY7nDl7qPv4coRQKFWyGhFtkZip6qUtTefWIonvuLwphK42y\n"\
00065     "fk1WpRPs6tqSnqxEQR5YYGUFZvjARL3LlPdCfgv3ZWiYUQXw8wWRBB0bF4LsyFe7\n"\
00066     "w2t6iPGwcswlWyCR7BYCEo8y6RcYSNDHBS4CMEK4JZwFaz+qOqfrU0j36NK2B5jc\n"\
00067     "G8Y0f3/JHIJ6BVgrCFvzOKKrF11myZjXnhCLotLddJr3cQxyYN/Nb5gznZY0dj4k\n"\
00068     "epKwDpUeb+agRThHqtdB7Uq3EvbXG4OKDy7YCbZZ16oE/9KTfWgu3YtLq1i6L43q\n"\
00069     "laegw1SJpfvbi1EinbLDvhG+LJGGi5Z4rSDTii8aP8bQUWWHIbEZAWV/RRyH9XzQ\n"\
00070     "QUxPKZgh/TMfdQwEUfoZd9vUFBzugcMd9Zi3aQaRIt0AUMyBMawSB3s42mhb5ivU\n"\
00071     "fslfrejrckzzAeVLIL+aplfKkQABi6F1ITe1Yw1nPkZPcCBnzsXWWdsC4PDSy826\n"\
00072     "YreQQejdIOQpvGQpQsgi3Hia/0PsmBsJUUtaWsJx8cTLc6nloQsCAwEAAaOCAc4w\n"\
00073     "ggHKMB0GA1UdDgQWBBQWtTIb1Mfz4OaO873SsDrusjkY0TCBowYDVR0jBIGbMIGY\n"\
00074     "gBQWtTIb1Mfz4OaO873SsDrusjkY0aF9pHsweTEQMA4GA1UEChMHUm9vdCBDQTEe\n"\
00075     "MBwGA1UECxMVaHR0cDovL3d3dy5jYWNlcnQub3JnMSIwIAYDVQQDExlDQSBDZXJ0\n"\
00076     "IFNpZ25pbmcgQXV0aG9yaXR5MSEwHwYJKoZIhvcNAQkBFhJzdXBwb3J0QGNhY2Vy\n"\
00077     "dC5vcmeCAQAwDwYDVR0TAQH/BAUwAwEB/zAyBgNVHR8EKzApMCegJaAjhiFodHRw\n"\
00078     "czovL3d3dy5jYWNlcnQub3JnL3Jldm9rZS5jcmwwMAYJYIZIAYb4QgEEBCMWIWh0\n"\
00079     "dHBzOi8vd3d3LmNhY2VydC5vcmcvcmV2b2tlLmNybDA0BglghkgBhvhCAQgEJxYl\n"\
00080     "aHR0cDovL3d3dy5jYWNlcnQub3JnL2luZGV4LnBocD9pZD0xMDBWBglghkgBhvhC\n"\
00081     "AQ0ESRZHVG8gZ2V0IHlvdXIgb3duIGNlcnRpZmljYXRlIGZvciBGUkVFIGhlYWQg\n"\
00082     "b3ZlciB0byBodHRwOi8vd3d3LmNhY2VydC5vcmcwDQYJKoZIhvcNAQEEBQADggIB\n"\
00083     "ACjH7pyCArpcgBLKNQodgW+JapnM8mgPf6fhjViVPr3yBsOQWqy1YPaZQwGjiHCc\n"\
00084     "nWKdpIevZ1gNMDY75q1I08t0AoZxPuIrA2jxNGJARjtT6ij0rPtmlVOKTV39O9lg\n"\
00085     "18p5aTuxZZKmxoGCXJzN600BiqXfEVWqFcofN8CCmHBh22p8lqOOLlQ+TyGpkO/c\n"\
00086     "gr/c6EWtTZBzCDyUZbAEmXZ/4rzCahWqlwQ3JNgelE5tDlG+1sSPypZt90Pf6DBl\n"\
00087     "Jzt7u0NDY8RD97LsaMzhGY4i+5jhe1o+ATc7iwiwovOVThrLm82asduycPAtStvY\n"\
00088     "sONvRUgzEv/+PDIqVPfE94rwiCPCR/5kenHA0R6mY7AHfqQv0wGP3J8rtsYIqQ+T\n"\
00089     "SCX8Ev2fQtzzxD72V7DX3WnRBnc0CkvSyqD/HMaMyRa+xMwyN2hzXwj7UfdJUzYF\n"\
00090     "CpUCTPJ5GhD22Dp1nPMd8aINcGeGG7MW9S/lpOt5hvk9C8JzC6WZrG/8Z7jlLwum\n"\
00091     "GCSNe9FINSkYQKyTYOGWhlC0elnYjyELn8+CkcY7v2vcB5G5l1YjqrZslMZIBjzk\n"\
00092     "zk6q5PYvCdxTby78dOs6Y5nCpqyJvKeyRKANihDjbPIky/qbn3BHLt4Ui9SyIAmW\n"\
00093     "omTxJBzcoTWcFbLUvFUufQb1nA5V9FrWk9p2rSVzTMVD\n"\
00094     "-----END CERTIFICATE-----\n";
00095 
00096 /*replace the XXX with the actual RSA key*/
00097   const char *mykey =
00098     "-----BEGIN RSA PRIVATE KEY-----\n"\
00099     "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"\
00100     "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"\
00101     "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"\
00102     "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"\
00103     "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"\
00104     "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"\
00105     "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"\
00106     "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"\
00107     "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"\
00108     "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"\
00109     "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"\
00110     "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"\
00111     "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"\
00112     "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"\
00113     "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"\
00114     "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"\
00115     "-----END RSA PRIVATE KEY-----\n";
00116 
00117   (void)curl; /* avoid warnings */
00118   (void)parm; /* avoid warnings */
00119 
00120   /* get a BIO */
00121   bio = BIO_new_mem_buf((char *)mypem, -1);
00122 
00123   if(bio == NULL) {
00124     printf("BIO_new_mem_buf failed\n");
00125   }
00126 
00127   /* use it to read the PEM formatted certificate from memory into an X509
00128    * structure that SSL can use
00129    */
00130   cert = PEM_read_bio_X509(bio, NULL, 0, NULL);
00131   if(cert == NULL) {
00132     printf("PEM_read_bio_X509 failed...\n");
00133   }
00134 
00135   /*tell SSL to use the X509 certificate*/
00136   ret = SSL_CTX_use_certificate((SSL_CTX*)sslctx, cert);
00137   if(ret != 1) {
00138     printf("Use certificate failed\n");
00139   }
00140 
00141   /*create a bio for the RSA key*/
00142   kbio = BIO_new_mem_buf((char *)mykey, -1);
00143   if(kbio == NULL) {
00144     printf("BIO_new_mem_buf failed\n");
00145   }
00146 
00147   /*read the key bio into an RSA object*/
00148   rsa = PEM_read_bio_RSAPrivateKey(kbio, NULL, 0, NULL);
00149   if(rsa == NULL) {
00150     printf("Failed to create key bio\n");
00151   }
00152 
00153   /*tell SSL to use the RSA key from memory*/
00154   ret = SSL_CTX_use_RSAPrivateKey((SSL_CTX*)sslctx, rsa);
00155   if(ret != 1) {
00156     printf("Use Key failed\n");
00157   }
00158 
00159   /* free resources that have been allocated by openssl functions */
00160   if(bio)
00161     BIO_free(bio);
00162 
00163   if(kbio)
00164     BIO_free(kbio);
00165 
00166   if(rsa)
00167     RSA_free(rsa);
00168 
00169   if(cert)
00170     X509_free(cert);
00171 
00172   /* all set to go */
00173   return CURLE_OK;
00174 }
00175 
00176 int main(void)
00177 {
00178   CURL *ch;
00179   CURLcode rv;
00180 
00181   rv = curl_global_init(CURL_GLOBAL_ALL);
00182   ch = curl_easy_init();
00183   rv = curl_easy_setopt(ch, CURLOPT_VERBOSE, 0L);
00184   rv = curl_easy_setopt(ch, CURLOPT_HEADER, 0L);
00185   rv = curl_easy_setopt(ch, CURLOPT_NOPROGRESS, 1L);
00186   rv = curl_easy_setopt(ch, CURLOPT_NOSIGNAL, 1L);
00187   rv = curl_easy_setopt(ch, CURLOPT_WRITEFUNCTION, writefunction);
00188   rv = curl_easy_setopt(ch, CURLOPT_WRITEDATA, stdout);
00189   rv = curl_easy_setopt(ch, CURLOPT_HEADERFUNCTION, writefunction);
00190   rv = curl_easy_setopt(ch, CURLOPT_HEADERDATA, stderr);
00191   rv = curl_easy_setopt(ch, CURLOPT_SSLCERTTYPE, "PEM");
00192 
00193   /* both VERIFYPEER and VERIFYHOST are set to 0 in this case because there is
00194      no CA certificate*/
00195 
00196   rv = curl_easy_setopt(ch, CURLOPT_SSL_VERIFYPEER, 0L);
00197   rv = curl_easy_setopt(ch, CURLOPT_SSL_VERIFYHOST, 0L);
00198   rv = curl_easy_setopt(ch, CURLOPT_URL, "https://www.example.com/");
00199   rv = curl_easy_setopt(ch, CURLOPT_SSLKEYTYPE, "PEM");
00200 
00201   /* first try: retrieve page without user certificate and key -> will fail
00202    */
00203   rv = curl_easy_perform(ch);
00204   if(rv==CURLE_OK) {
00205     printf("*** transfer succeeded ***\n");
00206   }
00207   else {
00208     printf("*** transfer failed ***\n");
00209   }
00210 
00211   /* second try: retrieve page using user certificate and key -> will succeed
00212    * load the certificate and key by installing a function doing the necessary
00213    * "modifications" to the SSL CONTEXT just before link init
00214    */
00215   rv = curl_easy_setopt(ch, CURLOPT_SSL_CTX_FUNCTION, *sslctx_function);
00216   rv = curl_easy_perform(ch);
00217   if(rv==CURLE_OK) {
00218     printf("*** transfer succeeded ***\n");
00219   }
00220   else {
00221     printf("*** transfer failed ***\n");
00222   }
00223 
00224   curl_easy_cleanup(ch);
00225   curl_global_cleanup();
00226   return rv;
00227 }


rc_visard_driver
Author(s): Heiko Hirschmueller , Christian Emmerich , Felix Ruess
autogenerated on Thu Jun 6 2019 20:43:07