Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Insecure mode #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 7 additions & 12 deletions examples/GettingStarted/basic/basic.ino
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,8 @@ ArrowheadESP Arrowhead;

const char* systemName = "securetemperaturesensor"; // name of the system, must match the common name of the certificate
int port = 8080; // doesn't really matter what number we type in, since it won't listen on it anyway
const char* publicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAubjry6ja1CZUGtRV/0WX" \
"C37s+mWgWJPtqJxMzaQPP+geZVlnm9QlDBAM+UW5Kjc1BWUPfCrVK6hAmHJQj7T2" \
"v76/+uV+E8vkg4bFjmwutUENItaZGivxf8Fy1lEEGs0168w1YEvyvrljtaK6Vu3O" \
"/Yum/KWHf2sGOEJv5xRTZy0HcfIDxXlXGEK43klrcrMAgp/AT59QosBh5zpyvK2K" \
"hxeO44pFt+EwYtgFYQ2t0gZWnWzHt1e6Hj5/80SAJvWL8IUHcWNzc3BFXfjn503Q" \
"kbLsSWnFC2uwi5tMdgiZd1zTSYrmkDfr4AMNZO8RxC0bCaM3MxUl+SnSLiPyO6yU" \
"PwIDAQAB";
const char* publicKey = "";
String ArrowheadProviderIP = "91.161.102.110";

void setup() {
// put your setup code here, to run once:
Expand All @@ -33,21 +28,21 @@ void setup() {
Arrowhead.getArrowheadESPFS().getProviderInfo().serviceRegistryPort
);

bool startupSuccess = Arrowhead.begin(); // true of connection to WiFi and loading Certificates is successful
bool startupSuccess = Arrowhead.begin(false); // true of connection to WiFi and loading Certificates is successful
if(startupSuccess){
String response = "";
int statusCode = Arrowhead.serviceRegistryEcho(&response);
int statusCode = Arrowhead.serviceRegistryEcho(false, &response);
Serial.print("Status code from server: ");
Serial.println(statusCode);
Serial.print("Response body from server: ");
Serial.println(response);
Serial.println("############################");
Serial.println();

String serviceRegistryEntry = "{\"endOfValidity\":\"2020-12-05 12:00:00\",\"interfaces\":[\"HTTP-SECURE-SenML\"],\"providerSystem\":{\"address\":\" "+ Arrowhead.getIP() +"\",\"authenticationInfo\":\""+ publicKey +"\",\"port\":"+ port +",\"systemName\":\""+ systemName +"\"},\"secure\":\"CERTIFICATE\",\"serviceDefinition\":\"temperature\",\"serviceUri\":\"/\",\"version\":1}";
String serviceRegistryEntry = "{\"endOfValidity\":\"2021-12-05 12:00:00\",\"interfaces\":[\"HTTP-INSECURE-SenML\"],\"providerSystem\":{\"address\":\" "+ ArrowheadProviderIP +"\",\"authenticationInfo\":\""+ publicKey +"\",\"port\":"+ port +",\"systemName\":\""+ systemName +"\"},\"secure\":\"NOT_SECURE\",\"serviceDefinition\":\"dhtesp1\",\"serviceUri\":\"/\",\"version\":1}";

response = "";
statusCode = Arrowhead.serviceRegistryRegister(serviceRegistryEntry.c_str(), &response);
statusCode = Arrowhead.serviceRegistryRegister(false, serviceRegistryEntry.c_str(), &response);
Serial.print("Status code from server: ");
Serial.println(statusCode);
Serial.print("Response body from server: ");
Expand All @@ -63,4 +58,4 @@ void loop() {
// put your main code here, to run repeatedly:

yield();
}
}
152 changes: 90 additions & 62 deletions src/ArrowheadESP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,62 +56,62 @@ bool ArrowheadESP::setupWiFi() {
return false;
}

bool ArrowheadESP::setupCertificates() {
// Set up the NTPClient. It's constructor needs an UDP client. If somebody has a better solution then declaring it here, let me know.

bool ArrowheadESP::setupCertificates(bool secureMode) {

// By default 'pool.ntp.org' is used with 60 seconds update interval and
// no offset
NTPClient timeClient(_ntpUDP);
timeClient.begin();
while (!timeClient.update()) {
timeClient.forceUpdate();
}
// Set the proper time for Certificate validation
getArrowheadHTTPSClient().getWiFiClientSecure().setX509Time(timeClient.getEpochTime());
// Setting the request timeout
getArrowheadHTTPSClient().getWiFiClientSecure().setTimeout(5000);
// Setting the buffer sizes
getArrowheadHTTPSClient().getWiFiClientSecure().setBufferSizes(512, 512);

// Disable X509 Certificate verification
if (getArrowheadESPFS().getSSLInfo().insecure) {
getArrowheadHTTPSClient().getWiFiClientSecure().setInsecure();
debugPrintln("Disabled CA verification");
}

if(getArrowheadESPFS().loadClientCertificateFiles()) {

// Load CA certificate
if (getArrowheadHTTPSClient().getWiFiClientSecure().loadCACert(getArrowheadESPFS().getCA())) {
debugPrintln("CA cert loaded");
} else {
debugPrintln("CA cert failed");
if (secureMode) {
// Set the proper time for Certificate validation
getArrowheadHTTPSClient().getWiFiClientSecure().setX509Time(timeClient.getEpochTime());
// Setting the request timeout
getArrowheadHTTPSClient().getWiFiClientSecure().setTimeout(5000);
// Setting the buffer sizes
getArrowheadHTTPSClient().getWiFiClientSecure().setBufferSizes(512, 512);

// Disable X509 Certificate verification
if (getArrowheadESPFS().getSSLInfo().insecure) {
getArrowheadHTTPSClient().getWiFiClientSecure().setInsecure();
debugPrintln("Disabled CA verification");
}
//delay(1000);

// Load Client certificate
if (getArrowheadHTTPSClient().getWiFiClientSecure().loadCertificate(getArrowheadESPFS().getCl())) {
debugPrintln("Client cert loaded");
} else {
debugPrintln("Client cert failed");
}
//delay(1000);
if(getArrowheadESPFS().loadClientCertificateFiles()) {

// Load CA certificate
if (getArrowheadHTTPSClient().getWiFiClientSecure().loadCACert(getArrowheadESPFS().getCA())) {
debugPrintln("CA cert loaded");
} else {
debugPrintln("CA cert failed");
}
//delay(1000);

// Load Client certificate
if (getArrowheadHTTPSClient().getWiFiClientSecure().loadCertificate(getArrowheadESPFS().getCl())) {
debugPrintln("Client cert loaded");
} else {
debugPrintln("Client cert failed");
}
//delay(1000);

// Load Private key
if (getArrowheadHTTPSClient().getWiFiClientSecure().loadPrivateKey(getArrowheadESPFS().getPK())) {
debugPrintln("Private key loaded");
} else {
debugPrintln("Private key failed");
}

// close client certificate files
getArrowheadESPFS().closeClientCertificateFiles();

// Load Private key
if (getArrowheadHTTPSClient().getWiFiClientSecure().loadPrivateKey(getArrowheadESPFS().getPK())) {
debugPrintln("Private key loaded");
} else {
debugPrintln("Private key failed");
}

// close client certificate files
getArrowheadESPFS().closeClientCertificateFiles();

}
else {
Serial.println("Client certificate files could not be loaded.");
else {
Serial.println("Client certificate files could not be loaded.");
}
}

delay(1000);
}

Expand Down Expand Up @@ -156,6 +156,10 @@ ArrowheadESPFS &ArrowheadESP::getArrowheadESPFS() {
return _arrowheadEspFs;
}

ArrowheadHTTPClient &ArrowheadESP::getArrowheadHTTPClient() {
return _httpClient;
}

ArrowheadHTTPSClient &ArrowheadESP::getArrowheadHTTPSClient() {
return _httpsClient;
}
Expand All @@ -177,50 +181,74 @@ void ArrowheadESP::setServiceRegistryAddress(const char *host, int port) {
this->_srPort = port;
}

int ArrowheadESP::serviceRegistryEcho() {
return getArrowheadHTTPSClient().get(_srHost, _srPort, "/serviceregistry/echo", NULL, NULL);
int ArrowheadESP::serviceRegistryEcho(bool secureMode) {
if (secureMode)
return getArrowheadHTTPSClient().get(_srHost, _srPort, "/serviceregistry/echo", NULL, NULL);
else
return getArrowheadHTTPClient().get(_srHost, _srPort, "/serviceregistry/echo", NULL, NULL);
}

int ArrowheadESP::serviceRegistryEcho(String *response) {
return getArrowheadHTTPSClient().get(_srHost, _srPort, "/serviceregistry/echo", NULL, response);
int ArrowheadESP::serviceRegistryEcho(bool secureMode, String *response) {
if (secureMode)
return getArrowheadHTTPSClient().get(_srHost, _srPort, "/serviceregistry/echo", NULL, response);
else
return getArrowheadHTTPClient().get(_srHost, _srPort, "/serviceregistry/echo", NULL, response);
}

int ArrowheadESP::serviceRegistryQuery(const char *body) {
return getArrowheadHTTPSClient().post(_srHost, _srPort, "/serviceregistry/query", body);
int ArrowheadESP::serviceRegistryQuery(bool secureMode, const char *body) {
if (secureMode)
return getArrowheadHTTPSClient().post(_srHost, _srPort, "/serviceregistry/query", body);
else
return getArrowheadHTTPClient().post(_srHost, _srPort, "/serviceregistry/query", body);
}

int ArrowheadESP::serviceRegistryQuery(const char *body, String *response) {
return getArrowheadHTTPSClient().post(_srHost, _srPort, "/serviceregistry/query", body, response);
int ArrowheadESP::serviceRegistryQuery(bool secureMode, const char *body, String *response) {
if (secureMode)
return getArrowheadHTTPSClient().post(_srHost, _srPort, "/serviceregistry/query", body, response);
else
return getArrowheadHTTPClient().post(_srHost, _srPort, "/serviceregistry/query", body, response);
}

int ArrowheadESP::serviceRegistryRegister(const char *body) {
return getArrowheadHTTPSClient().post(_srHost, _srPort, "/serviceregistry/register", body);
int ArrowheadESP::serviceRegistryRegister(bool secureMode, const char *body) {
if (secureMode)
return getArrowheadHTTPSClient().post(_srHost, _srPort, "/serviceregistry/register", body);
else
return getArrowheadHTTPClient().post(_srHost, _srPort, "/serviceregistry/register", body);
}

int ArrowheadESP::serviceRegistryRegister(const char *body, String *response) {
return getArrowheadHTTPSClient().post(_srHost, _srPort, "/serviceregistry/register", body, response);
int ArrowheadESP::serviceRegistryRegister(bool secureMode, const char *body, String *response) {
if (secureMode)
return getArrowheadHTTPSClient().post(_srHost, _srPort, "/serviceregistry/register", body, response);
else
return getArrowheadHTTPClient().post(_srHost, _srPort, "/serviceregistry/register", body, response);
}

int ArrowheadESP::serviceRegistryUnregister(const char *systemName, int port, const char *serviceDefinition) {
int ArrowheadESP::serviceRegistryUnregister(bool secureMode, const char *systemName, int port, const char *serviceDefinition) {
String query = String("?system_name=") + systemName + "&address=" + WiFi.localIP().toString() + "&port=" + port + "&service_definition=" +
serviceDefinition;
return getArrowheadHTTPSClient().del(_srHost, _srPort, "/serviceregistry/unregister", query.c_str());
if (secureMode)
return getArrowheadHTTPSClient().del(_srHost, _srPort, "/serviceregistry/unregister", query.c_str());
else
return getArrowheadHTTPClient().del(_srHost, _srPort, "/serviceregistry/unregister", query.c_str());
}

int ArrowheadESP::serviceRegistryUnregister(const char *systemName, int port, const char *serviceDefinition, String *response) {
int ArrowheadESP::serviceRegistryUnregister(bool secureMode, const char *systemName, int port, const char *serviceDefinition, String *response) {
String query = String("?system_name=") + systemName + "&address=" + WiFi.localIP().toString() + "&port=" + port + "&service_definition=" +
serviceDefinition;
return getArrowheadHTTPSClient().del(_srHost, _srPort, "/serviceregistry/unregister", query.c_str(), response);
if (secureMode)
return getArrowheadHTTPSClient().del(_srHost, _srPort, "/serviceregistry/unregister", query.c_str(), response);
else
return getArrowheadHTTPClient().del(_srHost, _srPort, "/serviceregistry/unregister", query.c_str(), response);
}

bool ArrowheadESP::begin() {
bool ArrowheadESP::begin(bool secureMode) {
debugPrintln("ArrowheadESP - Begin");
// Cannot proceed without WiFi connection
if (!setupWiFi()) {
return false;
}
//delay(1000);
setupCertificates();
setupCertificates(secureMode);

if (MDNS.begin("esp8266")) {
Serial.println("MDNS responder started");
Expand Down
36 changes: 24 additions & 12 deletions src/ArrowheadESP.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

// Header Includes
#include "ArrowheadESPFS/ArrowheadESPFS.h"
#include "ArrowheadHTTPClient/ArrowheadHTTPClient.h"
#include "ArrowheadHTTPSClient/ArrowheadHTTPSClient.h"

class ArrowheadESP {
Expand All @@ -33,6 +34,11 @@ class ArrowheadESP {
*/
ArrowheadESPFS _arrowheadEspFs;

/**
* ArrowheadHTTPClient instance
*/
ArrowheadHTTPClient _httpClient;

/**
* ArrowheadHTTPSClient instance
*/
Expand Down Expand Up @@ -81,7 +87,7 @@ class ArrowheadESP {
*
* @return
*/
bool setupCertificates();
bool setupCertificates(bool secureMode);
/**
* Sets up the secure web server with certificates
*
Expand All @@ -101,6 +107,13 @@ class ArrowheadESP {
*/
ArrowheadESPFS& getArrowheadESPFS();

/**
* Returns the instance of the ArrowheadHTTPClient
*
* @return
*/
ArrowheadHTTPClient& getArrowheadHTTPClient();

/**
* Returns the instance of the ArrowheadHTTPSClient
*
Expand Down Expand Up @@ -151,66 +164,66 @@ class ArrowheadESP {
* Echo
* @return
*/
int serviceRegistryEcho();
int serviceRegistryEcho(bool secureMode);
/**
* Echo
*
* @param response
* @return
*/
int serviceRegistryEcho(String* response);
int serviceRegistryEcho(bool secureMode, String* response);
/**
* Query
*
* @param body
* @return
*/
int serviceRegistryQuery(const char *body);
int serviceRegistryQuery(bool secureMode, const char *body);
/**
* Query
*
* @param body
* @param response
* @return
*/
int serviceRegistryQuery(const char *body, String* response);
int serviceRegistryQuery(bool secureMode, const char *body, String* response);
/**
* Register
*
* @param body
* @return
*/
int serviceRegistryRegister(const char *body);
int serviceRegistryRegister(bool secureMode, const char *body);
/**
* Register
*
* @param body
* @param response
* @return
*/
int serviceRegistryRegister(const char *body, String* response);
int serviceRegistryRegister(bool secureMode, const char *body, String* response);
/**
* Unregister
*
* @param serviceDefinition
* @return
*/
int serviceRegistryUnregister(const char *systemName, int port, const char *serviceDefinition);
int serviceRegistryUnregister(bool secureMode, const char *systemName, int port, const char *serviceDefinition);
/**
* Unregister
*
* @param serviceDefinition
* @param response
* @return
*/
int serviceRegistryUnregister(const char *systemName, int port, const char *serviceDefinition, String* response);
int serviceRegistryUnregister(bool secureMode, const char *systemName, int port, const char *serviceDefinition, String* response);

/**
* Starts the operation of the library
*
* @return
*/
bool begin();
bool begin(bool secureMode);

/**
* Keeps all connection alive
Expand All @@ -220,5 +233,4 @@ class ArrowheadESP {
int loop();
};


#endif //ARROWHEADESP_ARROWHEADESP_H
#endif //ARROWHEADESP_ARROWHEADESP_H
Loading