Skip to content

Instantly share code, notes, and snippets.

@erikcorry
Created July 21, 2023 13:31
Show Gist options
  • Save erikcorry/f06bfb92b64b1ac8533ea761258cca5a to your computer and use it in GitHub Desktop.
Save erikcorry/f06bfb92b64b1ac8533ea761258cca5a to your computer and use it in GitHub Desktop.
Save session data between TLS connecitons.
Author: Kasper Lund <kasper@toit.io>
Date: Mon Jun 12 13:14:22 2023 +0200
Store TLS session data in RTC memory (#715)
diff --git a/src/service/brokers/http/connection.toit b/src/service/brokers/http/connection.toit
index 35ad787..e5b9888 100644
--- a/src/service/brokers/http/connection.toit
+++ b/src/service/brokers/http/connection.toit
@@ -6,9 +6,9 @@ import http
import net
import net.x509
import reader show Reader
+import system.storage
import ....shared.server_config show ServerConfigHttp
-
class HttpConnection_:
client_/http.Client? := ?
config_/ServerConfigHttp
@@ -17,8 +17,9 @@ class HttpConnection_:
if config_.root_certificate_ders:
root_certificates := config_.root_certificate_ders.map:
x509.Certificate.parse it
-
- client_ = http.Client.tls network --root_certificates=root_certificates
+ client_ = http.Client.tls network
+ --root_certificates=root_certificates
+ --security_store=HttpSecurityStore_
else:
client_ = http.Client network
@@ -60,3 +61,21 @@ class HttpConnection_:
block.call body
finally:
catch: response.drain
+
+class HttpSecurityStore_ extends http.SecurityStore:
+ // We store the cached session data in RTC memory. This means that
+ // it survives deep sleeps, but that any loss of power or firmware
+ // update will clear it.
+ static bucket ::= storage.Bucket.open --ram "toit.io/artemis/tls"
+
+ store_session_data host/string port/int data/ByteArray -> none:
+ bucket[key_ host port] = data
+
+ delete_session_data host/string port/int -> none:
+ bucket.remove (key_ host port)
+
+ retrieve_session_data host/string port/int -> ByteArray?:
+ return bucket.get (key_ host port)
+
+ key_ host/string port/int -> string:
+ return "$host:$port"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment