Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <curl/curl.h>
- int main(int argc, char** argv){
- if(argc<4){
- printf("no args");
- return 0;
- }
- int i;
- CURL *curl;
- CURLcode res;
- FILE *headerfile;
- const char *pPassphrase = "test13";
- char *url = argv[1];
- char *pCertFile = argv[2]; //"testcert.pem";
- char *pCACertFile = argv[4]; //"cacert.pem";
- const char *pKeyName;
- const char *pKeyType;
- printf("%s : %s | %s\n", url, pCertFile, pCACertFile);
- const char *pEngine;
- #ifdef USE_ENGINE
- pKeyName = "rsa_test";
- pKeyType = "ENG";
- pEngine = "chil"; /* for nChiper HSM... */
- #else
- pKeyName = argv[3];
- pKeyType = "PEM";
- pEngine = NULL;
- #endif
- headerfile = fopen("dumpit", "w");
- curl_global_init(CURL_GLOBAL_DEFAULT);
- curl = curl_easy_init();
- if(curl) {
- /* what call to write: */
- curl_easy_setopt(curl, CURLOPT_URL, url);
- curl_easy_setopt(curl, CURLOPT_HEADERDATA, headerfile);
- for(i = 0; i < 1; i++) /* single-iteration loop, just to break out from */
- {
- if (pEngine) /* use crypto engine */
- {
- if (curl_easy_setopt(curl, CURLOPT_SSLENGINE,pEngine) != CURLE_OK)
- { /* load the crypto engine */
- fprintf(stderr,"can't set crypto engine\n");
- break;
- }
- if (curl_easy_setopt(curl, CURLOPT_SSLENGINE_DEFAULT,1L) != CURLE_OK)
- { /* set the crypto engine as default */
- /* only needed for the first time you load
- a engine in a curl object... */
- fprintf(stderr,"can't set crypto engine as default\n");
- break;
- }
- }
- /* cert is stored PEM coded in file... */
- /* since PEM is default, we needn't set it for PEM */
- curl_easy_setopt(curl,CURLOPT_SSLCERTTYPE,"PEM");
- /* set the cert for client authentication */
- curl_easy_setopt(curl,CURLOPT_SSLCERT,pCertFile);
- /* sorry, for engine we must set the passphrase
- (if the key has one...) */
- if (pPassphrase)
- curl_easy_setopt(curl,CURLOPT_KEYPASSWD,pPassphrase);
- /* if we use a key stored in a crypto engine,
- we must set the key type to "ENG" */
- curl_easy_setopt(curl,CURLOPT_SSLKEYTYPE,pKeyType);
- /* set the private key (file or ID in engine) */
- curl_easy_setopt(curl,CURLOPT_SSLKEY,pKeyName);
- /* set the file with the certs vaildating the server */
- curl_easy_setopt(curl,CURLOPT_CAINFO,pCACertFile);
- /* disconnect if we can't validate server's cert */
- curl_easy_setopt(curl,CURLOPT_SSL_VERIFYPEER, 1);
- /* Perform the request, res will get the return code */
- res = curl_easy_perform(curl);
- /* Check for errors */
- if(res != CURLE_OK)
- fprintf(stderr, "curl_easy_perform() failed: %s\n",
- curl_easy_strerror(res));
- /* we are done... */
- }
- /* always cleanup */
- curl_easy_cleanup(curl);
- }
- curl_global_cleanup();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement