|
| 1 | +use byteorder::{BigEndian, ByteOrder}; |
| 2 | +use elliptic_curve::ecdh::{EphemeralSecret, SharedSecret}; |
| 3 | +use elliptic_curve::point::PointCompression; |
| 4 | +use elliptic_curve::sec1::{FromEncodedPoint, ModulusSize, ToEncodedPoint}; |
| 5 | +use elliptic_curve::{AffinePoint, Curve, CurveArithmetic, FieldBytesSize}; |
| 6 | +use log::debug; |
| 7 | +use p256::NistP256; |
| 8 | +use p384::NistP384; |
| 9 | +use p521::NistP521; |
| 10 | +use russh_cryptovec::CryptoVec; |
| 11 | +use russh_keys::encoding::Encoding; |
| 12 | + |
| 13 | +use crate::kex::{compute_keys, KexAlgorithm, KexType}; |
| 14 | +use crate::mac::{self}; |
| 15 | +use crate::session::Exchange; |
| 16 | +use crate::{cipher, msg}; |
| 17 | + |
| 18 | +pub struct EcdhNistP256KexType {} |
| 19 | + |
| 20 | +impl KexType for EcdhNistP256KexType { |
| 21 | + fn make(&self) -> Box<dyn KexAlgorithm + Send> { |
| 22 | + Box::new(EcdhNistPKex::<NistP256> { |
| 23 | + local_secret: None, |
| 24 | + shared_secret: None, |
| 25 | + }) as Box<dyn KexAlgorithm + Send> |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +pub struct EcdhNistP384KexType {} |
| 30 | + |
| 31 | +impl KexType for EcdhNistP384KexType { |
| 32 | + fn make(&self) -> Box<dyn KexAlgorithm + Send> { |
| 33 | + Box::new(EcdhNistPKex::<NistP384> { |
| 34 | + local_secret: None, |
| 35 | + shared_secret: None, |
| 36 | + }) as Box<dyn KexAlgorithm + Send> |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +pub struct EcdhNistP521KexType {} |
| 41 | + |
| 42 | +impl KexType for EcdhNistP521KexType { |
| 43 | + fn make(&self) -> Box<dyn KexAlgorithm + Send> { |
| 44 | + Box::new(EcdhNistPKex::<NistP521> { |
| 45 | + local_secret: None, |
| 46 | + shared_secret: None, |
| 47 | + }) as Box<dyn KexAlgorithm + Send> |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +#[doc(hidden)] |
| 52 | +pub struct EcdhNistPKex<C: Curve + CurveArithmetic> { |
| 53 | + local_secret: Option<EphemeralSecret<C>>, |
| 54 | + shared_secret: Option<SharedSecret<C>>, |
| 55 | +} |
| 56 | + |
| 57 | +impl<C: Curve + CurveArithmetic> std::fmt::Debug for EcdhNistPKex<C> { |
| 58 | + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { |
| 59 | + write!( |
| 60 | + f, |
| 61 | + "Algorithm {{ local_secret: [hidden], shared_secret: [hidden] }}", |
| 62 | + ) |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +impl<C: Curve + CurveArithmetic> KexAlgorithm for EcdhNistPKex<C> |
| 67 | +where |
| 68 | + C: PointCompression, |
| 69 | + FieldBytesSize<C>: ModulusSize, |
| 70 | + AffinePoint<C>: FromEncodedPoint<C> + ToEncodedPoint<C>, |
| 71 | +{ |
| 72 | + fn skip_exchange(&self) -> bool { |
| 73 | + false |
| 74 | + } |
| 75 | + |
| 76 | + #[doc(hidden)] |
| 77 | + fn server_dh(&mut self, exchange: &mut Exchange, payload: &[u8]) -> Result<(), crate::Error> { |
| 78 | + debug!("server_dh"); |
| 79 | + |
| 80 | + let client_pubkey = { |
| 81 | + if payload.first() != Some(&msg::KEX_ECDH_INIT) { |
| 82 | + return Err(crate::Error::Inconsistent); |
| 83 | + } |
| 84 | + |
| 85 | + #[allow(clippy::indexing_slicing)] // length checked |
| 86 | + let pubkey_len = BigEndian::read_u32(&payload[1..]) as usize; |
| 87 | + |
| 88 | + if payload.len() < 5 + pubkey_len { |
| 89 | + return Err(crate::Error::Inconsistent); |
| 90 | + } |
| 91 | + |
| 92 | + #[allow(clippy::indexing_slicing)] // length checked |
| 93 | + elliptic_curve::PublicKey::<C>::from_sec1_bytes(&payload[5..(5 + pubkey_len)]) |
| 94 | + .map_err(|_| crate::Error::Inconsistent)? |
| 95 | + }; |
| 96 | + |
| 97 | + let server_secret = |
| 98 | + elliptic_curve::ecdh::EphemeralSecret::<C>::random(&mut rand_core::OsRng); |
| 99 | + let server_pubkey = server_secret.public_key(); |
| 100 | + |
| 101 | + // fill exchange. |
| 102 | + exchange.server_ephemeral.clear(); |
| 103 | + exchange |
| 104 | + .server_ephemeral |
| 105 | + .extend(&server_pubkey.to_sec1_bytes()); |
| 106 | + let shared = server_secret.diffie_hellman(&client_pubkey); |
| 107 | + self.shared_secret = Some(shared); |
| 108 | + Ok(()) |
| 109 | + } |
| 110 | + |
| 111 | + #[doc(hidden)] |
| 112 | + fn client_dh( |
| 113 | + &mut self, |
| 114 | + client_ephemeral: &mut CryptoVec, |
| 115 | + buf: &mut CryptoVec, |
| 116 | + ) -> Result<(), crate::Error> { |
| 117 | + let client_secret = |
| 118 | + elliptic_curve::ecdh::EphemeralSecret::<C>::random(&mut rand_core::OsRng); |
| 119 | + let client_pubkey = client_secret.public_key(); |
| 120 | + |
| 121 | + // fill exchange. |
| 122 | + client_ephemeral.clear(); |
| 123 | + client_ephemeral.extend(&client_pubkey.to_sec1_bytes()); |
| 124 | + |
| 125 | + buf.push(msg::KEX_ECDH_INIT); |
| 126 | + buf.extend_ssh_string(&client_pubkey.to_sec1_bytes()); |
| 127 | + |
| 128 | + self.local_secret = Some(client_secret); |
| 129 | + Ok(()) |
| 130 | + } |
| 131 | + |
| 132 | + fn compute_shared_secret(&mut self, remote_pubkey_: &[u8]) -> Result<(), crate::Error> { |
| 133 | + let local_secret = self.local_secret.take().ok_or(crate::Error::KexInit)?; |
| 134 | + let pubkey = elliptic_curve::PublicKey::<C>::from_sec1_bytes(remote_pubkey_) |
| 135 | + .map_err(|_| crate::Error::KexInit)?; |
| 136 | + self.shared_secret = Some(local_secret.diffie_hellman(&pubkey)); |
| 137 | + Ok(()) |
| 138 | + } |
| 139 | + |
| 140 | + fn compute_exchange_hash( |
| 141 | + &self, |
| 142 | + key: &CryptoVec, |
| 143 | + exchange: &Exchange, |
| 144 | + buffer: &mut CryptoVec, |
| 145 | + ) -> Result<CryptoVec, crate::Error> { |
| 146 | + // Computing the exchange hash, see page 7 of RFC 5656. |
| 147 | + buffer.clear(); |
| 148 | + buffer.extend_ssh_string(&exchange.client_id); |
| 149 | + buffer.extend_ssh_string(&exchange.server_id); |
| 150 | + buffer.extend_ssh_string(&exchange.client_kex_init); |
| 151 | + buffer.extend_ssh_string(&exchange.server_kex_init); |
| 152 | + |
| 153 | + buffer.extend(key); |
| 154 | + buffer.extend_ssh_string(&exchange.client_ephemeral); |
| 155 | + buffer.extend_ssh_string(&exchange.server_ephemeral); |
| 156 | + |
| 157 | + if let Some(ref shared) = self.shared_secret { |
| 158 | + buffer.extend_ssh_mpint(shared.raw_secret_bytes()); |
| 159 | + } |
| 160 | + |
| 161 | + use sha2::Digest; |
| 162 | + let mut hasher = sha2::Sha256::new(); |
| 163 | + hasher.update(&buffer); |
| 164 | + |
| 165 | + let mut res = CryptoVec::new(); |
| 166 | + res.extend(hasher.finalize().as_slice()); |
| 167 | + Ok(res) |
| 168 | + } |
| 169 | + |
| 170 | + fn compute_keys( |
| 171 | + &self, |
| 172 | + session_id: &CryptoVec, |
| 173 | + exchange_hash: &CryptoVec, |
| 174 | + cipher: cipher::Name, |
| 175 | + remote_to_local_mac: mac::Name, |
| 176 | + local_to_remote_mac: mac::Name, |
| 177 | + is_server: bool, |
| 178 | + ) -> Result<crate::kex::cipher::CipherPair, crate::Error> { |
| 179 | + compute_keys::<sha2::Sha256>( |
| 180 | + self.shared_secret |
| 181 | + .as_ref() |
| 182 | + .map(|x| x.raw_secret_bytes() as &[u8]), |
| 183 | + session_id, |
| 184 | + exchange_hash, |
| 185 | + cipher, |
| 186 | + remote_to_local_mac, |
| 187 | + local_to_remote_mac, |
| 188 | + is_server, |
| 189 | + ) |
| 190 | + } |
| 191 | +} |
| 192 | + |
| 193 | +#[cfg(test)] |
| 194 | +mod tests { |
| 195 | + use super::*; |
| 196 | + |
| 197 | + #[test] |
| 198 | + fn test_shared_secret() { |
| 199 | + let mut party1 = EcdhNistPKex::<NistP256> { |
| 200 | + local_secret: Some(EphemeralSecret::<NistP256>::random(&mut rand_core::OsRng)), |
| 201 | + shared_secret: None, |
| 202 | + }; |
| 203 | + let p1_pubkey = party1.local_secret.as_ref().unwrap().public_key(); |
| 204 | + |
| 205 | + let mut party2 = EcdhNistPKex::<NistP256> { |
| 206 | + local_secret: Some(EphemeralSecret::<NistP256>::random(&mut rand_core::OsRng)), |
| 207 | + shared_secret: None, |
| 208 | + }; |
| 209 | + let p2_pubkey = party2.local_secret.as_ref().unwrap().public_key(); |
| 210 | + |
| 211 | + party1 |
| 212 | + .compute_shared_secret(&p2_pubkey.to_sec1_bytes()) |
| 213 | + .unwrap(); |
| 214 | + |
| 215 | + party2 |
| 216 | + .compute_shared_secret(&p1_pubkey.to_sec1_bytes()) |
| 217 | + .unwrap(); |
| 218 | + |
| 219 | + let p1_shared_secret = party1.shared_secret.unwrap(); |
| 220 | + let p2_shared_secret = party2.shared_secret.unwrap(); |
| 221 | + |
| 222 | + assert_eq!( |
| 223 | + p1_shared_secret.raw_secret_bytes(), |
| 224 | + p2_shared_secret.raw_secret_bytes() |
| 225 | + ) |
| 226 | + } |
| 227 | +} |
0 commit comments