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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
//! Contains all functionality related to the server part,
//! including the generation of values for the database.
//!
//! All arrays are **little endian**.
//! See `examples/server.rs` for how to interface with real clients.
//!
//! # Generating database values
//!
//! When signing up a new user you want to take their username and password and
//! convert them into the values stored in your database.
//! This is done with the [`SrpVerifier::from_username_and_password`] function.
//! This allows you to get the username, password verifier and salt values.
//! The [`SrpVerifier`] page contains specifics.
//! The [`SrpVerifier`] struct is created using [`NormalizedString`]s, the reasoning is
//! explained on the [`normalized_string`](`crate::normalized_string`) page.
//!
//! This could look something like this:
//! ```
//! use wow_srp::server::SrpVerifier;
//! use wow_srp::{error::NormalizedStringError, SALT_LENGTH, PASSWORD_VERIFIER_LENGTH};
//! use wow_srp::normalized_string::NormalizedString;
//!
//! // Generic function to tie everything together
//! fn get_credentials_and_save_to_database() -> Result<(),
//!                                      NormalizedStringError> {
//!     // This would be gotten from the sign up page
//!     let username = NormalizedString::new("Alice")?;
//!     let password = NormalizedString::new("password123")?;
//!     let v
//!          = SrpVerifier::from_username_and_password(username, password);
//!
//!     // The SrpVerifier is no longer needed after this
//!     // See NormalizedString for whether to save raw username or NormalizedString in Database
//!     save_values_to_database(v.username(),
//!                             &v.password_verifier(),
//!                             &v.salt());
//!     // Return successful
//!     Ok(())
//! }
//!
//! fn save_values_to_database(username: &str,
//!                            password_verifier: &[u8; PASSWORD_VERIFIER_LENGTH as usize],
//!                            salt: &[u8; SALT_LENGTH as usize]) {
//!     // DB specific stuff here.
//! }
//! ```
//!
//! # Authenticating on the server
//!
//! A functional example is included in `examples/server.rs`.
//! This text focuses on a high level overview and important notices.
//!
//! The [Typestate](https://yoric.github.io/post/rust-typestate/) pattern is used
//! in order to prevent incorrect use.
//! This means that whenever the next step of computation takes place, you call a function
//! taking `self`, consuming the old object, and returning the new object.
//!
//! When authenticating players attempting to connect to the server, we start from where we let go
//! with the last example.
//!
//! * First an [`SrpVerifier`] is created using the database values,
//! * Then [`SrpVerifier::into_proof`] is called to convert it into an [`SrpProof`],
//! * Finally [`SrpProof::into_server`] is called to convert it into an [`SrpServer`] and a
//! server proof.
//!
//! The [`SrpServer`] means that the client has been correctly authenticated
//! and can be sent the realmlist.
//! The [`SrpServer`] also provides the possibility of authenticating reconnects.
//! The [`SrpServer`] does _NOT_ provide any rate limiting or time based expiration of the
//! ability to reconnect, this means that the implementer will need to ensure that clients
//! do not abuse the functionality.
//!
//! The state machine goes like this:
//! ```text
//! SrpVerifier -> SrpProof -> mut SrpServer -|
//!                   +                ^      |
//!              server_proof          |      |
//!                                    |------|
//! ```
//! Where you would keep a mutable [`SrpServer`] around for reconnects and using the session key.
//! If any of the steps fail before the [`SrpServer`] has been created all progress will be lost.
//! After the [`SrpServer`] is created, an unlimited amount of reconnection attemps can be made,
//! and the call to [`SrpServer::verify_reconnection_attempt`] will automatically refresh the
//! [`SrpServer::reconnect_challenge_data`].
//!
//! ```
//! use wow_srp::server::SrpVerifier;
//! use wow_srp::normalized_string::NormalizedString;
//! use wow_srp::{PASSWORD_VERIFIER_LENGTH, SALT_LENGTH, error::SrpError, error::NormalizedStringError, PublicKey};
//!
//! fn server() -> Result<(), SrpError> {
//!     // Gotten from database
//!     let username = NormalizedString::new("A")?;
//!     let password_verifier = [ 106, 6, 11, 113, 103, 55, 49, 130, 210, 249, 178, 176, 73, 77, 229, 163, 127, 223, 122, 163, 245, 174, 60, 217, 151, 142, 169, 173, 208, 8, 152, 31, ];
//!     let salt = [ 120, 156, 208, 137, 73, 108, 21, 91, 28, 22, 13, 255, 99, 116, 71, 102, 158, 70, 65, 189, 153, 244, 143, 13, 214, 200, 160, 94, 217, 112, 206, 125, ];
//!
//!     let verifier = SrpVerifier::from_database_values(username, password_verifier, salt);
//!     let proof = verifier.into_proof();
//!
//!     // Gotten from client
//!     let client_public_key = [ 105, 93, 211, 227, 214, 155, 247, 119, 156, 33, 156, 79, 15, 139, 100, 120, 1, 180, 32, 66, 165, 41, 175, 146, 216, 251, 25, 207, 18, 14, 35, 68, ];
//!     // Can fail on invalid public key. See PublicKey for more info.
//!     let client_public_key = PublicKey::from_le_bytes(&client_public_key)?;
//!
//!     let client_proof = [ 228, 40, 212, 74, 196, 143, 169, 148, 201, 150, 184, 123, 205, 40, 103, 234, 99, 155, 193, 7, ];
//!
//!     // Can fail on proof comparison which means the password is incorrect.
//!     // Send a failure packet and drop the connection.
//!     let (mut server, server_proof) = proof.into_server(client_public_key, client_proof)?;
//!     // If this passes the client is successfully authenticated.
//!
//!     // Send the proof to client to prove that the server also knows the correct password.
//!
//!     // Add the server object to e.g. a HashMap of authenticated users.
//!     // Client will now send a 'Send Realmlist' packet
//!
//!     // Client wants to reconnect
//!     let reconnect_data = server.reconnect_challenge_data();
//!     // Send to client
//!     
//!     // Get back from client
//!     let client_data = [ 8, 226, 88, 41, 231, 219, 29, 59, 127, 98, 180, 55, 32, 201, 135, 163, ];
//!     let client_proof = [ 37, 167, 41, 153, 253, 156, 41, 174, 225, 125, 246, 158, 106, 248, 158, 232, 146, 8, 242, 164, ];
//!
//!     // Returns true if the proofs match, client is allowed to reconnect
//!     assert!(server.verify_reconnection_attempt(client_data, client_proof));
//!
//!     Ok(())
//! }
//! ```
//!

use crate::error::MatchProofsError;
use crate::key::{
    PrivateKey, Proof, PublicKey, ReconnectData, Salt, SessionKey, PROOF_LENGTH, PUBLIC_KEY_LENGTH,
    RECONNECT_CHALLENGE_DATA_LENGTH, SALT_LENGTH, SESSION_KEY_LENGTH,
};
use crate::key::{Verifier, PASSWORD_VERIFIER_LENGTH};
use crate::normalized_string::NormalizedString;
use crate::srp_internal::calculate_reconnect_proof;
use crate::{error::InvalidPublicKeyError, srp_internal};

/// Creates and contains the username, password verifier, and salt values.
/// First step of the server, next is [`SrpProof`].
///
/// These are values that should be stored in the database.
/// Do **NOT** store raw passwords in the database.
///
/// The salt is a randomly generated [32 byte](SALT_LENGTH) array of random data used as salt for
/// the password verifier.
/// The verifier is the result of SHA-1 hashing a combination of the username, password and salt.
/// The salt is sent over the network for the client to use.
/// The password verifier is used for generating the server public key, and should never leave the server.
/// The private key is not exposed through any of the structs.
///
/// All byte arrays are **little endian**.
///
/// # Example
///
/// ```rust
/// use wow_srp::normalized_string::NormalizedString;
/// use wow_srp::{error::NormalizedStringError, SALT_LENGTH, PASSWORD_VERIFIER_LENGTH};
/// use wow_srp::server::SrpVerifier;
///
/// // Create salt and password verifier values for signup page
/// fn to_database() -> Result<(), NormalizedStringError> {
///     // See NormalizedString for specifics.
///     let username = NormalizedString::new("Alice")?;
///     let password = NormalizedString::new("password123")?;
///
///     let verifier = SrpVerifier::from_username_and_password(username, password);
///
///     assert_eq!("ALICE", verifier.username());
///
///     // Salt is randomly chosen and password_verifier depends on salt so we can't assert_eq
///     // Store these values in the database for future authentication
///     let password_verifier = verifier.password_verifier();
///     let salt = verifier.salt();
///
///     Ok(())
/// }
///
/// // Authenticate client logging into game server
/// fn from_database() -> Result<(), NormalizedStringError> {
///     // Get these from the database
///     let username = NormalizedString::new("Alice")?;
///     let password_verifier = [0u8; PASSWORD_VERIFIER_LENGTH as usize];
///     let salt = [0u8; SALT_LENGTH as usize];
///
///     let verifier = SrpVerifier::from_database_values(username, password_verifier, salt);
///
///     // Next step is continuing into the state machine, see into_proof() and SrpProof for more.
///     let proof = verifier.into_proof();
///
///     Ok(())
/// }
///
/// # to_database();
/// ```
#[doc(alias = "v")]
#[doc(alias = "salt")]
pub struct SrpVerifier {
    username: NormalizedString,
    password_verifier: Verifier,
    salt: Salt,
}

impl SrpVerifier {
    #[doc(alias = "U")]
    /// The [`normalized_string`](`crate::normalized_string`) representation of the username,
    /// see that for more details.
    ///
    /// Called `U` and `<username>` in [RFC2945](https://tools.ietf.org/html/rfc2945).
    pub fn username(&self) -> &str {
        self.username.as_ref()
    }

    #[doc(alias = "v")]
    /// The password verifier. Should not be used except for when saving to the database.
    /// Array is **little endian**.
    ///
    /// Called `v` and `<password verifier>` in [RFC2945](https://tools.ietf.org/html/rfc2945).
    /// Always [32 bytes (256 bits)](crate::PASSWORD_VERIFIER_LENGTH) in length
    /// since the value is generated through
    /// the remainder of a [32 byte value](crate::LARGE_SAFE_PRIME_LENGTH).
    pub const fn password_verifier(&self) -> &[u8; PASSWORD_VERIFIER_LENGTH as usize] {
        self.password_verifier.as_le()
    }

    #[doc(alias = "s")]
    /// Salt value used for calculating verifier. Is sent to the client.
    /// Array is **little endian**.
    ///
    /// Called `s`, `<salt from passwd file>` and `<salt>` in [RFC2945](https://tools.ietf.org/html/rfc2945).
    /// Always [32 bytes (256 bits)](crate::SALT_LENGTH) in length since the packet sent to
    /// the client has a fixed width.
    pub const fn salt(&self) -> &[u8; SALT_LENGTH as usize] {
        self.salt.as_le()
    }

    /// See [`normalized_string`](`crate::normalized_string`) for more information on the format.
    /// Only use this for generating verifiers and salts to save to the database.
    /// Never use this by saving raw usernames and passwords on the database.
    pub fn from_username_and_password(
        username: NormalizedString,
        password: NormalizedString,
    ) -> Self {
        let salt = Salt::randomized();

        Self::with_specific_salt(username, password, &salt)
    }

    /// See [`normalized_string`](`crate::normalized_string`) for more information on the string format.
    /// Both arrays are **little endian**.
    pub const fn from_database_values(
        username: NormalizedString,
        password_verifier: [u8; PASSWORD_VERIFIER_LENGTH as usize],
        salt: [u8; SALT_LENGTH as usize],
    ) -> Self {
        Self {
            username,
            password_verifier: Verifier::from_le_bytes(password_verifier),
            salt: Salt::from_le_bytes(salt),
        }
    }

    /// Converts to an [`SrpProof`], consuming the [`SrpVerifier`].
    ///
    /// # Panics:
    ///
    /// * Panics if the RNG returns an error. If RNG does not work the authentication server
    /// should not continue functioning and therefore panics.
    /// * _Very_ rarely panic if the server generated public key is invalid.
    ///
    /// There are only two invalid states for the randomly generated server public key:
    /// * All zeros.
    /// * Exactly the same as [the large safe prime](`crate::LARGE_SAFE_PRIME_LITTLE_ENDIAN`).
    ///
    /// This is 2 out of `2^256` possible states. The chances of this occurring naturally are very slim.
    /// It is significantly more likely that the RNG of the system has been compromised in which case
    /// authentication is not possible.
    #[doc(alias = "M")]
    #[doc(alias = "M1")]
    #[doc(alias = "M2")]
    pub fn into_proof(self) -> SrpProof {
        let server_private_key = PrivateKey::randomized();

        Self::with_specific_private_key(self, server_private_key)
            .expect("The generated public key was invalid. This is insanely unlikely and even if you only see this error once you should probably check that your random number generation has not been compromised in some way. See documentation for SrpVerifier. Please report this on Github at 'https://github.com/gtker/wow_srp'.")
    }

    fn with_specific_salt(
        username: NormalizedString,
        password: NormalizedString,
        salt: &Salt,
    ) -> Self {
        let password_verifier =
            srp_internal::calculate_password_verifier(&username, &password, salt);

        Self::from_database_values(username, password_verifier, *salt.as_le())
    }

    fn with_specific_private_key(
        self,
        server_private_key: PrivateKey,
    ) -> Result<SrpProof, InvalidPublicKeyError> {
        let server_public_key = srp_internal::calculate_server_public_key(
            &self.password_verifier,
            &server_private_key,
        )?;

        Ok(SrpProof {
            username: self.username,
            server_public_key,
            salt: self.salt,
            server_private_key,
            password_verifier: self.password_verifier,
        })
    }
}

/// Contains the server public key, private key and salt. Second step of the server, next is [`SrpServer`].
///
/// This struct is created from the [`SrpVerifier::into_proof`] method.
/// The server public key is generated from the verifier and a random private key.
/// The salt is the same as the [`SrpVerifier`].
///
/// The client also requires the [large safe prime](crate::LARGE_SAFE_PRIME_LITTLE_ENDIAN) and the
/// [generator](crate::GENERATOR). These are static values that never change and therefore they
/// have their own const variables.
///
/// The private key used is 32 bytes long.
///
/// See `examples/server.rs` for the full example.
///
/// All byte arrays are **little endian**.
///
/// # Example
///
/// The example cuts out stuff related to [`SrpVerifier`] and [`SrpServer`].
/// ```
/// # use wow_srp::{error::MatchProofsError, PUBLIC_KEY_LENGTH, PROOF_LENGTH, PublicKey, error::InvalidPublicKeyError, LARGE_SAFE_PRIME_LITTLE_ENDIAN, GENERATOR};
/// # use wow_srp::server::{SrpVerifier, SrpServer};
/// # use wow_srp::normalized_string::NormalizedString;
/// # fn server() -> Result<(), MatchProofsError> {
/// #    let verifier = SrpVerifier::from_username_and_password(NormalizedString::new("Alice").unwrap(),
/// #                                                     NormalizedString::new("password123").unwrap());
/// #
/// let proof = verifier.into_proof();
/// let salt = proof.salt();
/// let server_public_key = proof.server_public_key();
///
/// // Not part of this struct, but used in the same network packet and tightly related.
/// let large_safe_prime = LARGE_SAFE_PRIME_LITTLE_ENDIAN;
/// let generator = GENERATOR;
/// #
/// # let client_public_key = [255u8; PUBLIC_KEY_LENGTH as usize];
///
/// // Public key gotten from client
/// let client_public_key = PublicKey::from_le_bytes(&client_public_key);
/// let client_public_key = match client_public_key {
///     Ok(c) => {c}
///     Err(_) => {
///         panic!("Public key is invalid. This is either _very_ rare or malicious.")
///     }
/// };
///
/// // Proof gotten from client
/// let client_proof = [255u8; PROOF_LENGTH as usize];
/// let server = proof.into_server(client_public_key, client_proof);
/// let server = match server {
///     Ok(s) => {s}
///     Err(_) => {
///         panic!("Proofs do not match (password is incorrect). This is common.")
///     }
/// };
/// #    Ok(())
/// # }
/// ```
///
#[derive(Debug)]
#[doc(alias = "M")]
#[doc(alias = "M1")]
#[doc(alias = "M2")]
#[doc(alias = "B")]
#[doc(alias = "s")]
#[doc(alias = "salt")]
pub struct SrpProof {
    username: NormalizedString,
    server_public_key: PublicKey,
    salt: Salt,

    server_private_key: PrivateKey,

    password_verifier: Verifier,
}

impl SrpProof {
    /// Server public key used in calculations by both the server and client. Is sent to the client.
    /// Array is **little endian**.
    ///
    /// Called `B` in [RFC2945](https://tools.ietf.org/html/rfc2945).
    /// Always [32 bytes (256 bits)](crate::PUBLIC_KEY_LENGTH) in length since the packet sent to the client has a fixed width.
    #[doc(alias = "B")]
    pub const fn server_public_key(&self) -> &[u8; PUBLIC_KEY_LENGTH as usize] {
        self.server_public_key.as_le()
    }

    /// Salt value used for calculating verifier. Is sent to the client.
    /// Array is **little endian**.
    ///
    /// Called `s`, `<salt from passwd file>` or `<salt>` in [RFC2945](https://tools.ietf.org/html/rfc2945).
    /// Always [32 bytes (256 bits)](crate::SALT_LENGTH) in length since the packet sent to the client has a fixed width.
    #[doc(alias = "s")]
    pub const fn salt(&self) -> &[u8; SALT_LENGTH as usize] {
        self.salt.as_le()
    }

    /// Converts to an [`SrpServer`] and server proof by using the client supplied public key and proof,
    /// consuming the [`SrpProof`].
    ///
    /// The server proof value must be sent to the client in order to prove that the server knows the
    /// same password as the client.
    /// The server proof is called `M` in [RFC2945](https://tools.ietf.org/html/rfc2945),
    /// and `M2` in other litterature.
    /// This is different from the paramter to [`SrpProof::into_server`] which is referred to as `M1`
    /// in other litterature, but both are referred to as `M` in
    /// [RFC2945](https://tools.ietf.org/html/rfc2945).
    ///
    /// The server proof is a **little endian** array.
    /// The server proof is always [20 bytes (160 bits)](crate::PROOF_LENGTH) because it's a SHA-1 hash.
    ///
    /// The [`PublicKey`] is used instead of an array in order to break the validation of the
    /// public key and the calculation of the proof into separate steps.
    /// An invalid [`PublicKey`] is more likely to be the result of the client deliberately sending
    /// known invalid data while the [`MatchProofsError`] just means that the entered password is incorrect.
    ///
    /// The client_public_key is called `A` in [RFC2945](https://tools.ietf.org/html/rfc2945) and most other
    /// literature. It is sometimes incorrectly called `a`, but this refers to the client private key.
    /// Private keys are never sent over the network, so if you see `a` in a packet table it is referring
    /// to the client _public_ key and not the private key.
    ///
    /// The client_proof is called `M` in [RFC2945](https://tools.ietf.org/html/rfc2945) and `M1` in other
    /// literature. This is different from the server proof returned from [`SrpProof::into_server`] which is often called
    /// `M2`, but is still referred to as `M` in [RFC2945](https://tools.ietf.org/html/rfc2945).
    /// The client_proof is always [20 bytes (160 bits)](PROOF_LENGTH) in length because it's a SHA-1 hash.
    ///
    /// # Example
    ///
    /// ```
    /// # use wow_srp::{error::MatchProofsError, PUBLIC_KEY_LENGTH, PROOF_LENGTH, PublicKey, error::InvalidPublicKeyError};
    /// # use wow_srp::server::{SrpVerifier, SrpServer};
    /// # use wow_srp::normalized_string::NormalizedString;
    /// # fn server() -> Result<(), MatchProofsError> {
    /// #    let verifier = SrpVerifier::from_username_and_password(NormalizedString::new("Alice").unwrap(),
    /// #                                                     NormalizedString::new("password123").unwrap());
    /// #
    /// #    let proof = verifier.into_proof();
    /// #
    /// # let client_public_key = [255u8; PUBLIC_KEY_LENGTH as usize];
    /// // This will panic because the public key, username, passwords and proofs are not related
    /// // Public key gotten from client
    /// let client_public_key = PublicKey::from_le_bytes(&client_public_key);
    /// let client_public_key = match client_public_key {
    ///     Ok(c) => {c}
    ///     Err(_) => {
    ///         panic!("Public key is invalid. This is either _very_ rare or malicious.")
    ///     }
    /// };
    /// // Proof gotten from client
    /// let client_proof = [255u8; PROOF_LENGTH as usize];
    ///
    /// let server = proof.into_server(client_public_key, client_proof);
    /// let server = match server {
    ///     Ok(s) => {s}
    ///     Err(_) => {
    ///         panic!("Proofs do not match (password is incorrect). This is common.")
    ///     }
    /// };
    /// #    Ok(())
    /// # }
    /// ```
    ///
    pub fn into_server(
        self,
        client_public_key: PublicKey,
        client_proof: [u8; PROOF_LENGTH as usize],
    ) -> Result<(SrpServer, [u8; PROOF_LENGTH as usize]), MatchProofsError> {
        let session_key = srp_internal::calculate_session_key(
            &client_public_key,
            &self.server_public_key,
            &self.password_verifier,
            &self.server_private_key,
        );

        let server_calculated_proof = srp_internal::calculate_client_proof(
            &self.username,
            &session_key,
            &client_public_key,
            &self.server_public_key,
            &self.salt,
        );

        let client_calculated_proof = Proof::from_le_bytes(client_proof);
        if client_calculated_proof != server_calculated_proof {
            return Err(MatchProofsError {
                client_proof: *client_calculated_proof.as_le(),
                server_proof: *server_calculated_proof.as_le(),
            });
        }

        let server_proof = srp_internal::calculate_server_proof(
            &client_public_key,
            &server_calculated_proof,
            &session_key,
        );

        let reconnect_challenge_data = ReconnectData::randomized();

        Ok((
            SrpServer {
                username: self.username,
                session_key,
                reconnect_challenge_data,
            },
            *server_proof.as_le(),
        ))
    }
}

/// The final step of authentication. Contains the session key, and reconnect logic.
///
/// This represents the final struct used in authentication, if this struct is constructed,
/// the client is correctly authenticated and should be allowed to connect to the server.
/// The session key is used internally in the struct for reconnection,
/// and externally for decryption of packets.
///
/// This struct should be saved in session storage to allow for clients to reconnect.
/// If a client disconnects and reconnects properly again, the old struct should be replaced with
/// the new one because the session key will be different.
///
/// Created from [`SrpProof::into_server`].
///
/// # Examples
///
/// ```rust
/// # use wow_srp::server::{SrpVerifier, SrpServer};
/// # use wow_srp::normalized_string::NormalizedString;
/// # use wow_srp::{PublicKey, PUBLIC_KEY_LENGTH, PROOF_LENGTH, RECONNECT_CHALLENGE_DATA_LENGTH};
/// # use std::collections::HashMap;
/// # fn test() {
/// let username = "Alice";
/// # let verifier = SrpVerifier::from_username_and_password(NormalizedString::new(username).unwrap(), NormalizedString::new("password123").unwrap());
/// # let proof = verifier.into_proof();
/// # let client_public_key = PublicKey::from_le_bytes(&[1u8; PUBLIC_KEY_LENGTH as usize]).unwrap();
/// # let client_proof = [1u8; PROOF_LENGTH as usize];
/// let mut authenticated_clients = HashMap::new();
///
/// // Server is created from unseen elements
/// let mut server = proof.into_server(client_public_key, client_proof);
///  let (server, server_proof) =  match server {
///      Ok(s) => {s}
///      Err(_) => return,
///  };
///
/// // Add the server to session storage, for example a HashMap
/// authenticated_clients.insert(username, server);
///
/// // Client drops connection and tries to reconnect
/// let client = authenticated_clients.get_mut(username);
/// let mut client = match client {
///     // Client does not have an active session, do not proceed
///     None => return,
///     // The username has an active session, but we still need to verify
///     // that the host connecting actually was the host that previously
///     // became authenticated.
///     Some(c) => c,
/// };
///
/// // Send this to the client
/// let server_challenge_data = client.reconnect_challenge_data();
///
/// // Get client data and proof in the response
/// # let client_challenge_data = [1u8; RECONNECT_CHALLENGE_DATA_LENGTH as usize];
/// # let client_proof = [1u8; PROOF_LENGTH as usize];
///
/// let should_allow_connection = client.verify_reconnection_attempt(client_challenge_data,
///                                                                  client_proof);
/// if should_allow_connection {
///     // The client has proven that it knows the session key,
///     // continue with the connection
/// } else {
///     // The client has proven nothing
///     // Send an error back or do nothing
/// }
/// # }
/// ```
pub struct SrpServer {
    username: NormalizedString,
    session_key: SessionKey,
    reconnect_challenge_data: ReconnectData,
}

impl SrpServer {
    /// Called `S` in [RFC2945](https://tools.ietf.org/html/rfc2945) and sometimes `K` or `key`
    /// in other literature.
    ///
    /// After successful authentication both client and server will have the exact same session
    /// key without onlookers being able to figure it out. This is used for decrypting packets later.
    ///
    /// The session key is always [40 bytes (320 bits)](SESSION_KEY_LENGTH) in length because it is
    /// created from 2 SHA-1 hashes of [20 bytes (160 bits)](PROOF_LENGTH).
    #[doc(alias = "K")]
    #[doc(alias = "S")]
    pub const fn session_key(&self) -> &[u8; SESSION_KEY_LENGTH as usize] {
        self.session_key.as_le()
    }

    /// Server data to be included in the reconnection challenge.
    ///
    /// Must be sent to the client as random data for the reconnect challenge.
    ///
    /// This is always [16 bytes](RECONNECT_CHALLENGE_DATA_LENGTH) since the field in the packet
    /// sent to the client has a fixed width.
    ///
    /// Not mentioned in [RFC2945](https://tools.ietf.org/html/rfc2945) at all.
    ///
    /// See [`verify_reconnection_attempt`](SrpServer::verify_reconnection_attempt) for more.
    pub const fn reconnect_challenge_data(
        &self,
    ) -> &[u8; RECONNECT_CHALLENGE_DATA_LENGTH as usize] {
        self.reconnect_challenge_data.as_le()
    }

    /// Computes a proof from the username, randomized client data, randomized server data,
    /// and session key.
    /// If the proofs match the client has the correct session key and should be allowed to reconnect.
    ///
    /// Returns true if the proof matches and false if it does not. A false return value does not mean
    /// that the [`SrpServer`] should be removed from the list of active sessions, since anybody could
    /// send the reconnect packages and get all the way here knowing only the username.
    ///
    /// After calling this the [`reconnect_challenge_data`](SrpServer::reconnect_challenge_data) has
    /// been randomized again and will need to be sent to the client, you can not keep using the same
    /// data for all reconnection attempts.
    ///
    /// It's important to note that a true value does not mean that the client is the same, it just
    /// means that the session key the reconnecting client has is the same as the one that was originally
    /// computed.
    ///
    /// Not mentioned in [RFC2945](https://tools.ietf.org/html/rfc2945) at all.
    pub fn verify_reconnection_attempt(
        &mut self,
        client_data: [u8; RECONNECT_CHALLENGE_DATA_LENGTH as usize],
        client_proof: [u8; PROOF_LENGTH as usize],
    ) -> bool {
        let server_proof = calculate_reconnect_proof(
            &self.username,
            &ReconnectData::from_le_bytes(client_data),
            &self.reconnect_challenge_data,
            &self.session_key,
        );

        let client_proof = Proof::from_le_bytes(client_proof);

        let reconnect_verified = server_proof == client_proof;

        self.reconnect_challenge_data.randomize_data();

        reconnect_verified
    }
}

#[cfg(test)]
mod test {

    use crate::key::{PrivateKey, Proof, PublicKey, Salt};
    use crate::normalized_string::NormalizedString;
    use crate::server::SrpVerifier;

    #[test]
    fn verify_known_client_values() {
        let server_private_key = PrivateKey::from_be_hex_str(
            "291BD2A76AAB9E7CDD702AFE1D07FDB316158BC2E4218FFDC32989AD3AF5026E",
        );

        let salt = Salt::from_be_hex_str(
            "65771e13b30bea9f4ef6c8390a594e297c9739e38ab02316bf1522ed5571813c",
        );

        let v = SrpVerifier::with_specific_salt(
            NormalizedString::new("A").unwrap(),
            NormalizedString::new("A").unwrap(),
            &salt,
        );
        let s = v.with_specific_private_key(server_private_key).unwrap();

        // Assert expects big endian
        let mut server_public_key = *s.server_public_key();
        server_public_key.reverse();
        assert_eq!(
            hex::encode(&server_public_key),
            "13ed2108a7c50c4aa451c05e3c8ba779c2201a9dbccc0841041c2466c5e24000"
        );

        assert_eq!(
            hex::encode(s.salt()),
            "3c817155ed2215bf1623b08ae339977c294e590a39c8f64e9fea0bb3131e7765"
        );

        let client_public_key = PublicKey::from_be_hex_str(
            "2e071e645d60721d15e8290dac4a3672d87045c14d2bdac52f1e6c998c7b7efa",
        )
        .unwrap();
        let client_proof = Proof::from_be_hex_str("b91e6e0c8c06969c44585d9f66d73454f60a43e6");

        let (_s, server_proof) = s
            .into_server(client_public_key, *client_proof.as_le())
            .unwrap();
        let server_proof = Proof::from_le_bytes(server_proof);

        assert_eq!(
            server_proof,
            Proof::from_be_hex_str("0e006885b6f27a1843043270d2c83c4e1a22780b")
        );
    }
}