1use crate::ffi;
2use crate::libc_types::c_int;
3use foreign_types::ForeignType;
4
5use crate::error::ErrorStack;
6use crate::hpke::HpkeKey;
7use crate::{cvt_0i, cvt_p};
8
9pub struct SslEchKeysBuilder {
10 keys: SslEchKeys,
11}
12
13impl SslEchKeysBuilder {
14 pub fn new() -> Result<SslEchKeysBuilder, ErrorStack> {
15 unsafe {
16 ffi::init();
17 let keys = cvt_p(ffi::SSL_ECH_KEYS_new())?;
18
19 Ok(SslEchKeysBuilder::from_ptr(keys))
20 }
21 }
22
23 pub unsafe fn from_ptr(keys: *mut ffi::SSL_ECH_KEYS) -> Self {
24 Self {
25 keys: SslEchKeys::from_ptr(keys),
26 }
27 }
28
29 pub fn add_key(
30 &mut self,
31 is_retry_config: bool,
32 ech_config: &[u8],
33 key: HpkeKey,
34 ) -> Result<(), ErrorStack> {
35 unsafe {
36 cvt_0i(ffi::SSL_ECH_KEYS_add(
37 self.keys.as_ptr(),
38 c_int::from(is_retry_config),
39 ech_config.as_ptr(),
40 ech_config.len(),
41 key.as_ptr(),
42 ))
43 .map(|_| ())
44 }
45 }
46
47 pub fn build(self) -> SslEchKeys {
48 self.keys
49 }
50}
51
52foreign_type_and_impl_send_sync! {
53 type CType = ffi::SSL_ECH_KEYS;
54 fn drop = ffi::SSL_ECH_KEYS_free;
55
56 pub struct SslEchKeys;
57}
58
59impl SslEchKeys {
60 pub fn builder() -> Result<SslEchKeysBuilder, ErrorStack> {
61 SslEchKeysBuilder::new()
62 }
63}