Add EthernetMQTT example to demonstrate mTLS (credit @Atalonica)
This commit is contained in:
parent
7a1a2a823b
commit
5ae689bf72
2 changed files with 162 additions and 0 deletions
87
examples/EthernetMQTT/EthernetMQTT.ino
Normal file
87
examples/EthernetMQTT/EthernetMQTT.ino
Normal file
|
@ -0,0 +1,87 @@
|
|||
/*
|
||||
Basic MQTT example (with SSL!)
|
||||
This sketch demonstrates the basic capabilities of the library.
|
||||
It connects to an MQTT server then:
|
||||
- publishes "hello world" to the topic "outTopic"
|
||||
- subscribes to the topic "inTopic", printing out any messages
|
||||
it receives. NB - it assumes the received payloads are strings not binary
|
||||
It will reconnect to the server if the connection is lost using a blocking
|
||||
reconnect function. See the 'mqtt_reconnect_nonblocking' example for how to
|
||||
achieve the same result without blocking the main loop.
|
||||
|
||||
You will need to populate "certificates.h" with your trust anchors
|
||||
(see https://github.com/OPEnSLab-OSU/SSLClient/blob/master/TrustAnchors.md)
|
||||
and my_cert/my_key with your certificate/private key pair
|
||||
(see https://github.com/OPEnSLab-OSU/SSLClient#mtls).
|
||||
*/
|
||||
#include <SPI.h>
|
||||
#include <EthernetLarge.h>
|
||||
#include <SSLClient.h>
|
||||
#include "certificates.h" // This file must be regenerated
|
||||
#include <PubSubClient.h>
|
||||
|
||||
const char my_cert[] = "FIXME";
|
||||
const char my_key[] = "FIXME";
|
||||
SSLClientParameters mTLS = SSLClientParameters::fromPEM(my_cert, sizeof my_cert, my_key, sizeof my_key);
|
||||
|
||||
byte mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED };
|
||||
const char* mqttServer = "broker.example"; // Broker address
|
||||
IPAddress ip (192, 168, 1, 2); // Custom client static IP
|
||||
|
||||
void callback(char* topic, byte* payload, unsigned int length) {
|
||||
Serial.print("Message arrived [");
|
||||
Serial.print(topic);
|
||||
Serial.print("] ");
|
||||
for (int i=0;i<length;i++) {
|
||||
Serial.print((char)payload[i]);
|
||||
}
|
||||
Serial.println();
|
||||
}
|
||||
|
||||
EthernetClient ethClient;
|
||||
SSLClient ethClientSSL(ethClient, TAs, (size_t)TAs_NUM, A5);
|
||||
PubSubClient client(mqttServer, 8883, callback, ethClientSSL);
|
||||
|
||||
void reconnect() {
|
||||
// Loop until we're reconnected
|
||||
while (!client.connected()) {
|
||||
Serial.print("Attempting MQTT connection...");
|
||||
// Attempt to connect
|
||||
if (client.connect("arduinoClient")) {
|
||||
Serial.println("connected");
|
||||
// Once connected, publish an announcement...
|
||||
client.publish("outTopic","hello world");
|
||||
// ... and resubscribe
|
||||
client.subscribe("inTopic");
|
||||
} else {
|
||||
Serial.print("failed, rc=");
|
||||
Serial.print(client.state());
|
||||
Serial.println(" try again in 5 seconds");
|
||||
// Wait 5 seconds before retrying
|
||||
delay(5000);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void setup(){
|
||||
// Start Serial
|
||||
Serial.begin(115200);
|
||||
while(!Serial);
|
||||
// Enable mutual TLS with SSLClient
|
||||
ethClientSSL.setMutualAuthParams(mTLS);
|
||||
// You can use Ethernet.init(pin) to configure the CS pin
|
||||
Ethernet.init(10); // Most Arduino shields
|
||||
//Ethernet.init(5); // MKR ETH shield
|
||||
//Ethernet.init(0); // Teensy 2.0
|
||||
//Ethernet.init(20); // Teensy++ 2.0
|
||||
//Ethernet.init(15); // ESP8266 with Adafruit Featherwing Ethernet
|
||||
//Ethernet.init(33); // ESP32 with Adafruit Featherwing Ethernet
|
||||
Ethernet.begin(mac, ip);
|
||||
}
|
||||
|
||||
void loop(){
|
||||
if (!client.connected()) {
|
||||
reconnect();
|
||||
}
|
||||
client.loop();
|
||||
}
|
75
tools/pycert_bearssl/certificates.h
Normal file
75
tools/pycert_bearssl/certificates.h
Normal file
|
@ -0,0 +1,75 @@
|
|||
#ifndef _CERTIFICATES_H_
|
||||
#define _CERTIFICATES_H_
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
/* This file is auto-generated by the pycert_bearssl tool. Do not change it manually.
|
||||
* Certificates are BearSSL br_x509_trust_anchor format. Included certs:
|
||||
*
|
||||
* Index: 0
|
||||
* Label: OPEnS
|
||||
* Subject: C=US,ST=OR,L=Corvallis,O=OPEnS
|
||||
*/
|
||||
|
||||
#define TAs_NUM 1
|
||||
|
||||
static const unsigned char TA_DN0[] = {
|
||||
0x30, 0x3e, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13,
|
||||
0x02, 0x55, 0x53, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x08,
|
||||
0x0c, 0x02, 0x4f, 0x52, 0x31, 0x12, 0x30, 0x10, 0x06, 0x03, 0x55, 0x04,
|
||||
0x07, 0x0c, 0x09, 0x43, 0x6f, 0x72, 0x76, 0x61, 0x6c, 0x6c, 0x69, 0x73,
|
||||
0x31, 0x0e, 0x30, 0x0c, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x05, 0x4f,
|
||||
0x50, 0x45, 0x6e, 0x53,
|
||||
};
|
||||
|
||||
static const unsigned char TA_RSA_N0[] = {
|
||||
0xe2, 0x80, 0x32, 0x18, 0xe7, 0xa6, 0x94, 0x04, 0x62, 0x43, 0xbc, 0x1e,
|
||||
0x14, 0x13, 0xaf, 0x40, 0x82, 0x0e, 0x14, 0x57, 0xe8, 0x50, 0x98, 0x38,
|
||||
0xa4, 0xd9, 0xf1, 0x8a, 0x20, 0x5f, 0x37, 0xae, 0xd1, 0xdf, 0xc1, 0xc0,
|
||||
0x53, 0x47, 0xd2, 0x4b, 0x10, 0x63, 0xe8, 0x8f, 0xb5, 0x58, 0x9f, 0xd8,
|
||||
0x69, 0x86, 0x31, 0xbc, 0x9f, 0x39, 0xf5, 0x88, 0xb3, 0xae, 0xcb, 0x25,
|
||||
0x50, 0xc0, 0x47, 0x7c, 0xde, 0x68, 0xdc, 0x5c, 0xb6, 0x68, 0xef, 0x17,
|
||||
0xe3, 0xf1, 0xd9, 0x41, 0xb1, 0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x60, 0x0a, 0xc4, 0x58,
|
||||
0xca, 0x8e, 0x26, 0x03,
|
||||
};
|
||||
|
||||
static const unsigned char TA_RSA_E0[] = {
|
||||
0x01, 0x00, 0x01,
|
||||
};
|
||||
|
||||
static const br_x509_trust_anchor TAs[] = {
|
||||
{
|
||||
{ (unsigned char *)TA_DN0, sizeof TA_DN0 },
|
||||
BR_X509_TA_CA,
|
||||
{
|
||||
BR_KEYTYPE_RSA,
|
||||
{ .rsa = {
|
||||
(unsigned char *)TA_RSA_N0, sizeof TA_RSA_N0,
|
||||
(unsigned char *)TA_RSA_E0, sizeof TA_RSA_E0,
|
||||
} }
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
#ifdef __cplusplus
|
||||
} /* extern "C" */
|
||||
#endif
|
||||
|
||||
#endif /* ifndef _CERTIFICATES_H_ */
|
Loading…
Reference in a new issue