diff --git a/native/cpp/CommonCpp/grpc/grpc_client/src/lib.rs b/native/cpp/CommonCpp/grpc/grpc_client/src/lib.rs --- a/native/cpp/CommonCpp/grpc/grpc_client/src/lib.rs +++ b/native/cpp/CommonCpp/grpc/grpc_client/src/lib.rs @@ -1,9 +1,9 @@ use lazy_static::lazy_static; use opaque_ke::{ ClientLogin, ClientLoginFinishParameters, ClientLoginStartParameters, - ClientRegistration, ClientRegistrationFinishParameters, - CredentialFinalization, CredentialRequest, CredentialResponse, - RegistrationResponse, RegistrationUpload, + ClientLoginStartResult, ClientRegistration, + ClientRegistrationFinishParameters, CredentialFinalization, + CredentialResponse, RegistrationResponse, RegistrationUpload, }; use rand::{rngs::OsRng, CryptoRng, Rng}; use std::sync::Arc; @@ -132,13 +132,9 @@ fn pake_registration_finish( rng: &mut (impl Rng + CryptoRng), registration_response_bytes: &[u8], - client_registration: Option>, + client_registration: ClientRegistration, ) -> Result, Status> { client_registration - .ok_or_else(|| { - error!("PAKE client_registration not found"); - Status::aborted("Registration not found") - })? .finish( rng, RegistrationResponse::deserialize(registration_response_bytes).map_err( @@ -159,8 +155,8 @@ fn pake_login_start( rng: &mut (impl Rng + CryptoRng), password: &str, -) -> Result<(CredentialRequest, Option>), Status> { - let client_login_start_result = ClientLogin::::start( +) -> Result, Status> { + ClientLogin::::start( rng, password.as_bytes(), ClientLoginStartParameters::default(), @@ -168,11 +164,7 @@ .map_err(|e| { error!("Failed to start PAKE login: {}", e); Status::failed_precondition("PAKE failure") - })?; - Ok(( - client_login_start_result.message, - Some(client_login_start_result.state), - )) + }) } fn pake_login_finish( @@ -206,3 +198,46 @@ error!("Received an unexpected message: {:?}", message); Status::invalid_argument("Invalid response data") } + +async fn handle_registration_response( + message: Option, + client_rng: &mut (impl Rng + CryptoRng), + client_registration: ClientRegistration, + password: &str, + tx: mpsc::Sender, +) -> Result, Status> { + if let Some(RegistrationResponseMessage { + data: Some(PakeRegistrationResponse(registration_response_bytes)), + .. + }) = message + { + let pake_registration_upload = pake_registration_finish( + client_rng, + ®istration_response_bytes, + client_registration, + )? + .serialize(); + let client_login_start_result = pake_login_start(client_rng, password)?; + let registration_request = RegistrationRequest { + data: Some(PakeRegistrationUploadAndCredentialRequest( + PakeRegistrationUploadAndCredentialRequestStruct { + pake_registration_upload, + pake_credential_request: client_login_start_result + .message + .serialize() + .map_err(|e| { + error!("Could not serialize credential request: {}", e); + Status::failed_precondition("PAKE failure") + })?, + }, + )), + }; + if let Err(e) = tx.send(registration_request).await { + error!("Response was dropped: {}", e); + return Err(Status::aborted("Dropped response")); + } + Ok(client_login_start_result.state) + } else { + Err(handle_unexpected_registration_response(message)) + } +}