etsi014_client/
lib.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
extern crate core;

mod c;
mod error;
mod json;
mod status;
mod utils;

pub use error::Error;
pub use etsi014_client::ETSI014Client;
pub use secrets::SecretVec;
pub use status::Status;

pub mod etsi014_client {
    use crate::error::ErrorType::{
        ConnectionError, InvalidArgument, InvalidHost, InvalidResponse,
    };
    use crate::json::key_container::KeyContainer;
    use crate::json::key_id::KeyId;
    use crate::json::key_request::KeyRequest;
    use crate::json::keys_by_ids_request::KeysByIdsRequest;
    use crate::json::status_response::StatusResponse;
    use crate::status::Status;
    use crate::utils::read_file;
    use crate::Error;
    use base64ct::{Base64, Encoding};
    use reqwest::header::CONTENT_TYPE;
    use reqwest::{Client, Identity, Url};
    pub use secrets::Secret;
    pub use secrets::SecretBox;
    pub use secrets::SecretVec;
    use serde::de;
    use std::path::PathBuf;

    #[derive(Debug)]
    pub struct ETSI014Client {
        http_client: Client,
        base_url: Url,
    }

    impl ETSI014Client {
        const PATH_PREFIX: &'static str = "api/v1/keys";

        pub fn new(
            host: &str,
            port: u16,
            cert_path: &PathBuf,
            key_path: &PathBuf,
            server_ca_path: &PathBuf,
        ) -> Result<Self, Error> {
            // Can not set host and port without parsing something first
            let mut base_url =
                Url::parse("https://localhost").expect("Error parsing hardcoded URL");
            base_url
                .set_scheme("https")
                .expect("Error setting https as scheme");
            base_url.set_host(Some(host)).map_err(|e| {
                Error::new(
                    format!("Invalid host: {host}"),
                    InvalidHost,
                    Some(Box::new(e)),
                )
            })?;
            base_url
                .set_port(Some(port))
                // Might fail if host invalid
                .map_err(|_| {
                    Error::new(
                        format!("Error setting port for host: '{host}"),
                        InvalidHost,
                        None,
                    )
                })?;
            let server_ca = reqwest::Certificate::from_pem(&read_file(server_ca_path)?)
                .map_err(|e| {
                Error::new(
                    format!("Error parsing {server_ca_path:?}"),
                    InvalidArgument,
                    Some(Box::new(e)),
                )
            })?;
            let identity =
                Identity::from_pkcs8_pem(&read_file(cert_path)?, &read_file(key_path)?)
                    .map_err(|e| {
                    Error::new(
                        format!("Error parsing {cert_path:?} or {key_path:?}"),
                        InvalidArgument,
                        Some(Box::new(e)),
                    )
                })?;
            let http_client = Client::builder()
                .use_native_tls()
                .tls_built_in_root_certs(false)
                .add_root_certificate(server_ca)
                .identity(identity)
                .build()
                .map_err(|e| {
                    Error::new(
                        "Error creating http client".to_string(),
                        InvalidArgument,
                        Some(Box::new(e)),
                    )
                })?;
            Ok(ETSI014Client {
                http_client,
                base_url,
            })
        }

        async fn send_request<T>(
            &self,
            target_sae_id: &str,
            endpoint: &str,
            body: Option<&str>,
        ) -> Result<T, Error>
        where
            T: de::DeserializeOwned,
        {
            let mut url = self.base_url.clone();
            let path_prefix = Self::PATH_PREFIX;
            url.set_path(&format!("{path_prefix}/{target_sae_id}/{endpoint}"));
            let request = match body {
                None => self
                    .http_client
                    .get(url.clone())
                    .build()
                    .map_err(|e| Error::new(
                        format!("Error building request for url: {url}"),
                        InvalidArgument,
                        Some(Box::new(e)),
                    )),
                Some(body) =>
                    self.http_client
                        .post(url.clone())
                        .header(CONTENT_TYPE, "application/json")
                        .body(body.to_owned())
                        .build()
                        .map_err(|e| Error::new(
                            format!("Error building request for url: {url}\n\nRequest body: {body}"),
                            InvalidArgument,
                            Some(Box::new(e))
                        )),
            }?;

            let response = self
                .http_client
                .execute(request.try_clone().unwrap())
                .await
                .map_err(|e| {
                    Error::new(
                        format!("Error sending request: {request:#?}"),
                        ConnectionError,
                        Some(Box::new(e)),
                    )
                })?;
            let http_code = response.status();
            let response_string = response.text().await.map_err(|e| {
                Error::new(
                    "Response not UTF-8".to_string(),
                    InvalidResponse,
                    Some(Box::new(e)),
                )
            })?;
            let error_info = |s: String| {
                let body_info = match body {
                    None => "".to_owned(),
                    Some(body) => format!("\nUsing POST body: {body}"),
                };
                format!(
                    "{s}\n\n\
                         HTTP Code: {http_code}\n\
                         Response:\n{response_string}\n\
                         Using request: {request:#?}{body_info}"
                )
            };
            if !http_code.is_success() {
                return Err(Error::new(
                    error_info("Unsuccessful HTTP code".to_string()),
                    InvalidResponse,
                    None,
                ));
            }
            serde_json::from_str::<T>(&response_string).map_err(|e| {
                Error::new(
                    error_info("Unable to deserialize JSON from response.".to_string()),
                    InvalidResponse,
                    Some(Box::new(e)),
                )
            })
        }

        pub async fn get_status(&self, target_sae_id: &str) -> Result<Status, Error> {
            let sr: StatusResponse =
                self.send_request(target_sae_id, "status", None).await?;
            Ok(Status {
                source_kme_id: sr.source_kme_id,
                target_kme_id: sr.target_kme_id,
                source_sae_id: sr.source_sae_id,
                target_sae_id: sr.target_sae_id,
                key_size: sr.key_size,
                stored_key_count: sr.stored_key_count,
                max_key_count: sr.max_key_count,
                max_key_per_request: sr.max_key_per_request,
                max_key_size: sr.max_key_size,
                min_key_size: sr.min_key_size,
                max_sae_id_count: sr.max_sae_id_count,
            })
        }

        fn key_container_to_vector(
            kc: KeyContainer,
        ) -> Result<Vec<(String, SecretVec<u8>)>, Error> {
            let amount_of_keys = kc.keys.len();
            kc.keys.into_iter().try_fold(
                Vec::with_capacity(amount_of_keys),
                |mut l, key_and_id| {
                    let uuid = &key_and_id.key_id;
                    let base64_string = key_and_id.key;
                    let mut base64_vec = base64_string.into_bytes();
                    let base64_slice = base64_vec.as_mut();
                    let mut secret_base64 = SecretVec::from(base64_slice);
                    let mut secret_base64_ref_mut = secret_base64.borrow_mut();
                    let secret_slice = Base64::decode_in_place(
                        secret_base64_ref_mut.as_mut(),
                    )
                    .map_err(|_| {
                        Error::new(
                            format!("Error decoding base64 for uuid {uuid}"),
                            InvalidResponse,
                            None,
                        )
                    })?;
                    // Cannot resize SecretVec, so create a new shorter one.
                    let secret = SecretVec::new(secret_slice.len(), |sv| {
                        sv.copy_from_slice(secret_slice);
                    });
                    l.push((key_and_id.key_id, secret));
                    Ok(l)
                },
            )
        }

        pub async fn get_keys(
            &self,
            key_size_bits: u32,
            target_sae_id: &str,
            additional_target_sae_ids: &[&str],
            amount_of_keys: u32,
        ) -> Result<Vec<(String, SecretVec<u8>)>, Error> {
            let post_body = serde_json::to_string(&KeyRequest {
                number: amount_of_keys,
                size: Some(key_size_bits),
                additional_target_sae_ids,
                extension_mandatory: None,
            })
            .expect("Error serializing key request.");
            let key_container = self
                .send_request::<KeyContainer>(target_sae_id, "enc_keys", Some(&post_body))
                .await?;
            Self::key_container_to_vector(key_container)
        }

        pub async fn get_keys_by_ids(
            &self,
            target_sae_id: &str,
            key_ids: &[&str],
        ) -> Result<Vec<(String, SecretVec<u8>)>, Error> {
            let post_body = serde_json::to_string(&KeysByIdsRequest {
                key_ids: key_ids.iter().map(|key_id| KeyId { key_id }).collect(),
            })
            .expect("Error serializing keys by ids reqeust");
            let key_container = self
                .send_request::<KeyContainer>(target_sae_id, "dec_keys", Some(&post_body))
                .await?;
            Self::key_container_to_vector(key_container)
        }
    }
}