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,45 +93,56 @@ 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) {
Serial.println("Unable to allocate SSL object");
}
else {
err = wolfSSL_connect(ssl);
if (err != WOLFSSL_SUCCESS) {
err = wolfSSL_get_error(ssl, 0); err = wolfSSL_get_error(ssl, 0);
wolfSSL_ERR_error_string(err, errBuf); wolfSSL_ERR_error_string(err, errBuf);
Serial.print("Unable to get SSL object. Error = "); Serial.print("TLS Connect Error: ");
Serial.println(errBuf); Serial.println(errBuf);
} }
Serial.print("SSL version is "); Serial.print("SSL version is ");
Serial.println(wolfSSL_get_version(ssl)); Serial.println(wolfSSL_get_version(ssl));
cipherName = wolfSSL_get_cipher(ssl);
if ((wolfSSL_write(ssl, msg, strlen(msg))) == msgSz) {
cipher = wolfSSL_get_current_cipher(ssl);
Serial.print("SSL cipher suite is "); Serial.print("SSL cipher suite is ");
Serial.println(wolfSSL_CIPHER_get_name(cipher)); Serial.println(cipherName);
if ((wolfSSL_write(ssl, msg, msgSz)) == msgSz) {
Serial.print("Server response: "); Serial.print("Server response: ");
while (client.available() || wolfSSL_pending(ssl)) { while (client.available() || wolfSSL_pending(ssl)) {
input = wolfSSL_read(ssl, reply, sizeof(reply) - 1); input = wolfSSL_read(ssl, reply, sizeof(reply) - 1);
total_input += input; total_input += input;
if ( input > 0 ) { if (input < 0) {
reply[input] = '\0';
Serial.print(reply);
} else if (input < 0) {
err = wolfSSL_get_error(ssl, 0); err = wolfSSL_get_error(ssl, 0);
wolfSSL_ERR_error_string(err, errBuf); wolfSSL_ERR_error_string(err, errBuf);
Serial.print("wolfSSL_read failed. Error: "); Serial.print("TLS Read Error: ");
Serial.println(errBuf); Serial.println(errBuf);
break;
} else if (input > 0) {
reply[input] = '\0';
Serial.print(reply);
} else { } else {
Serial.println(); Serial.println();
} }
} }
} else { } else {
Serial.println("SSL_write failed"); err = wolfSSL_get_error(ssl, 0);
wolfSSL_ERR_error_string(err, errBuf);
Serial.print("TLS Write Error: ");
Serial.println(errBuf);
} }
if (ssl != NULL) wolfSSL_shutdown(ssl);
wolfSSL_free(ssl); wolfSSL_free(ssl);
}
client.stop(); client.stop();
Serial.println("Connection complete."); Serial.println("Connection complete.");