ESP32 SSL connection works when CA Certificate is a constant, but not when read from a file

mdszy

I have the following Arduino code I'm using with an ESP32:

  if(!SPIFFS.begin(true)) {
    Serial.println("Error mounting SPIFFS.");
  }

  File file = SPIFFS.open("/root.cer");

  if(!file) {
    Serial.println("Error opening the file.");
  }


  Serial.println("CA Root certificate: ");

  String ca_cert = file.readString();

  Serial.println(ca_cert);
  espClient.setCACert(ca_cert.c_str());

  file.close();

This is the relevant code for loading a file and setting the WiFiClientSecure's CA certificate. This code does not work.

However, if I replace espClient.setCACert(ca_cert.c_str()); with espClient.setCACert(ROOTCERT); where ROOTCERT is defined as such:

#define ROOTCERT "-----BEGIN CERTIFICATE-----\n" \
"MIIDSjCCAjKgAwIBAgIQRK+wgNajJ7qJMDmGLvhAazANBgkqhkiG9w0BAQUFADA/\n" \
"MSQwIgYDVQQKExtEaWdpdGFsIFNpZ25hdHVyZSBUcnVzdCBDby4xFzAVBgNVBAMT\n" \
"DkRTVCBSb290IENBIFgzMB4XDTAwMDkzMDIxMTIxOVoXDTIxMDkzMDE0MDExNVow\n" \
"PzEkMCIGA1UEChMbRGlnaXRhbCBTaWduYXR1cmUgVHJ1c3QgQ28uMRcwFQYDVQQD\n" \
"Ew5EU1QgUm9vdCBDQSBYMzCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEB\n" \
"AN+v6ZdQCINXtMxiZfaQguzH0yxrMMpb7NnDfcdAwRgUi+DoM3ZJKuM/IUmTrE4O\n" \
"rz5Iy2Xu/NMhD2XSKtkyj4zl93ewEnu1lcCJo6m67XMuegwGMoOifooUMM0RoOEq\n" \
"OLl5CjH9UL2AZd+3UWODyOKIYepLYYHsUmu5ouJLGiifSKOeDNoJjj4XLh7dIN9b\n" \
"xiqKqy69cK3FCxolkHRyxXtqqzTWMIn/5WgTe1QLyNau7Fqckh49ZLOMxt+/yUFw\n" \
"7BZy1SbsOFU5Q9D8/RhcQPGX69Wam40dutolucbY38EVAjqr2m7xPi71XAicPNaD\n" \
"aeQQmxkqtilX4+U9m5/wAl0CAwEAAaNCMEAwDwYDVR0TAQH/BAUwAwEB/zAOBgNV\n" \
"HQ8BAf8EBAMCAQYwHQYDVR0OBBYEFMSnsaR7LHH62+FLkHX/xBVghYkQMA0GCSqG\n" \
"SIb3DQEBBQUAA4IBAQCjGiybFwBcqR7uKGY3Or+Dxz9LwwmglSBd49lZRNI+DT69\n" \
"ikugdB/OEIKcdBodfpga3csTS7MgROSR6cz8faXbauX+5v3gTt23ADq1cEmv8uXr\n" \
"AvHRAosZy5Q6XkjEGB5YGV8eAlrwDPGxrancWYaLbumR9YbK+rlmM6pZW87ipxZz\n" \
"R8srzJmwN0jP41ZL9c8PDHIyh8bwRLtTcm1D9SZImlJnt1ir/md2cXjbDaJWFBM5\n" \
"JDGFoqgCWjBH4d1QB7wCCZAA62RjYJsWvIjJEubSfZGL+T0yjWW06XyxV3bqxbYo\n" \
"Ob8VZRzI9neWagqNdwvYkQsEjgfbKbYK7p2CNTUQ\n" \
"-----END CERTIFICATE-----\n" 

The code works.

The ROOTCERT string is taken directly from the certificate file, so they must be identical.

The certificate file was downloaded and exported using Windows's certificate exporter. I've tried converting line endings to no avail.

EDIT: I've found a clue.

If I do the following:

String constString = ROOTCERT;
espClient.setCACert(constString.c_str());

It also does not work.

And I added this code:

 if(strcmp(constString.c_str(), ROOTCERT))
     Serial.println("Constant and converted string are equal.");
   else
     Serial.println("Constant and converted string are different.");

And it prints "Constant and converted string are different."

So it appears to be some kind of problem with how .c_str() does things? I have no idea what this could be, though. When printed to the console, the .c_str(), ROOTCERT and ca_cert Strings all appear IDENTICAL.

I am completely confused here.

Turns out I was using strcmp() incorrectly. Things are still not working.

mdszy

After messing around, I fixed it.

So .c_str() is just another way of pointing to the internal buffer of the String object.

Somehow that was messing things up. Using this code fixed it.

char *dest;

dest = (char *)malloc(sizeof(char) * (ca_cert.length()+1));
strcpy(dest, ca_cert.c_str());

espClient.setCACert(dest);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Flutter blue plus library, when trying to read a characteristics from esp32 driver getting PlatformException

From Dev

How to read information from SSL certificate file

From Dev

Keep getting Could not read CA certificate when trying to start docker

From Dev

Failed to read cluster ca certificate when joining worker node

From Dev

LINQ SQL Connection String Not Working When Read From TXT File

From Dev

When a server sends a copy of SSL certificate, the connection is encrypted?

From Dev

Docker: Could not read CA certificate: no such file or directory

From Dev

Esp32 read lines from text file as strings get wrong worder

From Dev

Android Smack SSL/TLS connection to XMPP Ejabberd server with CA Certificate

From Dev

"Invalid gcp.key" from Mongoose OS when connecting ESP32 to Google Cloud Platform

From Dev

How to noninteractively add Certificate Authority (CA) ssl certificates from script

From

When is it necessary to provide a CA file?

From Dev

Python's default SSL certificate context not working in requests method when behind proxy, works fine otherwise

From Dev

SSL: CERTIFICATE_VERIFY_FAILED when running on Python 3.8. Works on other versions of python

From Dev

Why displays the Esp32 "nan" when using logarithem?

From Dev

ESP32 : Error when activating Wi-Fi interface

From Dev

Weird behavior when using monitor and analogWrite (ESP32)

From Dev

ESP32 heap corruption error when releasing allocated memory

From Dev

Unexpected variable values reading from file (ESP32)

From Dev

How to transfer file from Django server to esp32?

From Dev

How to solve SSL certificate: self signed certificate when cloning repo from github?

From Dev

schema not works when no internet connection

From

Get the root CA certificate for the connection

From Java

Can't read certificate when running in Linux Docker container - works on Windows

From Dev

Update AWS RDS SSL/TLS Certificate from rds-ca-2015 to rds-ca-2019

From Dev

esp-idf build flash fails when esp32 when component #includes esp_types.h found in newlib/ not esp32/

From Dev

SSL Certificate add failed, Error: 183 Cannot create a file when that file already exists - How to bound certificate to ipport?

From

How to ignore the certificate check when ssl

From Dev

Browser not asking for client certificate when CA is set in Apache

Related Related

  1. 1

    Flutter blue plus library, when trying to read a characteristics from esp32 driver getting PlatformException

  2. 2

    How to read information from SSL certificate file

  3. 3

    Keep getting Could not read CA certificate when trying to start docker

  4. 4

    Failed to read cluster ca certificate when joining worker node

  5. 5

    LINQ SQL Connection String Not Working When Read From TXT File

  6. 6

    When a server sends a copy of SSL certificate, the connection is encrypted?

  7. 7

    Docker: Could not read CA certificate: no such file or directory

  8. 8

    Esp32 read lines from text file as strings get wrong worder

  9. 9

    Android Smack SSL/TLS connection to XMPP Ejabberd server with CA Certificate

  10. 10

    "Invalid gcp.key" from Mongoose OS when connecting ESP32 to Google Cloud Platform

  11. 11

    How to noninteractively add Certificate Authority (CA) ssl certificates from script

  12. 12

    When is it necessary to provide a CA file?

  13. 13

    Python's default SSL certificate context not working in requests method when behind proxy, works fine otherwise

  14. 14

    SSL: CERTIFICATE_VERIFY_FAILED when running on Python 3.8. Works on other versions of python

  15. 15

    Why displays the Esp32 "nan" when using logarithem?

  16. 16

    ESP32 : Error when activating Wi-Fi interface

  17. 17

    Weird behavior when using monitor and analogWrite (ESP32)

  18. 18

    ESP32 heap corruption error when releasing allocated memory

  19. 19

    Unexpected variable values reading from file (ESP32)

  20. 20

    How to transfer file from Django server to esp32?

  21. 21

    How to solve SSL certificate: self signed certificate when cloning repo from github?

  22. 22

    schema not works when no internet connection

  23. 23

    Get the root CA certificate for the connection

  24. 24

    Can't read certificate when running in Linux Docker container - works on Windows

  25. 25

    Update AWS RDS SSL/TLS Certificate from rds-ca-2015 to rds-ca-2019

  26. 26

    esp-idf build flash fails when esp32 when component #includes esp_types.h found in newlib/ not esp32/

  27. 27

    SSL Certificate add failed, Error: 183 Cannot create a file when that file already exists - How to bound certificate to ipport?

  28. 28

    How to ignore the certificate check when ssl

  29. 29

    Browser not asking for client certificate when CA is set in Apache

HotTag

Archive