sessioninfo.c
Go to the documentation of this file.
00001 /***************************************************************************
00002  *                                  _   _ ____  _
00003  *  Project                     ___| | | |  _ \| |
00004  *                             / __| | | | |_) | |
00005  *                            | (__| |_| |  _ <| |___
00006  *                             \___|\___/|_| \_\_____|
00007  *
00008  * Copyright (C) 1998 - 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  * Uses the CURLINFO_TLS_SESSION data.
00024  * </DESC>
00025  */
00026 
00027 /* Note that this example currently requires curl to be linked against
00028    GnuTLS (and this program must also be linked against -lgnutls). */
00029 
00030 #include <stdio.h>
00031 
00032 #include <curl/curl.h>
00033 #include <gnutls/gnutls.h>
00034 
00035 static CURL *curl;
00036 
00037 static size_t wrfu(void *ptr, size_t size, size_t nmemb, void *stream)
00038 {
00039   const struct curl_tlssessioninfo *info;
00040   unsigned int cert_list_size;
00041   const gnutls_datum_t *chainp;
00042   CURLcode res;
00043 
00044   (void)stream;
00045   (void)ptr;
00046 
00047   res = curl_easy_getinfo(curl, CURLINFO_TLS_SESSION, &info);
00048 
00049   if(!res) {
00050     switch(info->backend) {
00051     case CURLSSLBACKEND_GNUTLS:
00052       /* info->internals is now the gnutls_session_t */
00053       chainp = gnutls_certificate_get_peers(info->internals, &cert_list_size);
00054       if((chainp) && (cert_list_size)) {
00055         unsigned int i;
00056 
00057         for(i = 0; i < cert_list_size; i++) {
00058           gnutls_x509_crt_t cert;
00059           gnutls_datum_t dn;
00060 
00061           if(GNUTLS_E_SUCCESS == gnutls_x509_crt_init(&cert)) {
00062             if(GNUTLS_E_SUCCESS ==
00063                gnutls_x509_crt_import(cert, &chainp[i], GNUTLS_X509_FMT_DER)) {
00064               if(GNUTLS_E_SUCCESS ==
00065                  gnutls_x509_crt_print(cert, GNUTLS_CRT_PRINT_FULL, &dn)) {
00066                 fprintf(stderr, "Certificate #%d: %.*s", i, dn.size, dn.data);
00067 
00068                 gnutls_free(dn.data);
00069               }
00070             }
00071 
00072             gnutls_x509_crt_deinit(cert);
00073           }
00074         }
00075       }
00076       break;
00077     case CURLSSLBACKEND_NONE:
00078     default:
00079       break;
00080     }
00081   }
00082 
00083   return size * nmemb;
00084 }
00085 
00086 int main(void)
00087 {
00088   curl_global_init(CURL_GLOBAL_DEFAULT);
00089 
00090   curl = curl_easy_init();
00091   if(curl) {
00092     curl_easy_setopt(curl, CURLOPT_URL, "https://www.example.com/");
00093 
00094     curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, wrfu);
00095 
00096     curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
00097     curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
00098 
00099     curl_easy_setopt(curl, CURLOPT_VERBOSE, 0L);
00100 
00101     (void) curl_easy_perform(curl);
00102 
00103     curl_easy_cleanup(curl);
00104   }
00105 
00106   curl_global_cleanup();
00107 
00108   return 0;
00109 }


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