-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathsslHelper.cpp
136 lines (113 loc) · 3.56 KB
/
sslHelper.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
//
//
//
#include "GlobalDefine.h"
#include "sslHelper.h"
#undef FOUND_BOARD
#ifdef ARDUINO_ARCH_ESP8266
#include <ESP8266HTTPClient.h>
#include <ESP8266WiFi.h>
#define FOUND_BOARD ESP8266
#pragma message(Reminder "Target hardware: ESP8266")
#endif
#ifdef ARDUINO_ARCH_ESP32
#include <HTTPClient.h>
#include <WiFi.h>
#define FOUND_BOARD ESP32
#pragma message(Reminder "Target hardware: ESP32")
#endif
#ifndef FOUND_BOARD
#pragma message(Reminder "Error Target hardware not defined !")
#endif // ! FOUND_BOARD
void testSSL() {
WIFI_CLIENT_CLASS client;
Serial.println("TestSSL");
SET_HEAP_MESSAGE("testSSL");
HEAP_DEBUG_PRINTLN(DEFAULT_DEBUG_MESSAGE);
const char* host = "api.github.com";
const int httpsPort = 443;
// Use web browser to view and copy
// SHA1 fingerprint of the certificate
const char* fingerprint = "5F F1 60 31 09 04 3E F2 90 D2 B0 8A 50 38 04 E8 37 9F BC 76"; //"35 85 74 EF 67 35 A7 CE 40 69 50 F3 C0 F6 80 CF 80 3B 2E 19";
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
// Use WiFiClientSecure class to create TLS connection
// WiFiClientSecure client;
Serial.print("connecting to ");
Serial.println(host);
#ifdef ARDUINO_ARCH_ESP8266
client.setInsecure(); // TODO fix this. Needed for BearSSL
#endif
#ifdef ARDUINO_ARCH_ESP32
//client.setInsecure(); // not implemented in ESP32
#endif
// client.setInsecure();
if (!client.connect(host, httpsPort)) {
Serial.println("TLS connection failed");
delay(10000);
return;
}
#ifdef ARDUINO_ARCH_ESP8266
if (client.verify(fingerprint, host)) {
Serial.println("TLS certificate matches");
}
else {
Serial.println("TLS client.verify not implemented in ESP32");
}
#endif
#ifdef ARDUINO_ARCH_ESP32
Serial.println("TLS certificate doesn't match");
#endif
HEAP_DEBUG_PRINTLN(DEFAULT_DEBUG_MESSAGE);
HEAP_DEBUG_PRINTLN(DEFAULT_DEBUG_MESSAGE);
// String url = "/repos/esp8266/Arduino/commits/master/status";
String url = "/repos/espressif/arduino-esp32/commits/master/status";
Serial.print("requesting URL: ");
Serial.println(url);
Serial.println(DEBUG_SEPARATOR);
String tlsHTML = String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"User-Agent: BuildFailureDetectorESP32\r\n" +
"Connection: close\r\n\r\n";
Serial.println("tlsHTML:");
Serial.println(DEBUG_SEPARATOR);
Serial.println(tlsHTML);
Serial.println(DEBUG_SEPARATOR);
client.print(tlsHTML);
Serial.println("request sent");
HEAP_DEBUG_PRINT(getHeapMsg()); HEAP_DEBUG_PRINTLN(DEFAULT_DEBUG_MESSAGE);
while (client.connected()) {
String line = client.readStringUntil('\n');
if (line == "\r") {
Serial.println("header received:");
Serial.println(line);
break;
}
}
String line = client.readStringUntil('\n');
Serial.println("");
Serial.println("Next line:");
Serial.println(line);
if (line.startsWith("{\"state\":\"success\"")) {
Serial.println("arduino-esp32/Arduino CI successfull!");
}
else {
Serial.println("arduino-esp32/Arduino CI has failed");
}
Serial.println("reply was:");
Serial.println(DEBUG_SEPARATOR);
Serial.println(line);
Serial.println(DEBUG_SEPARATOR);
Serial.println("closing connection");
HEAP_DEBUG_PRINTLN(DEFAULT_DEBUG_MESSAGE);
line = "";
// client.stop(); // don't stop the local client here, as other instances will not be able to reconnect (TODO - have exactly one WiFiClientSecure client;)
HEAP_DEBUG_PRINTLN(DEFAULT_DEBUG_MESSAGE);
Serial.println("Flush...");
client.flush();
HEAP_DEBUG_PRINTLN(DEFAULT_DEBUG_MESSAGE);
Serial.println("Stop...");
client.stop();
HEAP_DEBUG_PRINTLN(DEFAULT_DEBUG_MESSAGE);
}