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
// standard
use std::clone::Clone;
use std::convert::TryInto;
use std::net::TcpStream;

// extern crates
use bson::doc;
use bson::spec::BinarySubtype;
use bson::{Binary, Bson};
use honk_rpc::honk_rpc::{
    get_message_overhead, get_response_section_size, ApiSet, ErrorCode, RequestCookie, Session,
};
use rand::rngs::OsRng;
use rand::RngCore;
use tor_interface::tor_crypto::*;

// internal crates
use crate::ascii_string::*;
use crate::gosling::*;

//
// Identity Server
//

#[derive(thiserror::Error, Debug)]
pub enum Error {
    #[error("HonkRPC method failed: {0}")]
    HonkRPCFailure(#[from] honk_rpc::honk_rpc::Error),

    #[error("server is in invalid state: {0}")]
    InvalidState(String),

    #[error("incorrect usage: {0}")]
    IncorrectUsage(String),

    #[error("client sent invalid request")]
    BadClient,

    #[error("provided endpoint challenge too large; encoded size would be {0} but session's maximum honk-rpc message size is {1}")]
    EndpointChallengeTooLarge(usize, usize),
}

pub(crate) enum IdentityServerEvent {
    EndpointRequestReceived {
        client_service_id: V3OnionServiceId,
        requested_endpoint: AsciiString,
    },

    ChallengeResponseReceived {
        challenge_response: bson::document::Document,
    },

    HandshakeCompleted {
        endpoint_private_key: Ed25519PrivateKey,
        endpoint_name: AsciiString,
        client_service_id: V3OnionServiceId,
        client_auth_public_key: X25519PublicKey,
    },

    HandshakeRejected {
        // Client not on the block-list
        client_allowed: bool,
        // The requested endpoint is valid
        client_requested_endpoint_valid: bool,
        // The client proof is valid and signed with client's public key
        client_proof_signature_valid: bool,
        // The client authorization signature is valid
        client_auth_signature_valid: bool,
        // The challenge response is valid
        challenge_response_valid: bool,
    },
}

#[derive(Debug, PartialEq)]
enum IdentityServerState {
    // valid/expected states
    WaitingForBeginHandshake,
    GettingChallenge,
    ChallengeReady,
    WaitingForSendResponse,
    GettingChallengeVerification,
    ChallengeVerificationReady,
    ChallengeVerificationResponseSent,
    HandshakeComplete,
    // failure state
    HandshakeFailed,
}

pub(crate) struct IdentityServer {
    // Session Data
    rpc: Option<Session<TcpStream>>,
    server_identity: V3OnionServiceId,

    // State Machine Data
    state: IdentityServerState,
    begin_handshake_request_cookie: Option<RequestCookie>,
    client_identity: Option<V3OnionServiceId>,
    requested_endpoint: Option<AsciiString>,
    server_cookie: Option<ServerCookie>,
    endpoint_challenge: Option<bson::document::Document>,
    send_response_request_cookie: Option<RequestCookie>,
    client_auth_key: Option<X25519PublicKey>,
    challenge_response: Option<bson::document::Document>,
    endpoint_private_key: Option<Ed25519PrivateKey>,

    // Verification flags

    // Client not on the block-list
    client_allowed: bool,
    // The requested endpoint is valid
    client_requested_endpoint_valid: bool,
    // The client proof is valid and signed with client's public key
    client_proof_signature_valid: bool,
    // The client authorization signature is valid
    client_auth_signature_valid: bool,
    // The challenge response is valid
    challenge_response_valid: bool,
}

impl IdentityServer {
    fn get_state(&self) -> String {
        format!("{{ state: {:?}, begin_handshake_request_cookie: {:?}, client_identity: {:?}, requested_endpoint: {:?}, server_cookie: {:?}, endpoint_challenge: {:?}, send_response_request_cookie: {:?}, client_auth_key: {:?}, challenge_response: {:?}, endpoint_private_key: {:?} }}", self.state, self.begin_handshake_request_cookie, self.client_identity, self.requested_endpoint, self.server_cookie, self.endpoint_challenge, self.send_response_request_cookie, self.client_auth_key, self.challenge_response, self.endpoint_private_key)
    }

    pub fn new(rpc: Session<TcpStream>, server_identity: V3OnionServiceId) -> Self {
        IdentityServer {
            // Session Data
            rpc: Some(rpc),
            server_identity,

            // State Machine Data
            state: IdentityServerState::WaitingForBeginHandshake,
            begin_handshake_request_cookie: None,
            client_identity: None,
            requested_endpoint: None,
            server_cookie: None,
            endpoint_challenge: None,
            send_response_request_cookie: None,
            client_auth_key: None,
            challenge_response: None,
            endpoint_private_key: None,

            // Verification Flags
            client_allowed: false,
            client_requested_endpoint_valid: false,
            client_proof_signature_valid: false,
            client_auth_signature_valid: false,
            challenge_response_valid: false,
        }
    }

    pub fn update(&mut self) -> Result<Option<IdentityServerEvent>, Error> {
        // need to remove ownership of the HonkRPC session from Self
        // before being able to pass self into the session update method
        if let Some(mut rpc) = std::mem::take(&mut self.rpc) {
            match rpc.update(Some(&mut [self])) {
                Ok(()) => {
                    self.rpc = Some(rpc);
                }
                Err(err) => {
                    self.rpc = Some(rpc);
                    return Err(err.into());
                }
            }
        }

        match(&self.state,
              self.begin_handshake_request_cookie,
              self.client_identity.as_ref(),
              self.requested_endpoint.as_ref(),
              self.server_cookie.as_ref(),
              self.endpoint_challenge.as_ref(),
              self.send_response_request_cookie,
              self.client_auth_key.as_ref(),
              self.challenge_response.as_mut(),
              self.endpoint_private_key.as_ref()) {
            (&IdentityServerState::WaitingForBeginHandshake,
             None, // begin_handshake_request_cookie
             None, // client_identity
             None, // requested_endpoint
             None, // server_cookie
             None, // endpoint_challenge
             None, // send_response_request_cookie
             None, // client_auth_key
             None, // challenge_response
             None) // endpoint_private_key
            => {
                // no-op, waiting for client to connect and begin handshake
            },
            (&IdentityServerState::WaitingForBeginHandshake,
             Some(_begin_handshake_request_cookie),
             Some(client_identity),
             Some(requested_endpoint),
             None, // server_cookie
             None, // endpoint_challenge
             None, // send_response_request_cookie
             None, // client_auth_key
             None, // challenge_response
             None) // endpoint_private_key
            => {
                self.state = IdentityServerState::GettingChallenge;
                return Ok(Some(IdentityServerEvent::EndpointRequestReceived{client_service_id: client_identity.clone(), requested_endpoint: requested_endpoint.clone()}));
            },
            (&IdentityServerState::WaitingForSendResponse,
             Some(_begin_handshake_request_cookie),
             Some(_client_identity),
             Some(_requested_endpoint),
             Some(_server_cookie),
             Some(_endpoint_challenge),
             None, // send_response_request_cookie
             None, // client_auth_key
             None, // challenge_response
             None) // endpoint_private_key
            => {
                // no-op, waiting for client to send challenge response
            },
            (&IdentityServerState::WaitingForSendResponse,
             Some(_begin_handshake_request_cookie),
             Some(_client_identity),
             Some(_requested_endpoint),
             Some(_server_cookie),
             Some(_endpoint_challenge),
             Some(_send_response_request_cookie),
             Some(_client_auth_key),
             Some(challenge_response),
             None) // endpoint_private_key
            => {
                self.state = IdentityServerState::GettingChallengeVerification;
                return Ok(Some(IdentityServerEvent::ChallengeResponseReceived{
                    challenge_response: std::mem::take(challenge_response),
                }));
            },
            (&IdentityServerState::ChallengeVerificationResponseSent,
             Some(_begin_handshake_request_cookie),
             Some(client_identity),
             Some(requested_endpoint),
             Some(_server_cookie),
             Some(_endpoint_challenge),
             Some(_send_response_request_cookie),
             Some(client_auth_key),
             Some(_challenge_response),
             Some(endpoint_private_key))
            => {
                self.state = IdentityServerState::HandshakeComplete;
                return Ok(Some(IdentityServerEvent::HandshakeCompleted{
                    endpoint_private_key: endpoint_private_key.clone(),
                    endpoint_name: requested_endpoint.clone(),
                    client_service_id: client_identity.clone(),
                    client_auth_public_key: client_auth_key.clone(),
                }));
            },
            (&IdentityServerState::ChallengeVerificationResponseSent,
             Some(_begin_handshake_request_cookie),
             Some(_client_identity),
             Some(_requested_endpoint),
             Some(_server_cookie),
             Some(_endpoint_challenge),
             Some(_send_response_request_cookie),
             Some(_client_auth_key),
             Some(_challenge_response),
             None) // endpoint_private_key
            => {
                self.state = IdentityServerState::HandshakeComplete;
                return Ok(Some(IdentityServerEvent::HandshakeRejected{
                    client_allowed: self.client_allowed,
                    client_requested_endpoint_valid: self.client_requested_endpoint_valid,
                    client_proof_signature_valid: self.client_proof_signature_valid,
                    client_auth_signature_valid: self.client_auth_signature_valid,
                    challenge_response_valid: self.challenge_response_valid,
                }));
            },
             _ => {
                if self.state == IdentityServerState::HandshakeFailed {
                    return Err(Error::BadClient);
                } else {
                    return Err(Error::InvalidState(self.get_state()));
                }
            }
        }

        Ok(None)
    }

    pub fn handle_endpoint_request_received(
        &mut self,
        client_allowed: bool,
        endpoint_valid: bool,
        endpoint_challenge: bson::document::Document,
    ) -> Result<(), Error> {
        match (
            &self.state,
            self.rpc.as_ref(),
            self.begin_handshake_request_cookie,
            self.client_identity.as_ref(),
            self.requested_endpoint.as_ref(),
            self.server_cookie.as_ref(),
            self.endpoint_challenge.as_ref(),
            self.client_auth_key.as_ref(),
            self.challenge_response.as_ref(),
            self.endpoint_private_key.as_ref(),
        ) {
            (
                &IdentityServerState::GettingChallenge,
                Some(rpc),
                Some(_begin_handshake_request_cookie),
                Some(_client_identity),
                Some(_endpoint_name),
                None, // server_cookie
                None, // endpoint_challenge
                None, // client_auth_key
                None, // challenge_response
                None, // endpoint_private_key
            ) => {
                let mut server_cookie: ServerCookie = Default::default();
                OsRng.fill_bytes(&mut server_cookie);

                // calculate required size of response message and ensure if fits our
                // specified message size budget
                let result = doc!{
                    "server_cookie" : Bson::Binary(Binary{subtype: BinarySubtype::Generic, bytes: server_cookie.to_vec()}),
                    "endpoint_challenge" : endpoint_challenge.clone(),
                };
                let response_section_size = get_response_section_size(Some(Bson::Document(result)))?;
                let message_size = get_message_overhead()? + response_section_size;
                let max_message_size = rpc.get_max_message_size();
                if message_size > max_message_size {
                    Err(Error::EndpointChallengeTooLarge(message_size, max_message_size))
                } else {
                    self.server_cookie = Some(server_cookie);
                    self.endpoint_challenge = Some(endpoint_challenge);
                    self.client_allowed = client_allowed;
                    self.client_requested_endpoint_valid = endpoint_valid;
                    self.state = IdentityServerState::ChallengeReady;
                    Ok(())
                }
            }
            _ => {
                Err(Error::IncorrectUsage("handle_endpoint_request_received() may only be called after EndpointRequestReceived has been returned from update(), and it may only be called once".to_string()))
            }
        }
    }

    pub fn handle_challenge_response_received(
        &mut self,
        challenge_response_valid: bool,
    ) -> Result<(), Error> {
        match (
            &self.state,
            self.begin_handshake_request_cookie,
            self.client_identity.as_ref(),
            self.requested_endpoint.as_ref(),
            self.server_cookie.as_ref(),
            self.endpoint_challenge.as_ref(),
            self.client_auth_key.as_ref(),
            self.challenge_response.as_ref(),
            self.endpoint_private_key.as_ref(),
        ) {
            (
                &IdentityServerState::GettingChallengeVerification,
                Some(_begin_handshake_request_cookie),
                Some(_client_identity),
                Some(_requested_endpoint),
                Some(_server_cookie),
                Some(_endpoint_challenge),
                Some(_client_auth_key),
                Some(_challenge_response),
                None,
            ) =>
            // endpoint_private_key
            {
                self.challenge_response_valid = challenge_response_valid;
                self.state = IdentityServerState::ChallengeVerificationReady;
                Ok(())
            }
            _ => {
                Err(Error::IncorrectUsage("handle_challenge_response_received() may only be called after ChallengeResponseReceived event has been returned from update(), and it may only be called once".to_string()))
            }
        }
    }
}

impl ApiSet for IdentityServer {
    fn namespace(&self) -> &str {
        "gosling_identity"
    }

    fn exec_function(
        &mut self,
        name: &str,
        version: i32,
        mut args: bson::document::Document,
        request_cookie: Option<RequestCookie>,
    ) -> Option<Result<Option<bson::Bson>, ErrorCode>> {
        let request_cookie = match request_cookie {
            Some(request_cookie) => request_cookie,
            None => {
                return Some(Err(ErrorCode::Runtime(
                    RpcError::RequestCookieRequired as i32,
                )))
            }
        };

        match (
            name,
            version,
            &self.state,
            self.begin_handshake_request_cookie,
            self.client_identity.as_ref(),
            self.requested_endpoint.as_ref(),
            self.server_cookie.as_ref(),
            self.endpoint_challenge.as_ref(),
            self.client_auth_key.as_ref(),
            self.challenge_response.as_ref(),
            self.endpoint_private_key.as_ref(),
        ) {
            // handle begin_handshake call
            (
                "begin_handshake",
                0,
                &IdentityServerState::WaitingForBeginHandshake,
                None, // begin_handshake_request_cookie
                None, // client_identity
                None, // requested_endpoint
                None, // server_cookie
                None, // endpoint_challenge
                None, // client_auth_key
                None, // challenge_response
                None, // endpoint_private_key
            ) => {
                let valid_version = match args.remove("version") {
                    Some(Bson::String(value)) => value == GOSLING_PROTOCOL_VERSION,
                    _ => false,
                };
                if !valid_version {
                    self.state = IdentityServerState::HandshakeFailed;
                    return Some(Err(ErrorCode::Runtime(RpcError::BadVersion as i32)));
                }

                if let (Some(Bson::String(client_identity)), Some(Bson::String(endpoint_name))) =
                    (args.remove("client_identity"), args.remove("endpoint"))
                {
                    // client_identiity
                    let client_identity = match V3OnionServiceId::from_string(&client_identity) {
                        Ok(client_identity) => client_identity,
                        Err(_) => {
                            self.state = IdentityServerState::HandshakeFailed;
                            return Some(Err(ErrorCode::Runtime(RpcError::InvalidArg as i32)));
                        }
                    };

                    // endpoint name
                    let endpoint_name = match AsciiString::new(endpoint_name) {
                        Ok(endpoint_name) => endpoint_name,
                        Err(_) => {
                            self.state = IdentityServerState::HandshakeFailed;
                            return Some(Err(ErrorCode::Runtime(RpcError::InvalidArg as i32)));
                        }
                    };

                    // save cookie
                    self.begin_handshake_request_cookie = Some(request_cookie);

                    // save results
                    self.client_identity = Some(client_identity);
                    self.requested_endpoint = Some(endpoint_name);
                    None
                } else {
                    self.state = IdentityServerState::HandshakeFailed;
                    Some(Err(ErrorCode::Runtime(RpcError::InvalidArg as i32)))
                }
            }
            // handle send_response call
            (
                "send_response",
                0,
                &IdentityServerState::WaitingForSendResponse,
                Some(_begin_handshake_request_cookie),
                Some(client_identity),
                Some(requested_endpoint),
                Some(server_cookie),
                Some(_endpoint_challenge),
                None, // client_auth_key
                None, // challenge_response
                None, // endpoint_private_key
            ) => {
                // arg validation
                if let (
                    Some(Bson::Binary(Binary {
                        subtype: BinarySubtype::Generic,
                        bytes: client_cookie,
                    })),
                    Some(Bson::Binary(Binary {
                        subtype: BinarySubtype::Generic,
                        bytes: client_identity_proof_signature,
                    })),
                    Some(Bson::Binary(Binary {
                        subtype: BinarySubtype::Generic,
                        bytes: client_authorization_key,
                    })),
                    Some(Bson::Boolean(client_authorization_key_signbit)),
                    Some(Bson::Binary(Binary {
                        subtype: BinarySubtype::Generic,
                        bytes: client_authorization_signature,
                    })),
                    Some(Bson::Document(challenge_response)),
                ) = (
                    args.remove("client_cookie"),
                    args.remove("client_identity_proof_signature"),
                    args.remove("client_authorization_key"),
                    args.remove("client_authorization_key_signbit"),
                    args.remove("client_authorization_signature"),
                    args.remove("challenge_response"),
                ) {
                    // client_cookie
                    let client_cookie: ClientCookie = match client_cookie.try_into() {
                        Ok(client_cookie) => client_cookie,
                        Err(_) => {
                            self.state = IdentityServerState::HandshakeFailed;
                            return Some(Err(ErrorCode::Runtime(RpcError::InvalidArg as i32)));
                        }
                    };

                    // client_identity_proof_signature
                    let client_identity_proof_signature: [u8; ED25519_SIGNATURE_SIZE] =
                        match client_identity_proof_signature.try_into() {
                            Ok(client_identity_proof_signature) => client_identity_proof_signature,
                            Err(_) => {
                                self.state = IdentityServerState::HandshakeFailed;
                                return Some(Err(ErrorCode::Runtime(RpcError::InvalidArg as i32)));
                            }
                        };
                    let client_identity_proof_signature =
                        match Ed25519Signature::from_raw(&client_identity_proof_signature) {
                            Ok(client_identity_proof_signature) => client_identity_proof_signature,
                            Err(_) => {
                                self.state = IdentityServerState::HandshakeFailed;
                                return Some(Err(ErrorCode::Runtime(RpcError::InvalidArg as i32)));
                            }
                        };

                    // client_authorization_key
                    let client_authorization_key: [u8; X25519_PUBLIC_KEY_SIZE] =
                        match client_authorization_key.try_into() {
                            Ok(client_authorization_key) => client_authorization_key,
                            Err(_) => {
                                self.state = IdentityServerState::HandshakeFailed;
                                return Some(Err(ErrorCode::Runtime(RpcError::InvalidArg as i32)));
                            }
                        };
                    let client_authorization_key =
                        X25519PublicKey::from_raw(&client_authorization_key);

                    // client_authorization_key_signbit
                    let client_authorization_key_signbit: SignBit =
                        client_authorization_key_signbit.into();

                    // client_authorization_signature
                    let client_authorization_signature: [u8; ED25519_SIGNATURE_SIZE] =
                        match client_authorization_signature.try_into() {
                            Ok(client_authorization_signature) => client_authorization_signature,
                            Err(_) => {
                                self.state = IdentityServerState::HandshakeFailed;
                                return Some(Err(ErrorCode::Runtime(RpcError::InvalidArg as i32)));
                            }
                        };
                    let client_authorization_signature =
                        match Ed25519Signature::from_raw(&client_authorization_signature) {
                            Ok(client_authorization_signature) => client_authorization_signature,
                            Err(_) => {
                                self.state = IdentityServerState::HandshakeFailed;
                                return Some(Err(ErrorCode::Runtime(RpcError::InvalidArg as i32)));
                            }
                        };

                    // save  cookie
                    self.send_response_request_cookie = Some(request_cookie);

                    // convert client_identity to client's public ed25519 key
                    if let Ok(client_identity_key) =
                        Ed25519PublicKey::from_service_id(client_identity)
                    {
                        // construct + verify client proof
                        let client_proof = build_client_proof(
                            DomainSeparator::GoslingIdentity,
                            requested_endpoint,
                            client_identity,
                            &self.server_identity,
                            &client_cookie,
                            server_cookie,
                        );
                        self.client_proof_signature_valid = client_identity_proof_signature
                            .verify(&client_proof, &client_identity_key);
                    }

                    // evaluate the client authorization signature
                    self.client_auth_signature_valid = client_authorization_signature
                        .verify_x25519(
                            client_identity.as_bytes(),
                            &client_authorization_key,
                            client_authorization_key_signbit,
                        );

                    // save off client auth key for future endpoint generation
                    self.client_auth_key = Some(client_authorization_key);

                    // safe off challenge response for verification
                    self.challenge_response = Some(challenge_response);

                    None
                } else {
                    self.state = IdentityServerState::HandshakeFailed;
                    Some(Err(ErrorCode::Runtime(RpcError::InvalidArg as i32)))
                }
            }
            _ => {
                self.state = IdentityServerState::HandshakeFailed;
                Some(Err(ErrorCode::Runtime(RpcError::Failure as i32)))
            }
        }
    }

    fn next_result(&mut self) -> Option<(RequestCookie, Result<Option<bson::Bson>, ErrorCode>)> {
        match (
            &self.state,
            self.begin_handshake_request_cookie,
            self.client_identity.as_ref(),
            self.requested_endpoint.as_ref(),
            self.server_cookie.as_ref(),
            self.endpoint_challenge.as_mut(),
            self.send_response_request_cookie,
            self.client_auth_key.as_ref(),
            self.challenge_response.as_ref(),
        ) {
            // return challenge from begin_handshake
            (
                &IdentityServerState::ChallengeReady,
                Some(begin_handshake_request_cookie),
                Some(_client_identity),
                Some(_requested_endpoint),
                Some(server_cookie),
                Some(endpoint_challenge),
                None, // send_response_request_cookie
                None, // client_auth_key
                None,
            ) =>
            // challenge_response
            {
                self.state = IdentityServerState::WaitingForSendResponse;
                Some((
                    begin_handshake_request_cookie,
                    Ok(Some(Bson::Document(doc! {
                        "server_cookie" : Bson::Binary(Binary{subtype: BinarySubtype::Generic, bytes: server_cookie.to_vec()}),
                        "endpoint_challenge" : std::mem::take(endpoint_challenge),
                    }))),
                ))
            }
            (&IdentityServerState::ChallengeReady, _, _, _, _, _, _, _, _) => unreachable!(),
            (
                &IdentityServerState::ChallengeVerificationReady,
                Some(_begin_handshake_request_cookie),
                Some(_client_identity),
                Some(_requested_endpoint),
                Some(_server_cookie),
                Some(_endpoint_challenge),
                Some(send_response_request_cookie),
                Some(_client_auth_key),
                Some(_challenge_response),
            ) => {
                let mut success = true;
                success &= self.client_allowed;
                success &= self.client_requested_endpoint_valid;
                success &= self.client_proof_signature_valid;
                success &= self.client_auth_signature_valid;
                success &= self.challenge_response_valid;

                self.state = IdentityServerState::ChallengeVerificationResponseSent;
                if success {
                    let endpoint_private_key = Ed25519PrivateKey::generate();
                    let endpoint_service_id =
                        V3OnionServiceId::from_private_key(&endpoint_private_key);
                    self.endpoint_private_key = Some(endpoint_private_key);
                    Some((
                        send_response_request_cookie,
                        Ok(Some(Bson::String(endpoint_service_id.to_string()))),
                    ))
                } else {
                    Some((
                        send_response_request_cookie,
                        Err(ErrorCode::Runtime(RpcError::Failure as i32)),
                    ))
                }
            }
            _ => None,
        }
    }
}