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
#![allow(non_camel_case_types)]
#![allow(clippy::unreadable_literal)]
use enum_primitive::{enum_from_primitive, enum_from_primitive_impl, enum_from_primitive_impl_ty};
enum_from_primitive! {
#[derive(Clone, Copy, Debug, PartialEq)]
#[repr(u8)]
pub enum TlsCipherKx {
Null = 0,
Psk,
Krb5,
Srp,
Rsa,
Dh,
Dhe,
Ecdh,
Ecdhe,
Aecdh,
Eccpwd,
Tls13,
}
}
enum_from_primitive! {
#[derive(Clone, Copy, Debug, PartialEq)]
#[repr(u8)]
pub enum TlsCipherAu {
Null = 0,
Psk,
Krb5,
Srp,
Srp_Dss,
Srp_Rsa,
Dss,
Rsa,
Dhe,
Ecdsa,
Eccpwd,
Tls13,
}
}
enum_from_primitive! {
#[derive(Clone, Copy, Debug, PartialEq)]
#[repr(u8)]
pub enum TlsCipherEnc {
Null,
Des,
TripleDes,
Rc2,
Rc4,
Aria,
Idea,
Seed,
Aes,
Camellia,
Chacha20_Poly1305,
Sm4,
}
}
enum_from_primitive! {
#[derive(Clone, Copy, Debug, PartialEq)]
#[repr(u8)]
pub enum TlsCipherEncMode {
Null,
Cbc,
Ccm,
Gcm,
}
}
enum_from_primitive! {
#[derive(Clone, Copy, Debug, PartialEq)]
#[repr(u8)]
pub enum TlsCipherMac {
Null,
HmacMd5,
HmacSha1,
HmacSha256,
HmacSha384,
Aead,
}
}
#[derive(Debug)]
pub struct TlsCipherSuite {
pub name: &'static str,
pub id: u16,
pub kx: TlsCipherKx,
pub au: TlsCipherAu,
pub enc: TlsCipherEnc,
pub enc_mode: TlsCipherEncMode,
pub enc_size: u16,
pub mac: TlsCipherMac,
pub mac_size: u16,
}
include!(concat!(env!("OUT_DIR"), "/codegen.rs"));
impl TlsCipherSuite {
pub fn from_id(id: u16) -> Option<&'static TlsCipherSuite> {
CIPHERS.get(&id)
}
pub fn from_name<'a>(name: &'a str) -> Option<&'static TlsCipherSuite> {
CIPHERS.values().find(|&v| v.name == name)
}
}
#[cfg(test)]
mod tests {
use crate::tls_ciphers::{TlsCipherKx, TlsCipherSuite, CIPHERS};
#[test]
fn test_cipher_count() {
println!("loaded: {} cipher suites", CIPHERS.len());
assert!(!CIPHERS.is_empty());
}
#[test]
fn test_cipher_from_id() {
let cipher = TlsCipherSuite::from_id(0xc025).expect("could not get cipher");
println!("Found cipher: {:?}", cipher);
}
#[test]
fn test_cipher_from_name() {
let cipher = TlsCipherSuite::from_name("TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384")
.expect("could not get cipher");
println!("Found cipher: {:?}", cipher);
}
#[test]
fn test_cipher_filter() {
let ecdhe_ciphers: Vec<&TlsCipherSuite> = CIPHERS
.values()
.filter(|c| c.kx == TlsCipherKx::Ecdhe)
.collect();
assert!(ecdhe_ciphers.len() > 20);
}
}