Skip to content

Commit d366779

Browse files
committed
rewrite function syncTime, verify a certificate fingerprint
1 parent 6b07559 commit d366779

File tree

1 file changed

+62
-56
lines changed

1 file changed

+62
-56
lines changed

MicroGear.cpp

+62-56
Original file line numberDiff line numberDiff line change
@@ -137,63 +137,69 @@ void MicroGear::syncTime(Client *client, unsigned long *bts) {
137137
int port = (this->securemode)?GEARAUTHSECUREPORT:GEARAUTHPORT;
138138

139139
*bts = 0;
140-
if(client->connect(gearauth,port)){
141-
142-
if (this->securemode) {
143-
WiFiClientSecure *clientsecure = (WiFiClientSecure *)(client);
144-
145-
// verify a certificate fingerprint against a fingerprint saved in eeprom
146-
readEEPROM(tstr, EEPROM_CERTFINGERPRINT, FINGERPRINTSIZE);
147-
#ifdef DEBUG_H
148-
Serial.print("fingerprint loaded from eeprom : ");
149-
Serial.println(tstr);
150-
#endif
151-
if (clientsecure->verify(tstr, gearauth)) {
152-
#ifdef DEBUG_H
153-
Serial.println("fingerprint matched");
154-
#endif
155-
}
156-
else {
157-
#ifdef DEBUG_H
158-
Serial.println("fingerprint mismatched, going to update");
159-
#endif
160-
AuthClient::randomString(nonce,8);
161-
sprintf(tstr,"GET /api/fingerprint/%s/%s HTTP/1.1\r\n\r\n",this->gearkey,nonce);
162-
clientsecure->write((const uint8_t *)tstr,strlen(tstr));
163-
delay(800);
164-
getHTTPReply(clientsecure,tstr,200);
165-
tstr[FINGERPRINTSIZE-1] = '\0'; // split fingerprint and signature
166-
sprintf(hashkey,"%s&%s&%s",this->gearkey,this->gearsecret,nonce);
167-
Sha1.initHmac((uint8_t*)hashkey,strlen(hashkey));
168-
Sha1.HmacBase64(hash, tstr);
169-
for (int i=0;i<HMACSIZE;i++)
170-
if (hash[i]=='/') hash[i] = '_';
171-
172-
if(strcmp(hash,tstr+FINGERPRINTSIZE)==0) {
173-
#ifdef DEBUG_H
174-
Serial.println("new fingerprint updated");
175-
#endif
176-
writeEEPROM(tstr, EEPROM_CERTFINGERPRINT, FINGERPRINTSIZE);
177-
}
178-
else {
179-
#ifdef DEBUG_H
180-
Serial.println("fingerprint verification failed, abort");
181-
#endif
182-
clientsecure->stop();
183-
delay(5000);
184-
return;
185-
}
186-
}
187-
}
188-
189-
strcpy(tstr,"GET /api/time HTTP/1.1\r\n\r\n");
190-
client->write((const uint8_t *)tstr,strlen(tstr));
140+
if (this->securemode) {
141+
WiFiClientSecure *clientsecure = (WiFiClientSecure *)(client);
142+
// verify a certificate fingerprint against a fingerprint saved in eeprom
143+
readEEPROM(tstr, EEPROM_CERTFINGERPRINT, FINGERPRINTSIZE);
144+
#ifdef DEBUG_H
145+
Serial.print("fingerprint loaded from eeprom : ");
146+
Serial.println(tstr);
147+
Serial.print("Host : ");
148+
Serial.println(gearauth);
149+
#endif
150+
clientsecure->setFingerprint(tstr);
151+
if(clientsecure->connect(gearauth,port)){
152+
if (clientsecure->verify(tstr, gearauth)) {
153+
#ifdef DEBUG_H
154+
Serial.println("fingerprint matched");
155+
#endif
156+
}
157+
}
158+
else {
159+
clientsecure->setInsecure();
160+
if(clientsecure->connect(gearauth,port)){
161+
#ifdef DEBUG_H
162+
Serial.println("fingerprint mismatched, going to update");
163+
#endif
164+
AuthClient::randomString(nonce,8);
165+
sprintf(tstr,"GET /api/fingerprint/%s/%s HTTP/1.1\r\n\r\n",this->gearkey,nonce);
166+
clientsecure->write((const uint8_t *)tstr,strlen(tstr));
167+
delay(800);
168+
getHTTPReply(clientsecure,tstr,200);
169+
tstr[FINGERPRINTSIZE-1] = '\0'; // split fingerprint and signature
170+
sprintf(hashkey,"%s&%s&%s",this->gearkey,this->gearsecret,nonce);
171+
Sha1.initHmac((uint8_t*)hashkey,strlen(hashkey));
172+
Sha1.HmacBase64(hash, tstr);
173+
for (int i=0;i<HMACSIZE;i++)
174+
if (hash[i]=='/') hash[i] = '_';
175+
if(strcmp(hash,tstr+FINGERPRINTSIZE)==0) {
176+
#ifdef DEBUG_H
177+
Serial.println("new fingerprint updated");
178+
Serial.print("fingerprint : ");
179+
Serial.println(tstr);
180+
#endif
181+
writeEEPROM(tstr, EEPROM_CERTFINGERPRINT, FINGERPRINTSIZE);
182+
}
183+
else {
184+
#ifdef DEBUG_H
185+
Serial.println("fingerprint verification failed, abort");
186+
#endif
187+
clientsecure->stop();
188+
delay(5000);
189+
return;
190+
}
191+
}
192+
}
193+
}
191194

192-
delay(1000);
193-
getHTTPReply(client,tstr,200);
194-
*bts = atol(tstr) - millis()/1000;
195-
client->stop();
196-
}
195+
if(client->connect(gearauth,port)){
196+
strcpy(tstr,"GET /api/time HTTP/1.1\r\n\r\n");
197+
client->write((const uint8_t *)tstr,strlen(tstr));
198+
delay(1000);
199+
getHTTPReply(client,tstr,200);
200+
*bts = atol(tstr) - millis()/1000;
201+
client->stop();
202+
}
197203
}
198204

199205
MicroGear::MicroGear(Client& netclient ) {

0 commit comments

Comments
 (0)