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
use std::{
io::{Error as IoError, ErrorKind},
path::Path,
};
use async_trait::async_trait;
use ring::digest::{Context, SHA256};
use tokio::fs::{create_dir_all, read, OpenOptions};
use tokio::io::AsyncWriteExt;
pub trait CacheError: std::error::Error + Send + Sync + 'static {}
impl<T> CacheError for T where T: std::error::Error + Send + Sync + 'static {}
#[async_trait]
pub trait AcmeCache {
type Error: CacheError;
async fn read_pkey(&self, directory_name: &str, domains: &[String]) -> Result<Option<Vec<u8>>, Self::Error>;
async fn write_pkey(&self, directory_name: &str, domains: &[String], data: &[u8]) -> Result<(), Self::Error>;
async fn read_cert(&self, directory_name: &str, domains: &[String]) -> Result<Option<Vec<u8>>, Self::Error>;
async fn write_cert(&self, directory_name: &str, domains: &[String], data: &[u8]) -> Result<(), Self::Error>;
}
static PKEY_PEM_PREFIX: &str = "pkey-";
static CERT_PEM_PREFIX: &str = "cert-";
#[async_trait]
impl<P> AcmeCache for P
where
P: AsRef<Path> + Send + Sync,
{
type Error = IoError;
async fn read_pkey(&self, directory_name: &str, domains: &[String]) -> Result<Option<Vec<u8>>, Self::Error> {
let mut path = self.as_ref().to_path_buf();
path.push(format!(
"{}{}-{}",
PKEY_PEM_PREFIX,
directory_name,
file_hash_part(domains)
));
match read(path).await {
Ok(data) => Ok(Some(data)),
Err(err) => match err.kind() {
ErrorKind::NotFound => Ok(None),
_ => Err(err),
},
}
}
async fn write_pkey(&self, directory_name: &str, domains: &[String], data: &[u8]) -> Result<(), Self::Error> {
let mut path = self.as_ref().to_path_buf();
create_dir_all(&path).await?;
path.push(format!(
"{}{}-{}",
PKEY_PEM_PREFIX,
directory_name,
file_hash_part(domains)
));
Ok(write_data(path, data).await?)
}
async fn read_cert(&self, directory_name: &str, domains: &[String]) -> Result<Option<Vec<u8>>, Self::Error> {
let mut path = self.as_ref().to_path_buf();
path.push(format!(
"{}{}-{}",
CERT_PEM_PREFIX,
directory_name,
file_hash_part(domains)
));
match read(path).await {
Ok(data) => Ok(Some(data)),
Err(err) => match err.kind() {
ErrorKind::NotFound => Ok(None),
_ => Err(err),
},
}
}
async fn write_cert(&self, directory_name: &str, domains: &[String], data: &[u8]) -> Result<(), Self::Error> {
let mut path = self.as_ref().to_path_buf();
create_dir_all(&path).await?;
path.push(format!(
"{}{}-{}",
CERT_PEM_PREFIX,
directory_name,
file_hash_part(domains)
));
Ok(write_data(path, data).await?)
}
}
async fn write_data(file_path: impl AsRef<Path>, data: impl AsRef<[u8]>) -> Result<(), IoError> {
let mut file = OpenOptions::new();
file.write(true).create(true).truncate(true);
#[cfg(unix)]
file.mode(0o600);
let mut buffer = file.open(file_path.as_ref()).await?;
buffer.write_all(data.as_ref()).await?;
Ok(())
}
fn file_hash_part(data: &[String]) -> String {
let mut ctx = Context::new(&SHA256);
for el in data {
ctx.update(el.as_ref());
ctx.update(&[0])
}
base64::encode_config(ctx.finish(), base64::URL_SAFE_NO_PAD)
}