-
Notifications
You must be signed in to change notification settings - Fork 179
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Abstraction for synchronous esp-tls #288
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a great idea!
I've left a bunch of (mostly stylistic / code reshuffling) comments, but the approach is solid.
Maybe the one important topic is whether it would be possible to also expose an API, that "adopts" an already existing socket, and does the TLS negotiation on top of it. But we can survive without this initially, I believe.
For the naming - yes, that's fine. As for
|
One more thing: you need to replace Long term this means most folks would depend only on |
* Allow splitting into EspTlsRead and EspTlsWrite * Don't use std * Use str and do alloc-free conversion
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think there's one thing remaining, most of the tls module should be guarded by a cfg, but we can't do it for the full module because X509 is exposed unconditionally.
I could split EspTls
etc into a separate module and then reexport it into tls
, what do you think?
src/tls.rs
Outdated
pub client_cert: Option<X509<'a>>, | ||
pub client_key: Option<X509<'a>>, | ||
pub client_key_password: Option<&'a str>, | ||
pub non_block: bool, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If this is true
, should we call conn_new_async
instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not necessarily, but VERY IMPORTANT: I was looking at the code and I think I just figured out a way for us to pass an already opened socket!
I think we still should keep the existing new
, but we should introduce (maybe post-merge) a new STD-only method along the lines of
#[cfg(feature = "std")]
pub fn wrap(hostname: &str/*We still need this as the hostname is part of the SSL validation*/, socket: std::net::TcpSocket, cfg: &Config) -> Result<EspError, Self> {
let tls = esp!(unsafe { esp_tls_init(); })?;
esp!(unsafe { esp_tls_set_conn_state(tls, esp_tls_conn_state_t_ESP_TLS_CONNECTING) })?;
esp!(unsafe { esp_tls_set_conn_sockfd(tls, socket.fd)? })?;
esp!(unsafe { esp_tls_conn_new_sync(<hostname_converted_to_char*>, <hostname_len>, 0, cfg, tls) })?;
Ok(...)
}
The above ^^^ will not try to open a new socket and will re-use ours, as the state of the connection is esp_tls_conn_state_t_ESP_TLS_CONNECTING
.
The above approach is important, because it might be a way to implement async
as well. Again, to do async
in a way which is independent of the concrete async Reactor, we needed a way to pass-in an already opened socket handle, and the above might be just this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To not make the above STD-only, we can even pass to the method a raw file descriptor, where the assumption is that the file descriptor is a socket descriptor, and also it is already opened.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good, I can give it a try in a follow-up PR.
@torkleyy Build fails because you are still using aliases from the |
Yeah, just don't create extra files. Surround your code with [cfg("esp_idf_comp_esp_tls_enabled")]
mod esptls {
...
} ... and then at the top, do [cfg("esp_idf_comp_esp_tls_enabled")]
pub use esptls::*; |
Build is looking good, finally 😅 |
@torkleyy JFYI I'll be pushing changes to the API in the next hours. Two reasons:
|
@ivmarkov Alright, thanks for letting me know! |
I think this should be pretty much good as-is, these TODOs remain:
crt_bundle_attach
Figure out how to map(not implemented by this PR)ds_data
#[cfg(esp_idf_comp_esp_tls_enabled)]
non-block
configLet me know if this PR is headed into the right direction.