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
use crate::msgs::enums::SignatureScheme;
use crate::sign;
use crate::key;
use crate::client;
use crate::error::TLSError;
use std::collections;
use std::sync::{Arc, Mutex};
pub struct NoClientSessionStorage {}
impl client::StoresClientSessions for NoClientSessionStorage {
fn put(&self, _key: Vec<u8>, _value: Vec<u8>) -> bool {
false
}
fn get(&self, _key: &[u8]) -> Option<Vec<u8>> {
None
}
}
pub struct ClientSessionMemoryCache {
cache: Mutex<collections::HashMap<Vec<u8>, Vec<u8>>>,
max_entries: usize,
}
impl ClientSessionMemoryCache {
pub fn new(size: usize) -> Arc<ClientSessionMemoryCache> {
debug_assert!(size > 0);
Arc::new(ClientSessionMemoryCache {
cache: Mutex::new(collections::HashMap::new()),
max_entries: size,
})
}
fn limit_size(&self) {
let mut cache = self.cache.lock().unwrap();
while cache.len() > self.max_entries {
let k = cache.keys().next().unwrap().clone();
cache.remove(&k);
}
}
}
impl client::StoresClientSessions for ClientSessionMemoryCache {
fn put(&self, key: Vec<u8>, value: Vec<u8>) -> bool {
self.cache.lock()
.unwrap()
.insert(key, value);
self.limit_size();
true
}
fn get(&self, key: &[u8]) -> Option<Vec<u8>> {
self.cache.lock()
.unwrap()
.get(key).cloned()
}
}
pub struct FailResolveClientCert {}
impl client::ResolvesClientCert for FailResolveClientCert {
fn resolve(&self,
_acceptable_issuers: &[&[u8]],
_sigschemes: &[SignatureScheme])
-> Option<sign::CertifiedKey> {
None
}
fn has_certs(&self) -> bool {
false
}
}
pub struct AlwaysResolvesClientCert(sign::CertifiedKey);
impl AlwaysResolvesClientCert {
pub fn new(chain: Vec<key::Certificate>,
priv_key: &key::PrivateKey) -> Result<AlwaysResolvesClientCert, TLSError> {
let key = sign::any_supported_type(priv_key)
.map_err(|_| TLSError::General("invalid private key".into()))?;
Ok(AlwaysResolvesClientCert(sign::CertifiedKey::new(chain, Arc::new(key))))
}
}
impl client::ResolvesClientCert for AlwaysResolvesClientCert {
fn resolve(&self,
_acceptable_issuers: &[&[u8]],
_sigschemes: &[SignatureScheme])
-> Option<sign::CertifiedKey> {
Some(self.0.clone())
}
fn has_certs(&self) -> bool {
true
}
}
#[cfg(test)]
mod test {
use super::*;
use crate::StoresClientSessions;
#[test]
fn test_noclientsessionstorage_drops_put() {
let c = NoClientSessionStorage {};
assert_eq!(c.put(vec![0x01], vec![0x02]), false);
}
#[test]
fn test_noclientsessionstorage_denies_gets() {
let c = NoClientSessionStorage {};
c.put(vec![0x01], vec![0x02]);
assert_eq!(c.get(&[]), None);
assert_eq!(c.get(&[0x01]), None);
assert_eq!(c.get(&[0x02]), None);
}
#[test]
fn test_clientsessionmemorycache_accepts_put() {
let c = ClientSessionMemoryCache::new(4);
assert_eq!(c.put(vec![0x01], vec![0x02]), true);
}
#[test]
fn test_clientsessionmemorycache_persists_put() {
let c = ClientSessionMemoryCache::new(4);
assert_eq!(c.put(vec![0x01], vec![0x02]), true);
assert_eq!(c.get(&[0x01]), Some(vec![0x02]));
assert_eq!(c.get(&[0x01]), Some(vec![0x02]));
}
#[test]
fn test_clientsessionmemorycache_overwrites_put() {
let c = ClientSessionMemoryCache::new(4);
assert_eq!(c.put(vec![0x01], vec![0x02]), true);
assert_eq!(c.put(vec![0x01], vec![0x04]), true);
assert_eq!(c.get(&[0x01]), Some(vec![0x04]));
}
#[test]
fn test_clientsessionmemorycache_drops_to_maintain_size_invariant() {
let c = ClientSessionMemoryCache::new(4);
assert_eq!(c.put(vec![0x01], vec![0x02]), true);
assert_eq!(c.put(vec![0x03], vec![0x04]), true);
assert_eq!(c.put(vec![0x05], vec![0x06]), true);
assert_eq!(c.put(vec![0x07], vec![0x08]), true);
assert_eq!(c.put(vec![0x09], vec![0x0a]), true);
let mut count = 0;
if c.get(&[0x01]).is_some() { count += 1; }
if c.get(&[0x03]).is_some() { count += 1; }
if c.get(&[0x05]).is_some() { count += 1; }
if c.get(&[0x07]).is_some() { count += 1; }
if c.get(&[0x09]).is_some() { count += 1; }
assert_eq!(count, 4);
}
}