Added connect and shutdown calls to Arduino sketch. Improved the get cipher suite. Improved error handling.

This commit is contained in:
David Garske
2019-01-07 11:47:33 -08:00
parent 141b263546
commit 0ef4856039
2 changed files with 48 additions and 37 deletions

View File

@ -16,7 +16,7 @@ wolfssl/IDE/ARDUINO directory:
Step 2: Edit `<wolfssl-root>/IDE/ARDUINO/wolfSSL/wolfssl/wolfcrypt/settings.h` uncomment the define for `WOLFSSL_ARDUINO` Step 2: Edit `<wolfssl-root>/IDE/ARDUINO/wolfSSL/wolfssl/wolfcrypt/settings.h` uncomment the define for `WOLFSSL_ARDUINO`
If building for Intel Galileo platform also uncomment the define for `INTEL_GALILEO`. If building for Intel Galileo platform also uncomment the define for `INTEL_GALILEO`.
#####Including wolfSSL in Arduino Libraries (for Arduino version 1.6.6) ##### Including wolfSSL in Arduino Libraries (for Arduino version 1.6.6)
1. In the Arduino IDE: 1. In the Arduino IDE:
- In `Sketch -> Include Library -> Add .ZIP Library...` and choose the - In `Sketch -> Include Library -> Add .ZIP Library...` and choose the

View File

@ -85,7 +85,7 @@ void loop() {
int msgSz = (int)strlen(msg); int msgSz = (int)strlen(msg);
char errBuf[80]; char errBuf[80];
char reply[80]; char reply[80];
WOLFSSL_CIPHER* cipher; const char* cipherName;
if (reconnect) { if (reconnect) {
reconnect--; reconnect--;
@ -93,51 +93,62 @@ void loop() {
Serial.print("Connected to "); Serial.print("Connected to ");
Serial.println(host); Serial.println(host);
ssl = wolfSSL_new(ctx); ssl = wolfSSL_new(ctx);
if (ssl == NULL) { if (ssl == NULL) {
err = wolfSSL_get_error(ssl, 0); Serial.println("Unable to allocate SSL object");
wolfSSL_ERR_error_string(err, errBuf);
Serial.print("Unable to get SSL object. Error = ");
Serial.println(errBuf);
} }
else {
Serial.print("SSL version is "); err = wolfSSL_connect(ssl);
Serial.println(wolfSSL_get_version(ssl)); if (err != WOLFSSL_SUCCESS) {
err = wolfSSL_get_error(ssl, 0);
wolfSSL_ERR_error_string(err, errBuf);
Serial.print("TLS Connect Error: ");
Serial.println(errBuf);
}
Serial.print("SSL version is ");
if ((wolfSSL_write(ssl, msg, strlen(msg))) == msgSz) { Serial.println(wolfSSL_get_version(ssl));
cipher = wolfSSL_get_current_cipher(ssl);
Serial.print("SSL cipher suite is "); cipherName = wolfSSL_get_cipher(ssl);
Serial.println(wolfSSL_CIPHER_get_name(cipher)); Serial.print("SSL cipher suite is ");
Serial.print("Server response: "); Serial.println(cipherName);
while (client.available() || wolfSSL_pending(ssl)) {
input = wolfSSL_read(ssl, reply, sizeof(reply) - 1); if ((wolfSSL_write(ssl, msg, msgSz)) == msgSz) {
total_input += input;
if ( input > 0 ) { Serial.print("Server response: ");
reply[input] = '\0'; while (client.available() || wolfSSL_pending(ssl)) {
Serial.print(reply); input = wolfSSL_read(ssl, reply, sizeof(reply) - 1);
} else if (input < 0) { total_input += input;
err = wolfSSL_get_error(ssl, 0); if (input < 0) {
wolfSSL_ERR_error_string(err, errBuf); err = wolfSSL_get_error(ssl, 0);
Serial.print("wolfSSL_read failed. Error: "); wolfSSL_ERR_error_string(err, errBuf);
Serial.println(errBuf); Serial.print("TLS Read Error: ");
} else { Serial.println(errBuf);
Serial.println(); break;
} } else if (input > 0) {
} reply[input] = '\0';
} else { Serial.print(reply);
Serial.println("SSL_write failed"); } else {
} Serial.println();
}
if (ssl != NULL) }
} else {
err = wolfSSL_get_error(ssl, 0);
wolfSSL_ERR_error_string(err, errBuf);
Serial.print("TLS Write Error: ");
Serial.println(errBuf);
}
wolfSSL_shutdown(ssl);
wolfSSL_free(ssl); wolfSSL_free(ssl);
}
client.stop(); client.stop();
Serial.println("Connection complete."); Serial.println("Connection complete.");
reconnect = 0; reconnect = 0;
} else { } else {
Serial.println("Trying to reconnect..."); Serial.println("Trying to reconnect...");
} }
} }
delay(1000); delay(1000);