Giter VIP home page Giter VIP logo

tlstcpclient's Introduction

Simple TLS Client library for Particle.

This library is based mbedTLS library version 2.16.3, this can use for several TLS server. Now this library is compatible with every TLS server certificate, client private key certification. Now supported TLS 1.2.

This library's Cipher Suite is based AES128/256 and SHA-1/224/256/384/512 only for application firmware size, here is cipher suite list.

  • TLS_RSA_WITH_AES_[128|256]_GCM_SHA[1|256|512]
  • TLS_EMPTY_RENOGOTIATION_INFO_SCSV

Example

Some sample sketches included(firmware/examples/a1-example.ino).

#include "application.h"

#include "TlsTcpClient/TlsTcpClient.h"

//
// This example connect to the Let's Encrypt HTTPS server.
// Let's Encrypt ROOT Ca PEM file is here ( https://letsencrypt.org/certificates/ )
// If you want to use other Root CA, check your server administrator or own Root CA pem.
//
#define LET_ENCRYPT_CA_PEM                                              \
"-----BEGIN CERTIFICATE----- \r\n"                                      \
"MIIFjTCCA3WgAwIBAgIRANOxciY0IzLc9AUoUSrsnGowDQYJKoZIhvcNAQELBQAw\r\n"  \
"TzELMAkGA1UEBhMCVVMxKTAnBgNVBAoTIEludGVybmV0IFNlY3VyaXR5IFJlc2Vh\r\n"  \
"cmNoIEdyb3VwMRUwEwYDVQQDEwxJU1JHIFJvb3QgWDEwHhcNMTYxMDA2MTU0MzU1\r\n"  \
"WhcNMjExMDA2MTU0MzU1WjBKMQswCQYDVQQGEwJVUzEWMBQGA1UEChMNTGV0J3Mg\r\n"  \
"RW5jcnlwdDEjMCEGA1UEAxMaTGV0J3MgRW5jcnlwdCBBdXRob3JpdHkgWDMwggEi\r\n"  \
"MA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQCc0wzwWuUuR7dyXTeDs2hjMOrX\r\n"  \
"NSYZJeG9vjXxcJIvt7hLQQWrqZ41CFjssSrEaIcLo+N15Obzp2JxunmBYB/XkZqf\r\n"  \
"89B4Z3HIaQ6Vkc/+5pnpYDxIzH7KTXcSJJ1HG1rrueweNwAcnKx7pwXqzkrrvUHl\r\n"  \
"Npi5y/1tPJZo3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"  \
"eO5HyELTgcVlLXXQDgAWnRzut1hFJeczY1tjQQno6f6s+nMydLN26WuU4s3UYvOu\r\n"  \
"OsUxRlJu7TSRHqDC3lSE5XggVkzdaPkuKGQbGpny+01/47hfXXNB7HntWNZ6N2Vw\r\n"  \
"p7G6OfY+YQrZwIaQmhrIqJZuigsrbe3W+gdn5ykE9+Ky0VgVUsfxo52mwFYs1JKY\r\n"  \
"2PGDuWx8M6DlS6qQkvHaRUo0FMd8TsSlbF0/v965qGFKhSDeQoMpYnwcmQilRh/0\r\n"  \
"ayLThlHLN81gSkJjVrPI0Y8xCVPB4twb1PFUd2fPM3sA1tJ83sZ5v8vgFv2yofKR\r\n"  \
"PB0t6JzUA81mSqM3kxl5e+IZwhYAyO0OTg3/fs8HqGTNKd9BqoUwSRBzp06JMg5b\r\n"  \
"rUCGwbCUDI0mxadJ3Bz4WxR6fyNpBK2yAinWEsikxqEt\r\n"  \
"-----END CERTIFICATE----- "
const char letencryptCaPem[] = LET_ENCRYPT_CA_PEM;

#define ONE_DAY_MILLIS (24 * 60 * 60 * 1000)
unsigned long lastSync = millis();

void setup() {
    Serial.begin(9600);

    // need a Particle time sync for X509 certificates verify.
    if (millis() - lastSync > ONE_DAY_MILLIS) {
        Particle.syncTime();
        lastSync = millis();
    }
    Serial.print(Time.timeStr());
}

void loop() {
    unsigned char buff[256];

    TlsTcpClient client;

    // setup Root CA pem.
    client.init(letencryptCaPem, sizeof(letencryptCaPem));

    // connect HTTPS server.
    client.connect("www.hirotakaster.com", 443);

    // check server certificate. if verify failed, TLS connection is alive.
    if (!client.verify()) {
      Serial.println("Server Certificates is in-valid.");
    }

    // Send request to HTTPS web server.
    int len = sprintf((char *)buff, "GET / HTTP/1.0\r\nHost: www.hirotakaster.com\r\nContent-Length: 0\r\n\r\n");
    client.write(buff, len );

    // GET HTTPS response.
    memset(buff, 0, sizeof(buff));
    while(1) {
        // read renponse.
        memset(buff, 0, sizeof(buff));
        int ret = client.read(buff, sizeof(buff) - 1);
        if (ret == MBEDTLS_ERR_SSL_WANT_READ) {
            delay(100);
        } else if (ret <= 0) {
            break;
        } else {
            Serial.println((char *)buff);
        }
    }
    delay(10000);
}

tlstcpclient's People

Contributors

hirotakaster avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

tlstcpclient's Issues

Firebase cloud notification no answer

Hello hiro,
i'm working on firebase cloud notifications with your TLS library...
Below my code snippet

// This #include statement was automatically added by the Particle IDE.
#include <TlsTcpClient.h>

TlsTcpClient secureClient;

String TOKEN = "*****";
String SERVER_AUTH = "*****";

boolean sent = false;

void setup() {
    Serial.begin(9600);
    delay(1000);
    Serial.println("OK");
}

void loop() {

    if (!sent) {
        sendNotificationToDevice();
        Serial.println("Sent");
        sent = true;
    }

}

void sendNotificationToDevice() {

    unsigned char buff[1024];
    String data = "{";
    data += "\"to\":\"";
    data += TOKEN;
    data += "\",";
    data += "\"notification\":{";
    data += "\"body\":\"asdsadsa\",";
    data += "\"title\":\"Alarm\",";
    data += "\"sound\":\"Default\"";
    data += "}}";

    if (secureClient.connect("fcm.googleapis.com", 443)){
        int len = sprintf((char *)buff, "POST /fcm/send HTTP/1.1\r\nHost: fcm.googleapis.com\r\nAuthorization: key=%s\r\nContent-Type: application/json\r\nContent-Length: %d\r\n\r\n%s", SERVER_AUTH.c_str(), data.length(), data.c_str());
        Serial.println(String(len));
        secureClient.write(buff, len);
        Serial.println((char*)buff);
    }

    delay(2000);

    while(1) {
       // read renponse.
       memset(buff, 0, sizeof(buff));
       int ret = secureClient.read(buff, sizeof(buff) - 1);
       if (ret == MBEDTLS_ERR_SSL_WANT_READ) {
           delay(100);
           continue;
       } else if (ret <= 0) {
           // no more read.
           Serial.println("\nNo answer");
           break;
       } else if (ret > 0){
           Serial.println((char *)buff);
       }
}

    /*while (secureClient.available()) {
        char c = secureClient.read();
        Serial.print(c);
    }*/

    Serial.println("\nFinished!");
    //secureClient.stop();
}

The serial is showing a well formatted http request but when the client wait for read it reports No Answer

I noticed also
int r = secureClient.connect("fcm.googleapis.com", 443);
it returns -28928
while
int r1 = secureClient.write(buff, len);
it returns -1

How can i fix this?

client.verify() fails on 0.1.15

Thanks for a great library!
My client.verify() fails on 0.1.15. When i switch back to 0.1.14 - it works fine.
I use Let's Encrypt, the same server and same sketch. Only library version differs.
Would be grateful for any help.
Verification fails, but the server gets a correct request afterwards.

Can't connect to an AWS-hosted server

Hello Hirotaka!
Great work with this library!!!

I would like to connect my Particle Photon directly to my database (InfluxDb instance on AWS cloud). The database is behind the AWS load balancer using default security policy.
My idea is to post a value directly from the Photon via an HTTPS post.

Unfortunately, I can't make it working: there is no "invalid certificate" or other errors. I was able to pin-point that the issue could be non compatibility of the ciphers used.
The ciphers supported by AWS are the following: https://docs.aws.amazon.com/elasticloadbalancing/latest/classic/elb-ssl-security-policy.html#ssl-ciphers

Could you please address this issue?

Thanks in advance,
Alex Masolin

undefined reference to `mbedtls_debug_set_threshold'

Thank you for porting this awesome library to particle,

When trying to build this in particle workbench with debug enabled I get the following message:

undefined reference to 'mbedtls_debug_set_threshold'

I have #define MBEDTLS_DEBUG_C and #include mbedtls/debug.h

I'm using a particle Boron with deviceOS 1.4.0

Invalid Certificate on 0.7.0 Version of DeviceOS

From #3 it is mentioned that the library gets an invalid certificate error on 0.7.0 version of the DeviceOS and is recommended to use the 0.6.3 version of the DeviceOS. Particle is now on release/stable version of DeviceOS at 1.0.0. Are there any plans to resolve the issue of invalid certificates on newer versions of the DeviceOS?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.