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
#![allow(clippy::module_name_repetitions)]

//! OPAQUE client side handling.

use opaque_ke::errors::ProtocolError;
use serde::{Deserialize, Serialize};

use crate::{
	cipher_suite, Config, Error, ExportKey, LoginFinalization, LoginRequest, LoginResponse,
	PublicKey, RegistrationFinalization, RegistrationRequest, RegistrationResponse, Result,
};

/// Client configuration.
#[derive(Clone, Copy, Debug, Default, Deserialize, Eq, Hash, PartialEq, Serialize)]
pub struct ClientConfig {
	/// Common config.
	config: Config,
	/// Server key pair.
	public_key: Option<PublicKey>,
}

impl ClientConfig {
	/// Create a new [`ClientConfig`].
	///
	/// A [`PublicKey`] can be used to ensure the servers identity
	/// during registration or login. If no [`PublicKey`] could be obtained
	/// beforehand, it can be retrieved after successful registration or login.
	///
	/// # Errors
	/// [`Error::Config`] if [`PublicKey`] was not created with the same
	/// [`Config`].
	pub fn new(config: Config, public_key: Option<PublicKey>) -> Result<Self> {
		if let Some(public_key) = public_key {
			if public_key.config != config {
				return Err(Error::Config);
			}
		}

		Ok(Self { config, public_key })
	}

	/// Returns the [`Config`] associated with this [`ClientConfig`].
	#[must_use]
	pub const fn config(&self) -> Config {
		self.config
	}

	/// Returns the [`PublicKey`] associated with this [`ClientConfig`].
	#[must_use]
	pub const fn public_key(&self) -> Option<PublicKey> {
		self.public_key
	}
}

/// Holds the state of a registration process. See [`register`](Self::register).
#[must_use = "Use `finish()` to complete the registration process"]
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct ClientRegistration {
	/// [`ClientConfig`] of this [`ClientRegistration`].
	config: ClientConfig,
	/// Client registration state.
	state: cipher_suite::ClientRegistration,
}

impl ClientRegistration {
	/// Returns the [`ClientConfig`] associated with this
	/// [`ClientRegistration`].
	#[must_use]
	pub const fn config(&self) -> ClientConfig {
		self.config
	}

	/// Starts the registration process. The returned [`RegistrationRequest`]
	/// has to be send to the server to drive the registration process. See
	/// [`ServerRegistration::register()`](crate::ServerRegistration::register).
	///
	/// # Errors
	/// [`Error::Opaque`] on internal OPAQUE error.
	pub fn register<P: AsRef<[u8]>>(
		config: ClientConfig,
		password: P,
	) -> Result<(Self, RegistrationRequest)> {
		let (state, message) = cipher_suite::ClientRegistration::register(
			config.config.cipher_suite,
			password.as_ref(),
		)?;

		Ok((Self { config, state }, RegistrationRequest {
			config: config.config,
			message,
		}))
	}

	/// Finishes the registration process. The returned
	/// [`RegistrationFinalization`] has to be send back to the server to finish
	/// the registration process. See
	/// [`ServerRegistration::finish()`](crate::ServerRegistration::finish).
	///
	/// [`ClientFile`] can be used to validate the server during login. See
	/// [`ClientLogin::login()`].
	///
	/// [`ExportKey`] can be used to encrypt data and store it safely on
	/// the server. See [`ExportKey`] for more details.
	///
	/// # Errors
	/// - [`Error::Config`] if [`ClientRegistration`] and
	///   [`RegistrationResponse`] were not created with the same [`Config`]
	/// - [`Error::InvalidServer`] if the public key given in
	///   [`register()`](Self::register) does not match the servers public key
	/// - [`Error::Opaque`] on internal OPAQUE error
	pub fn finish(
		self,
		response: RegistrationResponse,
	) -> Result<(ClientFile, RegistrationFinalization, ExportKey)> {
		if self.config.config != response.config {
			return Err(Error::Config);
		}

		let (message, new_public_key, export_key) = self
			.state
			.finish(response.message, &self.config.config.mhf().to_slow_hash())?;

		let public_key = if let Some(public_key) = self.config.public_key {
			if public_key.key != new_public_key {
				return Err(Error::InvalidServer);
			}

			public_key
		} else {
			PublicKey::new(self.config.config, new_public_key)
		};

		Ok((
			ClientFile(public_key),
			RegistrationFinalization {
				config: self.config.config,
				message,
			},
			ExportKey::new(export_key),
		))
	}
}

/// Use this to enable server validation during login.
#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
pub struct ClientFile(PublicKey);

impl ClientFile {
	/// Returns the [`Config`] associated with this [`ClientFile`].
	#[must_use]
	pub const fn config(&self) -> Config {
		self.0.config
	}

	/// Returns the servers [`PublicKey`] associated with this [`ClientFile`].
	#[must_use]
	pub const fn public_key(&self) -> PublicKey {
		self.0
	}
}

/// Holds the state of a login process. See [`login`](Self::login).
#[must_use = "Does nothing if not `finish`ed"]
#[derive(Clone, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
pub struct ClientLogin {
	/// [`ClientConfig`] of this [`ClientLogin`].
	config: ClientConfig,
	/// Client login state.
	state: cipher_suite::ClientLogin,
}

impl ClientLogin {
	/// Returns the [`ClientConfig`] associated with this [`ClientLogin`].
	#[must_use]
	pub const fn config(&self) -> ClientConfig {
		self.config
	}

	/// Starts the login process. The returned [`LoginRequest`] has to be send
	/// to the server to drive the login process. See
	/// [`ServerLogin::login()`](crate::ServerLogin::login).
	///
	/// If a [`ClientFile`] was stored during registration, it can validate the
	/// server when passed.
	///
	/// # Errors
	/// - [`Error::Config`] if [`ClientConfig`] and [`ClientFile`] were not
	///   created with the same [`Config`]
	/// - [`Error::ConfigPublicKey`] if [`PublicKey`] in [`ClientConfig`] and
	///   [`ClientFile`] don't match
	/// - [`Error::Opaque`] on internal OPAQUE error
	pub fn login<P: AsRef<[u8]>>(
		mut config: ClientConfig,
		file: Option<ClientFile>,
		password: P,
	) -> Result<(Self, LoginRequest)> {
		if let Some(file) = &file {
			if file.0.config != config.config {
				return Err(Error::Config);
			}

			if let Some(public_key) = config.public_key {
				if public_key != file.0 {
					return Err(Error::ConfigPublicKey);
				}
			} else {
				config.public_key = Some(file.0);
			}
		}

		let (state, message) =
			cipher_suite::ClientLogin::login(config.config.cipher_suite, password.as_ref())?;

		Ok((Self { config, state }, LoginRequest {
			config: config.config,
			message,
		}))
	}

	/// Finishes the login process. The returned [`LoginFinalization`] has to be
	/// send back to the server to finish the login process.
	///
	/// [`ClientFile`] can be used to validate the server during the next login.
	/// See [`login()`](Self::login).
	///
	/// [`ExportKey`] can be used to encrypt data and store it on safely on
	/// the server. See [`ExportKey`] for more details.
	///
	/// # Errors
	/// - [`Error::Config`] if [`ClientLogin`] and [`LoginResponse`] were not
	///   created with the same [`Config`]
	/// - [`Error::Credentials`] if credentials don't match
	/// - [`Error::InvalidServer`] if the public key given in
	///   [`login()`](Self::login) does not match the servers public key
	/// - [`Error::Opaque`] on internal OPAQUE error
	pub fn finish(
		self,
		response: LoginResponse,
	) -> Result<(ClientFile, LoginFinalization, ExportKey)> {
		if self.config.config != response.config {
			return Err(Error::Config);
		}

		let (message, new_public_key, export_key) = match self
			.state
			.finish(response.message, &self.config.config.mhf().to_slow_hash())
		{
			Ok(result) => result,
			Err(Error::Opaque(ProtocolError::InvalidLoginError)) => return Err(Error::Credentials),
			Err(error) => return Err(error),
		};

		let public_key = if let Some(public_key) = self.config.public_key {
			if public_key.key != new_public_key {
				return Err(Error::InvalidServer);
			}

			public_key
		} else {
			PublicKey::new(self.config.config, new_public_key)
		};

		Ok((
			ClientFile(public_key),
			LoginFinalization {
				config: self.config.config,
				message,
			},
			ExportKey::new(export_key),
		))
	}
}