Foro de elhacker.net

Comunicaciones => Redes => Mensaje iniciado por: Usuario887 en 13 Mayo 2021, 16:34 pm



Título: CURL en Windows. SSL
Publicado por: Usuario887 en 13 Mayo 2021, 16:34 pm
Tengo este codigo:

Código
  1. #include <stdio.h>
  2. #include "curl/include/curl/curl.h"
  3.  
  4. size_t dataSize=0;
  5. size_t curlWriteFunction(void* ptr, size_t size/*always==1*/,
  6.                         size_t nmemb, void* userdata)
  7. {
  8.    char** stringToWrite=(char**)userdata;
  9.    const char* input=(const char*)ptr;
  10.    if(nmemb==0) return 0;
  11.    if(!*stringToWrite)
  12.        *stringToWrite=malloc(nmemb+1);
  13.    else
  14.        *stringToWrite=realloc(*stringToWrite, dataSize+nmemb+1);
  15.    memcpy(*stringToWrite+dataSize, input, nmemb);
  16.    dataSize+=nmemb;
  17.    (*stringToWrite)[dataSize]='\0';
  18.    return nmemb;
  19. }
  20.  
  21. int main()
  22. {
  23.    char* data=0;
  24.    CURL*const curl=curl_easy_init();
  25.    if(!curl)
  26.    {
  27.        fprintf(stderr, "error: Initialization");
  28.        return 1;
  29.    }
  30.    curl_easy_setopt(curl, CURLOPT_URL, "http://www.google.com");
  31.    curl_easy_setopt(curl, CURLOPT_WRITEDATA, &data);
  32.    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &curlWriteFunction);
  33.    if(curl_easy_perform(curl)!=CURLE_OK)
  34.    {
  35.        fprintf(stderr, "error: get web page\n");
  36.        return 1;
  37.    }
  38.    curl_easy_cleanup(curl);
  39.  
  40.    if(!data)
  41.    {
  42.        fprintf(stderr, "error: Got no data\n");
  43.        return 1;
  44.    }
  45.  
  46.    printf("Page data:\n\n%s\n", data);
  47.    free(data);
  48. }
  49.  

En Linux, al especificar https: en el URL, no hace ninguna diferencia, sin embargo cuando lo hago en Windows falla la funcion curl_easy_perform. ¿Alguien tiene idea de por que?

Gracias y saludos,
marax.


Título: Re: CURL en Windows. SSL
Publicado por: engel lex en 14 Mayo 2021, 20:52 pm
que error te da? puede ser que te haga falta openssl o algo similar


Título: Re: CURL en Windows. SSL
Publicado por: Usuario887 en 15 Mayo 2021, 23:04 pm
Hola, gracias por responder.

Citar
C:\Users\Programming\Desktop\CURL test>main https://www.google.com
Failed to get web page: SSL peer certificate or SSH remote key was not OK

C:\Users\Programming\Desktop\CURL test>


Título: Re: CURL en Windows. SSL
Publicado por: engel lex en 16 Mayo 2021, 02:34 am
pareciera un error con el certificado ssl el asunto es que no se si remoto o local, estás tras proxy, vpn o similar?

intenta con la opcion CURLOPT_VERBOSE activada a ver si te da más info que sea util para determinar el problema


Título: Re: CURL en Windows. SSL
Publicado por: Usuario887 en 16 Mayo 2021, 21:43 pm
Citar
C:\Users\Programming\Desktop\CURL test>main https://www.google.com
*   Trying 142.250.185.4:443...
* Connected to www.google.com (142.250.185.4) port 443 (#0)
* ALPN, offering h2
* ALPN, offering http/1.1
* SSL certificate problem: unable to get local issuer certificate
* Closing connection 0
Failed to get web page: SSL peer certificate or SSH remote key was not OK

C:\Users\Programming\Desktop\CURL test>

¿Que es "Certificado del emisor"?


Título: Re: CURL en Windows. SSL
Publicado por: el-brujo en 19 Mayo 2021, 12:07 pm

Citar
Download the latest cacert.pem from https://curl.haxx.se/ca/cacert.pem

Add the following line to php.ini: (if this is shared hosting and you don't have access to php.ini then you could add this to .user.ini in public_html).

curl.cainfo="/path/to/downloaded/cacert.pem"

Make sure you enclose the path within double quotation marks!!!


Citar
I found out you need to download the .pem file here https://curl.haxx.se/docs/caextract.html

And add these settings...

curl_easy_setopt(curl, CURLOPT_SSL_VERIFYSTATUS, 1);           
curl_easy_setopt(curl, CURLOPT_CAINFO, "PATH TO FILE\\cacert.pem");
curl_easy_setopt(curl, CURLOPT_CAPATH, "PATH TO FILE\\cacert.pem");



Título: Re: CURL en Windows. SSL
Publicado por: engel lex en 19 Mayo 2021, 16:02 pm
por aclarar un poco

¿Que es "Certificado del emisor"?

el SSL no es unidireccional, es decir, no es que la pagina te pasa su certificado y listo, sino que es bidireccional, ella te pasa un certificado y tu le pasas uno, ambos son requeridos para el cifrado asimetrico....

El certificado del emisor, es tu certificado, el curl en tu windows aparentemente no lo consigue y por lo tanto no puede terminar el saludo SSL (handshake)

lo que propone el-brujo, es la solución oficial, que te descargues el certificado oficial de curl (cacert.pem) y que lo incluyas en el código (CURLOPT_CAINFO y CURLOPT_CAPATH)


Título: Re: CURL en Windows. SSL
Publicado por: Usuario887 en 21 Mayo 2021, 19:29 pm
Hola,

gracias por su atencion, pero el problema inicial persiste:

Citar
C:\Users\Programming\Desktop\CURL test>main https://www.google.com/
*   Trying 142.250.185.4:443...
* Connected to www.google.com (142.250.185.4) port 443 (#0)
* ALPN, offering h2
* ALPN, offering http/1.1
* successfully set certificate verify locations:
*  CAfile: cacert.pem
*  CApath: cacert.pem
* SSL connection using TLSv1.3 / TLS_AES_256_GCM_SHA384
* ALPN, server accepted to use h2
* Server certificate:
*  subject: C=US; ST=California; L=Mountain View; O=Google LLC; CN=www.google.com
*  start date: May  3 11:24:19 2021 GMT
*  expire date: Jul 26 11:24:18 2021 GMT
*  subjectAltName: host "www.google.com" matched cert's "www.google.com"
*  issuer: C=US; O=Google Trust Services; CN=GTS CA 1O1
*  SSL certificate verify ok.
* No OCSP response received
* Closing connection 0
Failed to get web page: SSL server certificate status verification FAILED

C:\Users\Programming\Desktop\CURL test>


Título: Re: CURL en Windows. SSL
Publicado por: MinusFour en 21 Mayo 2021, 20:14 pm
Hola,

gracias por su atencion, pero el problema inicial persiste:


No uses:

Código:
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYSTATUS, 1);

Los servidores de google (o al menos al que estás intentando conectarte) no soportan esa extensión.

Edit:
¿O quizás es algo que te haga falta a ti?


Título: Re: CURL en Windows. SSL
Publicado por: Usuario887 en 22 Mayo 2021, 17:35 pm
Tienes razon, MinusFour. Ese era el problema.

Ahora funciona.

Gracias por su atencion.