str0m-wincrypto 0.6.1

Supporting crate for str0m
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
//! CryptoProvider implementation for Windows CNG/SChannel.
//!
//! This module provides a complete CryptoProvider that can be passed to str0m,
//! implementing all cryptographic operations using Windows native APIs.

use dimpl::{Error as DtlsImplError, KeyingMaterial, SrtpProfile};
use std::sync::Arc;
use std::time::Instant;
use str0m_proto::crypto::SupportedAes128CmSha1_80;
use str0m_proto::crypto::{AeadAes128GcmCipher, AeadAes256GcmCipher, Aes128CmSha1_80Cipher};
use str0m_proto::crypto::{CryptoError, CryptoProvider};
use str0m_proto::crypto::{DtlsCert, DtlsInstance, DtlsOutput, DtlsProvider, DtlsVersion};
use str0m_proto::crypto::{Sha1HmacProvider, Sha256Provider};
use str0m_proto::crypto::{SrtpProvider, SupportedAeadAes128Gcm, SupportedAeadAes256Gcm};

/// Create the default Windows CNG/SChannel crypto provider.
///
/// This provider implements all cryptographic operations required for WebRTC:
/// - DTLS 1.2 for secure key exchange (using dimpl protocol + SChannel)
/// - SRTP for encrypted media (using Windows CNG)
/// - SHA1-HMAC for STUN message integrity (using Windows CNG)
/// - SHA-256 for certificate fingerprints (using Windows CNG)
///
/// # Example
///
/// ```no_run
/// use str0m::RtcConfig;
///
/// let crypto_provider = str0m_wincrypto::default_provider();
/// let config = RtcConfig::new().with_crypto_provider(crypto_provider);
/// ```
pub fn default_provider() -> CryptoProvider {
    static SRTP: WinCryptoSrtpProvider = WinCryptoSrtpProvider;
    static SHA1_HMAC: WinCryptoSha1HmacProvider = WinCryptoSha1HmacProvider;
    static SHA256: WinCryptoSha256Provider = WinCryptoSha256Provider;
    static DTLS: WinCryptoDtlsProvider = WinCryptoDtlsProvider;

    CryptoProvider {
        srtp_provider: &SRTP,
        sha1_hmac_provider: &SHA1_HMAC,
        sha256_provider: &SHA256,
        dtls_provider: &DTLS,
    }
}

// ============================================================================
// SHA1 HMAC Provider
// ============================================================================

#[derive(Debug)]
struct WinCryptoSha1HmacProvider;

impl Sha1HmacProvider for WinCryptoSha1HmacProvider {
    fn sha1_hmac(&self, key: &[u8], payloads: &[&[u8]]) -> [u8; 20] {
        crate::sha1_hmac(key, payloads).expect("SHA1-HMAC computation")
    }
}

// ============================================================================
// SHA256 Provider
// ============================================================================

#[derive(Debug)]
struct WinCryptoSha256Provider;

impl Sha256Provider for WinCryptoSha256Provider {
    fn sha256(&self, data: &[u8]) -> [u8; 32] {
        use windows::Win32::Security::Cryptography::BCRYPT_HASH_HANDLE;
        use windows::Win32::Security::Cryptography::BCRYPT_SHA256_ALG_HANDLE;
        use windows::Win32::Security::Cryptography::BCryptHashData;
        use windows::Win32::Security::Cryptography::{BCryptCreateHash, BCryptFinishHash};
        use windows::core::Owned;

        let mut hash = [0u8; 32];
        unsafe {
            let mut hash_handle = Owned::new(BCRYPT_HASH_HANDLE::default());

            crate::WinCryptoError::from_ntstatus(BCryptCreateHash(
                BCRYPT_SHA256_ALG_HANDLE,
                &mut *hash_handle,
                None,
                None,
                0,
            ))
            .expect("SHA-256 hash creation");

            crate::WinCryptoError::from_ntstatus(BCryptHashData(*hash_handle, data, 0))
                .expect("SHA-256 hash data");

            crate::WinCryptoError::from_ntstatus(BCryptFinishHash(*hash_handle, &mut hash, 0))
                .expect("SHA-256 hash finish");
        }
        hash
    }
}

// ============================================================================
// SRTP Provider
// ============================================================================

#[derive(Debug)]
struct WinCryptoSrtpProvider;

impl SrtpProvider for WinCryptoSrtpProvider {
    fn aes_128_cm_sha1_80(&self) -> &'static dyn SupportedAes128CmSha1_80 {
        &WinCryptoAes128CmSha1_80Factory
    }

    fn aead_aes_128_gcm(&self) -> &'static dyn SupportedAeadAes128Gcm {
        &WinCryptoAeadAes128GcmFactory
    }

    fn aead_aes_256_gcm(&self) -> &'static dyn SupportedAeadAes256Gcm {
        &WinCryptoAeadAes256GcmFactory
    }

    fn srtp_aes_128_ecb_round(&self, key: &[u8], input: &[u8], output: &mut [u8]) {
        let key = crate::SrtpKey::create_aes_ecb_key(key).expect("AES-128 ECB key");
        let count = crate::srtp_aes_ecb_round(&key, input, output).expect("AES-128 ECB");
        assert_eq!(count, 16 + 16); // block size + padding
    }

    fn srtp_aes_256_ecb_round(&self, key: &[u8], input: &[u8], output: &mut [u8]) {
        let key = crate::SrtpKey::create_aes_ecb_key(key).expect("AES-256 ECB key");
        let count = crate::srtp_aes_ecb_round(&key, input, output).expect("AES-256 ECB");
        assert_eq!(count, 16 + 16); // block size + padding
    }
}

// Cipher Factories

#[derive(Debug)]
struct WinCryptoAes128CmSha1_80Factory;

impl SupportedAes128CmSha1_80 for WinCryptoAes128CmSha1_80Factory {
    fn create_cipher(&self, key: [u8; 16], _encrypt: bool) -> Box<dyn Aes128CmSha1_80Cipher> {
        Box::new(WinCryptoAes128CmSha1_80::new(key))
    }
}

#[derive(Debug)]
struct WinCryptoAeadAes128GcmFactory;

impl SupportedAeadAes128Gcm for WinCryptoAeadAes128GcmFactory {
    fn create_cipher(&self, key: [u8; 16], _encrypt: bool) -> Box<dyn AeadAes128GcmCipher> {
        Box::new(WinCryptoAeadAes128Gcm::new(key))
    }
}

#[derive(Debug)]
struct WinCryptoAeadAes256GcmFactory;

impl SupportedAeadAes256Gcm for WinCryptoAeadAes256GcmFactory {
    fn create_cipher(&self, key: [u8; 32], _encrypt: bool) -> Box<dyn AeadAes256GcmCipher> {
        Box::new(WinCryptoAeadAes256Gcm::new(key))
    }
}

// Cipher Implementations

#[derive(Debug)]
struct WinCryptoAes128CmSha1_80 {
    key: crate::SrtpKey,
}

impl WinCryptoAes128CmSha1_80 {
    fn new(key: [u8; 16]) -> Self {
        Self {
            key: crate::SrtpKey::create_aes_ctr_key(&key).expect("AES-128-CTR key"),
        }
    }
}

impl Aes128CmSha1_80Cipher for WinCryptoAes128CmSha1_80 {
    fn encrypt(
        &mut self,
        iv: &[u8; 16],
        input: &[u8],
        output: &mut [u8],
    ) -> Result<(), CryptoError> {
        crate::srtp_aes_128_cm(&self.key, iv, input, output)
            .map_err(|e| CryptoError::Other(format!("AES-128-CM encrypt: {}", e)))?;
        Ok(())
    }

    fn decrypt(
        &mut self,
        iv: &[u8; 16],
        input: &[u8],
        output: &mut [u8],
    ) -> Result<(), CryptoError> {
        crate::srtp_aes_128_cm(&self.key, iv, input, output)
            .map_err(|e| CryptoError::Other(format!("AES-128-CM decrypt: {}", e)))?;
        Ok(())
    }
}

#[derive(Debug)]
struct WinCryptoAeadAes128Gcm {
    key: crate::SrtpKey,
}

impl WinCryptoAeadAes128Gcm {
    fn new(key: [u8; 16]) -> Self {
        Self {
            key: crate::SrtpKey::create_aes_gcm_key(&key).expect("AES-128-GCM key"),
        }
    }
}

impl AeadAes128GcmCipher for WinCryptoAeadAes128Gcm {
    fn encrypt(
        &mut self,
        iv: &[u8; 12],
        aad: &[u8],
        input: &[u8],
        output: &mut [u8],
    ) -> Result<(), CryptoError> {
        crate::srtp_aead_aes_gcm_encrypt(&self.key, iv, aad, input, output)
            .map_err(|e| CryptoError::Other(format!("AEAD-AES-128-GCM encrypt: {}", e)))?;
        Ok(())
    }

    fn decrypt(
        &mut self,
        iv: &[u8; 12],
        aads: &[&[u8]],
        input: &[u8],
        output: &mut [u8],
    ) -> Result<usize, CryptoError> {
        crate::srtp_aead_aes_gcm_decrypt(&self.key, iv, aads, input, output)
            .map_err(|e| CryptoError::Other(format!("AEAD-AES-128-GCM decrypt: {}", e)))
    }
}

#[derive(Debug)]
struct WinCryptoAeadAes256Gcm {
    key: crate::SrtpKey,
}

impl WinCryptoAeadAes256Gcm {
    fn new(key: [u8; 32]) -> Self {
        Self {
            key: crate::SrtpKey::create_aes_gcm_key(&key).expect("AES-256-GCM key"),
        }
    }
}

impl AeadAes256GcmCipher for WinCryptoAeadAes256Gcm {
    fn encrypt(
        &mut self,
        iv: &[u8; 12],
        aad: &[u8],
        input: &[u8],
        output: &mut [u8],
    ) -> Result<(), CryptoError> {
        crate::srtp_aead_aes_gcm_encrypt(&self.key, iv, aad, input, output)
            .map_err(|e| CryptoError::Other(format!("AEAD-AES-256-GCM encrypt: {}", e)))?;
        Ok(())
    }

    fn decrypt(
        &mut self,
        iv: &[u8; 12],
        aads: &[&[u8]],
        input: &[u8],
        output: &mut [u8],
    ) -> Result<usize, CryptoError> {
        crate::srtp_aead_aes_gcm_decrypt(&self.key, iv, aads, input, output)
            .map_err(|e| CryptoError::Other(format!("AEAD-AES-256-GCM decrypt: {}", e)))
    }
}

// ============================================================================
// DTLS Provider
// ============================================================================

#[derive(Debug)]
struct WinCryptoDtlsProvider;

impl DtlsProvider for WinCryptoDtlsProvider {
    fn generate_certificate(&self) -> Option<DtlsCert> {
        let cert = crate::Certificate::new_self_signed(true, "CN=WebRTC").ok()?;

        // Get the DER bytes from the certificate
        unsafe {
            let cert_context = *cert.context();
            let der_bytes = std::slice::from_raw_parts(
                cert_context.pbCertEncoded,
                cert_context.cbCertEncoded as usize,
            );

            // Store the certificate so we can use it later
            Some(DtlsCert {
                certificate: der_bytes.to_vec(),
                private_key: vec![], // We'll handle this through the Certificate wrapper
            })
        }
    }

    fn new_dtls(
        &self,
        cert: &DtlsCert,
        _now: Instant,
        dtls_version: DtlsVersion,
    ) -> Result<Box<dyn DtlsInstance>, CryptoError> {
        if dtls_version != DtlsVersion::Dtls12 {
            return Err(CryptoError::Other(
                "WinCrypto DTLS provider only supports DTLS 1.2/Auto. \
                 Use aws-lc-rs or rust-crypto backend for DTLS 1.3."
                    .to_string(),
            ));
        }
        // For now, generate a new certificate since we need the Windows CERT_CONTEXT
        // In a real implementation, we'd need to reconstruct from the DER bytes
        let win_cert = crate::Certificate::new_self_signed(true, "CN=WebRTC")
            .map_err(|e| CryptoError::Other(format!("Certificate creation: {}", e)))?;

        let dtls = crate::Dtls::new(Arc::new(win_cert))
            .map_err(|e| CryptoError::Other(format!("DTLS creation: {}", e)))?;

        Ok(Box::new(WinCryptoDtlsInstance { dtls }))
    }
}

// ============================================================================
// DTLS Instance Wrapper
// ============================================================================

struct WinCryptoDtlsInstance {
    dtls: crate::Dtls,
}

impl std::fmt::Debug for WinCryptoDtlsInstance {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("WinCryptoDtlsInstance").finish()
    }
}

impl DtlsInstance for WinCryptoDtlsInstance {
    fn set_active(&mut self, active: bool) {
        self.dtls.set_as_client(active).expect("set_as_client");
    }

    fn handle_packet(&mut self, packet: &[u8]) -> Result<(), DtlsImplError> {
        self.dtls
            .handle_receive(Some(packet))
            .map_err(|e| DtlsImplError::from(format!("DTLS error: {}", e)))?;
        Ok(())
    }

    fn poll_output<'a>(&mut self, buf: &'a mut [u8]) -> DtlsOutput<'a> {
        // Poll for datagram first
        if let Some(datagram) = self.dtls.pull_datagram() {
            let len = datagram.len().min(buf.len());
            buf[..len].copy_from_slice(&datagram[..len]);
            return DtlsOutput::Datagram(&buf[..len]);
        }

        // Check for events
        match self.dtls.handle_receive(None) {
            Ok(crate::DtlsEvent::Connected {
                srtp_profile_id,
                srtp_keying_material,
                peer_fingerprint,
            }) => {
                let profile = match srtp_profile_id {
                    0x0001 => SrtpProfile::AES128_CM_SHA1_80,
                    0x0007 => SrtpProfile::AEAD_AES_128_GCM,
                    0x0008 => SrtpProfile::AEAD_AES_256_GCM,
                    _ => return DtlsOutput::Error(DtlsImplError::from("Unknown SRTP profile")),
                };

                DtlsOutput::Connected {
                    srtp_profile: profile,
                    keying_material: KeyingMaterial::new(&srtp_keying_material),
                    peer_fingerprint,
                }
            }
            Ok(crate::DtlsEvent::Data(data)) => {
                let len = data.len().min(buf.len());
                buf[..len].copy_from_slice(&data[..len]);
                DtlsOutput::ApplicationData(&buf[..len])
            }
            Ok(crate::DtlsEvent::None) | Ok(crate::DtlsEvent::WouldBlock) => DtlsOutput::None,
            Err(e) => DtlsOutput::Error(DtlsImplError::from(format!("DTLS error: {}", e))),
        }
    }

    fn handle_timeout(&mut self, _now: Instant) -> Result<(), DtlsImplError> {
        self.dtls
            .handle_receive(None)
            .map_err(|e| DtlsImplError::from(format!("DTLS timeout: {}", e)))?;
        Ok(())
    }

    fn send_application_data(&mut self, data: &[u8]) -> Result<(), DtlsImplError> {
        self.dtls
            .send_data(data)
            .map_err(|e| DtlsImplError::from(format!("DTLS send: {}", e)))?;
        Ok(())
    }

    fn is_active(&self) -> bool {
        self.dtls.is_client().unwrap_or(false)
    }
}