/rust/registry/src/index.crates.io-1949cf8c6b5b557f/tls-parser-0.11.0/src/tls_ec.rs
Line | Count | Source |
1 | | use alloc::vec::Vec; |
2 | | use nom::error::{make_error, ErrorKind}; |
3 | | use nom::multi::length_data; |
4 | | use nom::number::streaming::be_u8; |
5 | | use nom::{Err, IResult}; |
6 | | use nom_derive::*; |
7 | | use rusticata_macros::newtype_enum; |
8 | | |
9 | | /// Named curves, as defined in [RFC4492](https://tools.ietf.org/html/rfc4492), [RFC7027](https://tools.ietf.org/html/rfc7027), [RFC7919](https://tools.ietf.org/html/rfc7919) and |
10 | | /// [IANA Supported Groups |
11 | | /// Registry](https://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml#tls-parameters-8) |
12 | 594 | #[derive(Clone, Copy, PartialEq, Eq, NomBE)] <tls_parser::tls_ec::NamedGroup as nom_derive::traits::Parse<&[u8]>>::parse_be Line | Count | Source | 12 | 268 | #[derive(Clone, Copy, PartialEq, Eq, NomBE)] |
Unexecuted instantiation: <tls_parser::tls_ec::NamedGroup as nom_derive::traits::Parse<&[u8]>>::parse_le <tls_parser::tls_ec::NamedGroup as nom_derive::traits::Parse<&[u8]>>::parse_be Line | Count | Source | 12 | 326 | #[derive(Clone, Copy, PartialEq, Eq, NomBE)] |
Unexecuted instantiation: <tls_parser::tls_ec::NamedGroup as nom_derive::traits::Parse<&[u8]>>::parse_le |
13 | | pub struct NamedGroup(pub u16); |
14 | | |
15 | | newtype_enum! { |
16 | | impl debug NamedGroup { |
17 | | Sect163k1 = 1, |
18 | | Sect163r1 = 2, |
19 | | Sect163r2 = 3, |
20 | | Sect193r1 = 4, |
21 | | Sect193r2 = 5, |
22 | | Sect233k1 = 6, |
23 | | Sect233r1 = 7, |
24 | | Sect239k1 = 8, |
25 | | Sect283k1 = 9, |
26 | | Sect283r1 = 10, |
27 | | Sect409k1 = 11, |
28 | | Sect409r1 = 12, |
29 | | Sect571k1 = 13, |
30 | | Sect571r1 = 14, |
31 | | Secp160k1 = 15, |
32 | | Secp160r1 = 16, |
33 | | Secp160r2 = 17, |
34 | | Secp192k1 = 18, |
35 | | Secp192r1 = 19, |
36 | | Secp224k1 = 20, |
37 | | Secp224r1 = 21, |
38 | | Secp256k1 = 22, |
39 | | Secp256r1 = 23, |
40 | | Secp384r1 = 24, |
41 | | Secp521r1 = 25, |
42 | | BrainpoolP256r1 = 26, |
43 | | BrainpoolP384r1 = 27, |
44 | | BrainpoolP512r1 = 28, |
45 | | EcdhX25519 = 29, |
46 | | EcdhX448 = 30, |
47 | | BrainpoolP256r1tls13 = 31, |
48 | | BrainpoolP384r1tls13 = 32, |
49 | | BrainpoolP512r1tls13 = 33, |
50 | | Sm2 = 41, |
51 | | Ffdhe2048 = 0x100, |
52 | | Ffdhe3072 = 0x101, |
53 | | Ffdhe4096 = 0x102, |
54 | | Ffdhe6144 = 0x103, |
55 | | Ffdhe8192 = 0x104, |
56 | | ArbitraryExplicitPrimeCurves = 0xFF01, |
57 | | ArbitraryExplicitChar2Curves = 0xFF02, |
58 | | } |
59 | | } |
60 | | |
61 | | impl NamedGroup { |
62 | | /// Return key size of curve in bits, or None if unknown |
63 | 0 | pub fn key_bits(self: NamedGroup) -> Option<u16> { |
64 | 0 | match self { |
65 | 0 | NamedGroup::Sect163k1 => Some(163), |
66 | 0 | NamedGroup::Sect163r1 => Some(163), |
67 | 0 | NamedGroup::Sect163r2 => Some(163), |
68 | 0 | NamedGroup::Sect193r1 => Some(193), |
69 | 0 | NamedGroup::Sect193r2 => Some(193), |
70 | 0 | NamedGroup::Sect233k1 => Some(233), |
71 | 0 | NamedGroup::Sect233r1 => Some(233), |
72 | 0 | NamedGroup::Sect239k1 => Some(239), |
73 | 0 | NamedGroup::Sect283k1 => Some(283), |
74 | 0 | NamedGroup::Sect283r1 => Some(283), |
75 | 0 | NamedGroup::Sect409k1 => Some(409), |
76 | 0 | NamedGroup::Sect409r1 => Some(409), |
77 | 0 | NamedGroup::Sect571k1 => Some(571), |
78 | 0 | NamedGroup::Sect571r1 => Some(571), |
79 | 0 | NamedGroup::Secp160k1 => Some(160), |
80 | 0 | NamedGroup::Secp160r1 => Some(160), |
81 | 0 | NamedGroup::Secp160r2 => Some(160), |
82 | 0 | NamedGroup::Secp192k1 => Some(192), |
83 | 0 | NamedGroup::Secp192r1 => Some(192), |
84 | 0 | NamedGroup::Secp224k1 => Some(224), |
85 | 0 | NamedGroup::Secp224r1 => Some(224), |
86 | 0 | NamedGroup::Secp256k1 => Some(256), |
87 | 0 | NamedGroup::Secp256r1 => Some(256), |
88 | 0 | NamedGroup::Secp384r1 => Some(384), |
89 | 0 | NamedGroup::Secp521r1 => Some(521), |
90 | 0 | NamedGroup::BrainpoolP256r1 => Some(256), |
91 | 0 | NamedGroup::BrainpoolP384r1 => Some(384), |
92 | 0 | NamedGroup::BrainpoolP512r1 => Some(521), |
93 | 0 | NamedGroup::EcdhX25519 => Some(253), |
94 | 0 | _ => None, |
95 | | } |
96 | 0 | } Unexecuted instantiation: <tls_parser::tls_ec::NamedGroup>::key_bits Unexecuted instantiation: <tls_parser::tls_ec::NamedGroup>::key_bits |
97 | | } |
98 | | |
99 | | /// Elliptic curve |
100 | | /// |
101 | | /// a and b specify the coefficients of the curve |
102 | 0 | #[derive(Debug, PartialEq, NomBE)] Unexecuted instantiation: <tls_parser::tls_ec::ECCurve as nom_derive::traits::Parse<&[u8]>>::parse_be Unexecuted instantiation: <tls_parser::tls_ec::ECCurve as nom_derive::traits::Parse<&[u8]>>::parse_le Unexecuted instantiation: <tls_parser::tls_ec::ECCurve as nom_derive::traits::Parse<&[u8]>>::parse_be Unexecuted instantiation: <tls_parser::tls_ec::ECCurve as nom_derive::traits::Parse<&[u8]>>::parse_le |
103 | | pub struct ECCurve<'a> { |
104 | | #[nom(Parse = "length_data(be_u8)")] |
105 | | pub a: &'a [u8], |
106 | | #[nom(Parse = "length_data(be_u8)")] |
107 | | pub b: &'a [u8], |
108 | | } |
109 | | |
110 | | /// Elliptic curve types, as defined in the |
111 | | /// [IANA EC Curve Type Registry |
112 | | /// Registry](https://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml#tls-parameters-10) |
113 | 0 | #[derive(Clone, Copy, PartialEq, Eq, NomBE)] Unexecuted instantiation: <tls_parser::tls_ec::ECCurveType as nom_derive::traits::Parse<&[u8]>>::parse_be Unexecuted instantiation: <tls_parser::tls_ec::ECCurveType as nom_derive::traits::Parse<&[u8]>>::parse_le Unexecuted instantiation: <tls_parser::tls_ec::ECCurveType as nom_derive::traits::Parse<&[u8]>>::parse_be Unexecuted instantiation: <tls_parser::tls_ec::ECCurveType as nom_derive::traits::Parse<&[u8]>>::parse_le |
114 | | pub struct ECCurveType(pub u8); |
115 | | |
116 | | newtype_enum! { |
117 | | impl display ECCurveType { |
118 | | ExplicitPrime = 1, |
119 | | ExplicitChar2 = 2, |
120 | | NamedGroup = 3, |
121 | | } |
122 | | } |
123 | | |
124 | | /// EC Point |
125 | 0 | #[derive(Clone, Debug, PartialEq, NomBE)] Unexecuted instantiation: <tls_parser::tls_ec::ECPoint as nom_derive::traits::Parse<&[u8]>>::parse_be Unexecuted instantiation: <tls_parser::tls_ec::ECPoint as nom_derive::traits::Parse<&[u8]>>::parse_le Unexecuted instantiation: <tls_parser::tls_ec::ECPoint as nom_derive::traits::Parse<&[u8]>>::parse_be Unexecuted instantiation: <tls_parser::tls_ec::ECPoint as nom_derive::traits::Parse<&[u8]>>::parse_le |
126 | | pub struct ECPoint<'a> { |
127 | | #[nom(Parse = "length_data(be_u8)")] |
128 | | pub point: &'a [u8], |
129 | | } |
130 | | |
131 | | /// Elliptic curve parameters, conveyed verbosely as a prime field, as |
132 | | /// defined in [RFC4492](https://tools.ietf.org/html/rfc4492) section 5.4 |
133 | 0 | #[derive(Debug, PartialEq, NomBE)] Unexecuted instantiation: <tls_parser::tls_ec::ExplicitPrimeContent as nom_derive::traits::Parse<&[u8]>>::parse_be Unexecuted instantiation: <tls_parser::tls_ec::ExplicitPrimeContent as nom_derive::traits::Parse<&[u8]>>::parse_le Unexecuted instantiation: <tls_parser::tls_ec::ExplicitPrimeContent as nom_derive::traits::Parse<&[u8]>>::parse_be Unexecuted instantiation: <tls_parser::tls_ec::ExplicitPrimeContent as nom_derive::traits::Parse<&[u8]>>::parse_le |
134 | | pub struct ExplicitPrimeContent<'a> { |
135 | | #[nom(Parse = "length_data(be_u8)")] |
136 | | pub prime_p: &'a [u8], |
137 | | pub curve: ECCurve<'a>, |
138 | | pub base: ECPoint<'a>, |
139 | | #[nom(Parse = "length_data(be_u8)")] |
140 | | pub order: &'a [u8], |
141 | | #[nom(Parse = "length_data(be_u8)")] |
142 | | pub cofactor: &'a [u8], |
143 | | } |
144 | | |
145 | | /// Elliptic curve parameters content (depending on EC type) |
146 | 0 | #[derive(PartialEq, Nom)] Unexecuted instantiation: <tls_parser::tls_ec::ECParametersContent>::parse_be Unexecuted instantiation: <tls_parser::tls_ec::ECParametersContent>::parse_le Unexecuted instantiation: <tls_parser::tls_ec::ECParametersContent>::parse_be Unexecuted instantiation: <tls_parser::tls_ec::ECParametersContent>::parse_le |
147 | | #[nom(Selector = "ECCurveType")] |
148 | | pub enum ECParametersContent<'a> { |
149 | | #[nom(Selector = "ECCurveType::ExplicitPrime")] |
150 | | ExplicitPrime(ExplicitPrimeContent<'a>), |
151 | | // TODO ExplicitChar2 is defined in [RFC4492] section 5.4 |
152 | | // #[nom(Selector="ECCurveType::ExplicitChar2")] |
153 | | // ExplicitChar2(&'a [u8]), |
154 | | #[nom(Selector = "ECCurveType::NamedGroup")] |
155 | | NamedGroup(NamedGroup), |
156 | | } |
157 | | |
158 | | /// Elliptic curve parameters, |
159 | | /// defined in [RFC4492](https://tools.ietf.org/html/rfc4492) section 5.4 |
160 | 0 | #[derive(PartialEq, NomBE)] Unexecuted instantiation: <tls_parser::tls_ec::ECParameters as nom_derive::traits::Parse<&[u8]>>::parse_be Unexecuted instantiation: <tls_parser::tls_ec::ECParameters as nom_derive::traits::Parse<&[u8]>>::parse_le Unexecuted instantiation: <tls_parser::tls_ec::ECParameters as nom_derive::traits::Parse<&[u8]>>::parse_be Unexecuted instantiation: <tls_parser::tls_ec::ECParameters as nom_derive::traits::Parse<&[u8]>>::parse_le |
161 | | pub struct ECParameters<'a> { |
162 | | /// Should match a [ECCurveType](enum.ECCurveType.html) value |
163 | | pub curve_type: ECCurveType, |
164 | | #[nom(Parse = "{|i| ECParametersContent::parse(i, curve_type)}")] |
165 | | pub params_content: ECParametersContent<'a>, |
166 | | } |
167 | | |
168 | | /// ECDH parameters |
169 | | /// defined in [RFC4492](https://tools.ietf.org/html/rfc4492) section 5.4 |
170 | 0 | #[derive(Debug, PartialEq, NomBE)] Unexecuted instantiation: <tls_parser::tls_ec::ServerECDHParams as nom_derive::traits::Parse<&[u8]>>::parse_be Unexecuted instantiation: <tls_parser::tls_ec::ServerECDHParams as nom_derive::traits::Parse<&[u8]>>::parse_le Unexecuted instantiation: <tls_parser::tls_ec::ServerECDHParams as nom_derive::traits::Parse<&[u8]>>::parse_be Unexecuted instantiation: <tls_parser::tls_ec::ServerECDHParams as nom_derive::traits::Parse<&[u8]>>::parse_le |
171 | | pub struct ServerECDHParams<'a> { |
172 | | pub curve_params: ECParameters<'a>, |
173 | | pub public: ECPoint<'a>, |
174 | | } |
175 | | |
176 | | /// Parse the entire input as a list of named groups (curves) |
177 | 5.31k | pub fn parse_named_groups(i: &[u8]) -> IResult<&[u8], Vec<NamedGroup>> { |
178 | 5.31k | let len = i.len(); |
179 | 5.31k | if len == 0 { |
180 | 862 | return Ok((i, Vec::new())); |
181 | 4.44k | } |
182 | 4.44k | if len % 2 == 1 || len > i.len() { |
183 | 394 | return Err(Err::Error(make_error(i, ErrorKind::LengthValue))); |
184 | 4.05k | } |
185 | 4.05k | let v = (&i[..len]) |
186 | 4.05k | .chunks(2) |
187 | 1.01M | .map(|chunk| NamedGroup((chunk[0] as u16) << 8 | chunk[1] as u16)) tls_parser::tls_ec::parse_named_groups::{closure#0}Line | Count | Source | 187 | 602k | .map(|chunk| NamedGroup((chunk[0] as u16) << 8 | chunk[1] as u16)) |
tls_parser::tls_ec::parse_named_groups::{closure#0}Line | Count | Source | 187 | 415k | .map(|chunk| NamedGroup((chunk[0] as u16) << 8 | chunk[1] as u16)) |
|
188 | 4.05k | .collect(); |
189 | 4.05k | Ok((&i[len..], v)) |
190 | 5.31k | } tls_parser::tls_ec::parse_named_groups Line | Count | Source | 177 | 3.12k | pub fn parse_named_groups(i: &[u8]) -> IResult<&[u8], Vec<NamedGroup>> { | 178 | 3.12k | let len = i.len(); | 179 | 3.12k | if len == 0 { | 180 | 526 | return Ok((i, Vec::new())); | 181 | 2.59k | } | 182 | 2.59k | if len % 2 == 1 || len > i.len() { | 183 | 197 | return Err(Err::Error(make_error(i, ErrorKind::LengthValue))); | 184 | 2.39k | } | 185 | 2.39k | let v = (&i[..len]) | 186 | 2.39k | .chunks(2) | 187 | 2.39k | .map(|chunk| NamedGroup((chunk[0] as u16) << 8 | chunk[1] as u16)) | 188 | 2.39k | .collect(); | 189 | 2.39k | Ok((&i[len..], v)) | 190 | 3.12k | } |
tls_parser::tls_ec::parse_named_groups Line | Count | Source | 177 | 2.18k | pub fn parse_named_groups(i: &[u8]) -> IResult<&[u8], Vec<NamedGroup>> { | 178 | 2.18k | let len = i.len(); | 179 | 2.18k | if len == 0 { | 180 | 336 | return Ok((i, Vec::new())); | 181 | 1.85k | } | 182 | 1.85k | if len % 2 == 1 || len > i.len() { | 183 | 197 | return Err(Err::Error(make_error(i, ErrorKind::LengthValue))); | 184 | 1.65k | } | 185 | 1.65k | let v = (&i[..len]) | 186 | 1.65k | .chunks(2) | 187 | 1.65k | .map(|chunk| NamedGroup((chunk[0] as u16) << 8 | chunk[1] as u16)) | 188 | 1.65k | .collect(); | 189 | 1.65k | Ok((&i[len..], v)) | 190 | 2.18k | } |
|
191 | | |
192 | | #[inline] |
193 | 0 | pub fn parse_ec_parameters(i: &[u8]) -> IResult<&[u8], ECParameters> { |
194 | 0 | ECParameters::parse(i) |
195 | 0 | } Unexecuted instantiation: tls_parser::tls_ec::parse_ec_parameters Unexecuted instantiation: tls_parser::tls_ec::parse_ec_parameters |
196 | | |
197 | | #[inline] |
198 | 0 | pub fn parse_ecdh_params(i: &[u8]) -> IResult<&[u8], ServerECDHParams> { |
199 | 0 | ServerECDHParams::parse(i) |
200 | 0 | } Unexecuted instantiation: tls_parser::tls_ec::parse_ecdh_params Unexecuted instantiation: tls_parser::tls_ec::parse_ecdh_params |