-->
Page 1 of 1

Question on verifyCertChain

PostPosted: Sun Mar 26, 2017 11:51 am
by tomte76
Hi,

as far as I understand the fingerprint of an LetsEncrypt certifiacate changes on every renew. So it is a kind of a problem to verify the fingerprint in a sketch as I would need to update the fingerprint every 3 month. So I looked at the code an found verifyCertChain. I tried the to load the intermediate certificate from the output of e.g.

Code: Select allopenssl s_client -showcerts -connect www.howsmyssl.com:443 </dev/null

using

Code: Select allconst char*  server = "www.howsmyssl.com";

unsigned char letsencrypt_root_ca[] = "-----BEGIN CERTIFICATE-----\n" \
"MIIEkjCCA3qgAwIBAgIQCgFBQgAAAVOFc2oLheynCDANBgkqhkiG9w0BAQsFADA/\n" \
"MSQwIgYDVQQKExtEaWdpdGFsIFNpZ25hdHVyZSBUcnVzdCBDby4xFzAVBgNVBAMT\n" \
"DkRTVCBSb290IENBIFgzMB4XDTE2MDMxNzE2NDA0NloXDTIxMDMxNzE2NDA0Nlow\n" \
"SjELMAkGA1UEBhMCVVMxFjAUBgNVBAoTDUxldCdzIEVuY3J5cHQxIzAhBgNVBAMT\n" \
"GkxldCdzIEVuY3J5cHQgQXV0aG9yaXR5IFgzMIIBIjANBgkqhkiG9w0BAQEFAAOC\n" \
"AQ8AMIIBCgKCAQEAnNMM8FrlLke3cl03g7NoYzDq1zUmGSXhvb418XCSL7e4S0EF\n" \
"q6meNQhY7LEqxGiHC6PjdeTm86dicbp5gWAf15Gan/PQeGdxyGkOlZHP/uaZ6WA8\n" \
"SMx+yk13EiSdRxta67nsHjcAHJyse6cF6s5K671B5TaYucv9bTyWaN8jKkKQDIZ0\n" \
"Z8h/pZq4UmEUEz9l6YKHy9v6Dlb2honzhT+Xhq+w3Brvaw2VFn3EK6BlspkENnWA\n" \
"a6xK8xuQSXgvopZPKiAlKQTGdMDQMc2PMTiVFrqoM7hD8bEfwzB/onkxEz0tNvjj\n" \
"/PIzark5McWvxI0NHWQWM6r6hCm21AvA2H3DkwIDAQABo4IBfTCCAXkwEgYDVR0T\n" \
"AQH/BAgwBgEB/wIBADAOBgNVHQ8BAf8EBAMCAYYwfwYIKwYBBQUHAQEEczBxMDIG\n" \
"CCsGAQUFBzABhiZodHRwOi8vaXNyZy50cnVzdGlkLm9jc3AuaWRlbnRydXN0LmNv\n" \
"bTA7BggrBgEFBQcwAoYvaHR0cDovL2FwcHMuaWRlbnRydXN0LmNvbS9yb290cy9k\n" \
"c3Ryb290Y2F4My5wN2MwHwYDVR0jBBgwFoAUxKexpHsscfrb4UuQdf/EFWCFiRAw\n" \
"VAYDVR0gBE0wSzAIBgZngQwBAgEwPwYLKwYBBAGC3xMBAQEwMDAuBggrBgEFBQcC\n" \
"ARYiaHR0cDovL2Nwcy5yb290LXgxLmxldHNlbmNyeXB0Lm9yZzA8BgNVHR8ENTAz\n" \
"MDGgL6AthitodHRwOi8vY3JsLmlkZW50cnVzdC5jb20vRFNUUk9PVENBWDNDUkwu\n" \
"Y3JsMB0GA1UdDgQWBBSoSmpjBH3duubRObemRWXv86jsoTANBgkqhkiG9w0BAQsF\n" \
"AAOCAQEA3TPXEfNjWDjdGBX7CVW+dla5cEilaUcne8IkCJLxWh9KEik3JHRRHGJo\n" \
"uM2VcGfl96S8TihRzZvoroed6ti6WqEBmtzw3Wodatg+VyOeph4EYpr/1wXKtx8/\n" \
"wApIvJSwtmVi4MFU5aMqrSDE6ea73Mj2tcMyo5jMd6jmeWUHK8so/joWUoHOUgwu\n" \
"X4Po1QYz+3dszkDqMp4fklxBwXRsW10KXzPMTZ+sOPAveyxindmjkW8lGy+QsRlG\n" \
"PfZ+G6Z6h7mjem0Y+iWlkYcV4PIWL1iwBi8saCbGS5jN2p8M+X+Q7UNKEkROb3N6\n" \
"KOqkqm57TH2H3eDJAkSnh6/DNFu0Qg==\n"
"-----END CERTIFICATE-----\n";

const unsigned char* server_root_cert = letsencrypt_root_ca;

later in the sketch I do

Code: Select all  WiFiClientSecure client;
  Serial.print("connecting to ");
  Serial.println(host);

  client.setCACert(server_root_cert,sizeof(server_root_cert));
   
  if (!client.connect(host, httpsPort)) {
    Serial.println("connection failed");
    return;
  }
 
  if (client.verifyCertChain(server)) {
    Serial.println("certificate matches");
  } else {
    Serial.println("certificate doesn't match");
  }


But the check always fails. Did I get something wrong and a verification of this kind is not possible? I also search the documentation. But this feature does not seem to be documented there.

Any hints on how to realize a check like this? Or other aproaches not to use the fingerprint? Or a pointer to documenation I was not able to find?

Thank you.

Re: Question on verifyCertChain

PostPosted: Sun Mar 26, 2017 2:43 pm
by tomte76
I found some answers to my questions. Maybe it will be helpful to others.

1.) LetsEncrypt is a tricky example as the certificate is cross-signed be another CA. axTLS seems to verfiy against the highest depth-level in the chain an therefore the recommended intermediate from letsencrypt cannot be used for matching. You have to take the certificate from https://www.identrust.com/certificates/trustid/root-download-x3.html.

2.) The idea for the way I included the certificate was born by the way it is done for Superhouse Open-RTOS and also I found a similar example in arduino-esp32. Both are using mbedtls. This seems to be wrong for the actual ESP8266 Arduino, which is using axTLS. Meanwhile I found the following solution to prepare the above mentioned certificate:

Code: Select allopenssl enc -d -a -in dst-root-ca-x3.crt > dst-root-ca-x3.bin.crt
xxd -i dst-root-ca-x3.bin.crt

This results in the following code which can be used in the sketch

Code: Select allunsigned char dst_root_ca_x3_bin_crt[] = {
  0x30, 0x82, 0x03, 0x4a, 0x30, 0x82, 0x02, 0x32, 0xa0, 0x03, 0x02, 0x01,
  0x02, 0x02, 0x10, 0x44, 0xaf, 0xb0, 0x80, 0xd6, 0xa3, 0x27, 0xba, 0x89,
  0x30, 0x39, 0x86, 0x2e, 0xf8, 0x40, 0x6b, 0x30, 0x0d, 0x06, 0x09, 0x2a,
  0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x05, 0x05, 0x00, 0x30, 0x3f,
  0x31, 0x24, 0x30, 0x22, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x13, 0x1b, 0x44,
  0x69, 0x67, 0x69, 0x74, 0x61, 0x6c, 0x20, 0x53, 0x69, 0x67, 0x6e, 0x61,
  0x74, 0x75, 0x72, 0x65, 0x20, 0x54, 0x72, 0x75, 0x73, 0x74, 0x20, 0x43,
  0x6f, 0x2e, 0x31, 0x17, 0x30, 0x15, 0x06, 0x03, 0x55, 0x04, 0x03, 0x13,
  0x0e, 0x44, 0x53, 0x54, 0x20, 0x52, 0x6f, 0x6f, 0x74, 0x20, 0x43, 0x41,
  0x20, 0x58, 0x33, 0x30, 0x1e, 0x17, 0x0d, 0x30, 0x30, 0x30, 0x39, 0x33,
  0x30, 0x32, 0x31, 0x31, 0x32, 0x31, 0x39, 0x5a, 0x17, 0x0d, 0x32, 0x31,
  0x30, 0x39, 0x33, 0x30, 0x31, 0x34, 0x30, 0x31, 0x31, 0x35, 0x5a, 0x30,
  0x3f, 0x31, 0x24, 0x30, 0x22, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x13, 0x1b,
  0x44, 0x69, 0x67, 0x69, 0x74, 0x61, 0x6c, 0x20, 0x53, 0x69, 0x67, 0x6e,
  0x61, 0x74, 0x75, 0x72, 0x65, 0x20, 0x54, 0x72, 0x75, 0x73, 0x74, 0x20,
  0x43, 0x6f, 0x2e, 0x31, 0x17, 0x30, 0x15, 0x06, 0x03, 0x55, 0x04, 0x03,
  0x13, 0x0e, 0x44, 0x53, 0x54, 0x20, 0x52, 0x6f, 0x6f, 0x74, 0x20, 0x43,
  0x41, 0x20, 0x58, 0x33, 0x30, 0x82, 0x01, 0x22, 0x30, 0x0d, 0x06, 0x09,
  0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x03,
  0x82, 0x01, 0x0f, 0x00, 0x30, 0x82, 0x01, 0x0a, 0x02, 0x82, 0x01, 0x01,
  0x00, 0xdf, 0xaf, 0xe9, 0x97, 0x50, 0x08, 0x83, 0x57, 0xb4, 0xcc, 0x62,
  0x65, 0xf6, 0x90, 0x82, 0xec, 0xc7, 0xd3, 0x2c, 0x6b, 0x30, 0xca, 0x5b,
  0xec, 0xd9, 0x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f1, 0x97, 0xeb, 0xd5, 0x9a, 0x9b, 0x8d, 0x1d,
  0xba, 0xda, 0x25, 0xb9, 0xc6, 0xd8, 0xdf, 0xc1, 0x15, 0x02, 0x3a, 0xab,
  0xda, 0x6e, 0xf1, 0x3e, 0x2e, 0xf5, 0x5c, 0x08, 0x9c, 0x3c, 0xd6, 0x83,
  0x69, 0xe4, 0x10, 0x9b, 0x19, 0x2a, 0xb6, 0x29, 0x57, 0xe3, 0xe5, 0x3d,
  0x9b, 0x9f, 0xf0, 0x02, 0x5d, 0x02, 0x03, 0x01, 0x00, 0x01, 0xa3, 0x42,
  0x30, 0x40, 0x30, 0x0f, 0x06, 0x03, 0x55, 0x1d, 0x13, 0x01, 0x01, 0xff,
  0x04, 0x05, 0x30, 0x03, 0x01, 0x01, 0xff, 0x30, 0x0e, 0x06, 0x03, 0x55,
  0x1d, 0x0f, 0x01, 0x01, 0xff, 0x04, 0x04, 0x03, 0x02, 0x01, 0x06, 0x30,
  0x1d, 0x06, 0x03, 0x55, 0x1d, 0x0e, 0x04, 0x16, 0x04, 0x14, 0xc4, 0xa7,
  0xb1, 0xa4, 0x7b, 0x2c, 0x71, 0xfa, 0xdb, 0xe1, 0x4b, 0x90, 0x75, 0xff,
  0xc4, 0x15, 0x60, 0x85, 0x89, 0x10, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86,
  0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x05, 0x05, 0x00, 0x03, 0x82, 0x01,
  0x01, 0x00, 0xa3, 0x1a, 0x2c, 0x9b, 0x17, 0x00, 0x5c, 0xa9, 0x1e, 0xee,
  0x28, 0x66, 0x37, 0x3a, 0xbf, 0x83, 0xc7, 0x3f, 0x4b, 0xc3, 0x09, 0xa0,
  0x95, 0x20, 0x5d, 0xe3, 0xd9, 0x59, 0x44, 0xd2, 0x3e, 0x0d, 0x3e, 0xbd,
  0x8a, 0x4b, 0xa0, 0x74, 0x1f, 0xce, 0x10, 0x82, 0x9c, 0x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 0x28,
  0x39, 0xbf, 0x15, 0x65, 0x1c, 0xc8, 0xf6, 0x77, 0x96, 0x6a, 0x0a, 0x8d,
  0x77, 0x0b, 0xd8, 0x91, 0x0b, 0x04, 0x8e, 0x07, 0xdb, 0x29, 0xb6, 0x0a,
  0xee, 0x9d, 0x82, 0x35, 0x35, 0x10
};
unsigned int dst_root_ca_x3_bin_crt_len = 846;

3.) To load the certificate a created _ssl context is needed. This context seems to be only available _after_ you called <WiFiClientSecure-Instance>.connect(). If you try to load the certificate before, it will just fail silently. I found this by adding DEBUGV code to WiFiClientSecure::setCACert.

If you follow the above steps, then you'll get something like:

Code: Select alldomain name: 'my.influxdb.server'
certificate matches

which looks good so far.

I'll try to interchange the certificate by a valid self-signed the next days to see, if it fails as expected. Also I'll try to read the certificate from SPIFFS instead of putting it in the code. But for my esp01 modules I think I'll leave it in the code as I can save the whole SPIFFS stack then.

Re: Question on verifyCertChain

PostPosted: Sun Apr 23, 2017 2:58 pm
by Gadzair
Hey; this is not working for me. Can you check to see if I did the right steps;

Code: Select all  WiFiClientSecure client;
 
  int res = client.connect("letsencrypt.org", 443);
  Serial.printf("%d\n", res);

  bool success;
  success = client.setCACert(dst_root_ca_x3_bin_crt, dst_root_ca_x3_bin_crt_len);
  Serial.printf("setCACert: %d\n", (int) success);
 
  success = client.verifyCertChain("letsencrypt.org");
  Serial.printf("verifyCertChain: %d\n", (int) success);
 
  client.stop();


My output is:
...
1
setCACert: 1
please start sntp first !
ssl_verify_cert returned -516
verifyCertChain: 0
...

Any ideas on what I'm doing wrong here?

Re: Question on verifyCertChain

PostPosted: Sun Apr 23, 2017 6:00 pm
by Gadzair
Hi,

I tried following your suggestions but it isn't working me. Can you take a look and let me know if this basically is what you did:

Code: Select all  WiFiClientSecure client;
 
  int res = client.connect("letsencrypt.org", 443);
  Serial.printf("%d\n", res);

  bool success;
  success = client.setCACert(dst_root_ca_x3_bin_crt, dst_root_ca_x3_bin_crt_len);
  Serial.printf("setCACert: %d\n", (int) success);
 
  success = client.verifyCertChain("letsencrypt.org");
  Serial.printf("verifyCertChain: %d\n", (int) success);
 
  client.stop();


My output:
1
setCACert: 1
please start sntp first !
ssl_verify_cert returned -516
verifyCertChain: 0