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
pub mod sodiumoxide; use self::sodiumoxide::{ SodiumOxidePublicAsymmetricKey, SodiumOxidePublicAsymmetricKeyBuilder, SodiumOxideSecretAsymmetricKey, SodiumOxideSecretAsymmetricKeyBuilder, SodiumOxideSymmetricKey, SodiumOxideSymmetricKeyBuilder, }; use crate::{ Builder, ByteSource, ByteUnsealable, CryptoError, EntryPath, HasBuilder, HasByteSource, HasIndex, SymmetricNonce, TypeBuilder, TypeBuilderContainer, }; use mongodb::bson::{self, Document}; use serde::{Deserialize, Serialize}; use std::convert::TryFrom; pub trait SymmetricSealer { type SealedOutput; type Nonce; fn seal( &self, plaintext: ByteSource, nonce: Option<&Self::Nonce>, key_path: Option<EntryPath>, ) -> Result<Self::SealedOutput, CryptoError>; } pub trait SymmetricUnsealer { type UnsealedOutput; type Nonce; fn unseal( &self, ciphertext: ByteSource, nonce: &Self::Nonce, key_path: Option<EntryPath>, ) -> Result<Self::UnsealedOutput, CryptoError>; } pub trait SecretAsymmetricSealer { type SealedOutput; type Nonce; type PublicKey; fn seal( &self, plaintext: ByteSource, public_key: Option<&Self::PublicKey>, nonce: Option<&Self::Nonce>, key_path: Option<EntryPath>, ) -> Result<Self::SealedOutput, CryptoError>; } pub trait SecretAsymmetricUnsealer { type UnsealedOutput; type Nonce; type PublicKey; fn unseal( &self, ciphertext: ByteSource, public_key: Option<&Self::PublicKey>, nonce: &Self::Nonce, key_path: Option<EntryPath>, ) -> Result<Self::UnsealedOutput, CryptoError>; } pub trait PublicAsymmetricSealer { type SealedOutput; type Nonce; type SecretKey; fn seal( &self, plaintext: ByteSource, secret_key: &Self::SecretKey, nonce: Option<&Self::Nonce>, key_path: Option<EntryPath>, ) -> Result<Self::SealedOutput, CryptoError>; } pub trait PublicAsymmetricUnsealer { type UnsealedOutput; type Nonce; type SecretKey; fn unseal( &self, ciphertext: ByteSource, secret_key: &Self::SecretKey, nonce: &Self::Nonce, key_path: Option<EntryPath>, ) -> Result<Self::UnsealedOutput, CryptoError>; } #[derive(Serialize, Deserialize, Debug, Clone)] #[serde(tag = "t", content = "c")] pub enum Key { Symmetric(SymmetricKey), Asymmetric(AsymmetricKey), } impl HasIndex for Key { type Index = Document; fn get_index() -> Option<Self::Index> { Some(bson::doc! { "c": { "builder": { "t": "Key" } } }) } } impl HasBuilder for Key { type Builder = KeyBuilder; fn builder(&self) -> Self::Builder { match self { Self::Symmetric(sk) => KeyBuilder::Symmetric(sk.builder()), Self::Asymmetric(ak) => KeyBuilder::Asymmetric(ak.builder()), } } } impl HasByteSource for Key { fn byte_source(&self) -> ByteSource { match self { Self::Symmetric(sk) => sk.byte_source(), Self::Asymmetric(ak) => ak.byte_source(), } } } #[derive(Serialize, Deserialize, Debug, Copy, Clone)] #[serde(tag = "t", content = "c")] pub enum KeyBuilder { Symmetric(SymmetricKeyBuilder), Asymmetric(AsymmetricKeyBuilder), } impl TryFrom<TypeBuilderContainer> for KeyBuilder { type Error = CryptoError; fn try_from(builder: TypeBuilderContainer) -> Result<Self, Self::Error> { match builder.0 { TypeBuilder::Key(kb) => Ok(kb), _ => Err(CryptoError::NotDowncastable), } } } impl From<KeyBuilder> for TypeBuilder { fn from(kb: KeyBuilder) -> TypeBuilder { TypeBuilder::Key(kb) } } impl Builder for KeyBuilder { type Output = Key; fn build(&self, bytes: Option<&[u8]>) -> Result<Self::Output, CryptoError> { match self { Self::Symmetric(sk) => Ok(Key::Symmetric(sk.build(bytes)?)), Self::Asymmetric(ak) => Ok(Key::Asymmetric(ak.build(bytes)?)), } } } #[derive(Serialize, Deserialize, Debug, Clone)] #[serde(tag = "t", content = "c")] pub enum SymmetricKey { SodiumOxide(SodiumOxideSymmetricKey), } impl SymmetricSealer for SymmetricKey { type SealedOutput = ByteUnsealable; type Nonce = SymmetricNonce; fn seal( &self, plaintext: ByteSource, nonce: Option<&Self::Nonce>, path: Option<EntryPath>, ) -> Result<Self::SealedOutput, CryptoError> { match self { Self::SodiumOxide(sosk) => { let nonce = match nonce { Some(n) => match n { SymmetricNonce::SodiumOxide(sosn) => Ok::<_, CryptoError>(Some(sosn)), }, None => Ok(None), }?; Ok(ByteUnsealable::SodiumOxideSymmetricKey( sosk.seal(plaintext, nonce, path)?, )) } } } } impl HasIndex for SymmetricKey { type Index = Document; fn get_index() -> Option<Self::Index> { Some(bson::doc! { "c": { "builder": { "t": "Key", "c": { "t": "Symmetric" } } } }) } } impl HasBuilder for SymmetricKey { type Builder = SymmetricKeyBuilder; fn builder(&self) -> Self::Builder { match self { Self::SodiumOxide(sosk) => SymmetricKeyBuilder::SodiumOxide(sosk.builder()), } } } impl HasByteSource for SymmetricKey { fn byte_source(&self) -> ByteSource { match self { Self::SodiumOxide(sosk) => sosk.byte_source(), } } } #[derive(Serialize, Deserialize, Debug, Copy, Clone)] #[serde(tag = "t", content = "c")] pub enum SymmetricKeyBuilder { SodiumOxide(SodiumOxideSymmetricKeyBuilder), } impl TryFrom<TypeBuilderContainer> for SymmetricKeyBuilder { type Error = CryptoError; fn try_from(builder: TypeBuilderContainer) -> Result<Self, Self::Error> { match builder.0 { TypeBuilder::Key(KeyBuilder::Symmetric(skb)) => Ok(skb), _ => Err(CryptoError::NotDowncastable), } } } impl From<SymmetricKeyBuilder> for TypeBuilder { fn from(skb: SymmetricKeyBuilder) -> TypeBuilder { TypeBuilder::Key(KeyBuilder::Symmetric(skb)) } } impl Builder for SymmetricKeyBuilder { type Output = SymmetricKey; fn build(&self, bytes: Option<&[u8]>) -> Result<Self::Output, CryptoError> { match self { Self::SodiumOxide(soskb) => Ok(SymmetricKey::SodiumOxide(soskb.build(bytes)?)), } } } #[derive(Serialize, Deserialize, Debug, Clone)] #[serde(tag = "t", content = "c")] pub enum AsymmetricKey { Public(PublicAsymmetricKey), Secret(SecretAsymmetricKey), } impl HasIndex for AsymmetricKey { type Index = Document; fn get_index() -> Option<Self::Index> { Some(bson::doc! { "c": { "builder": { "t": "Key", "c": { "t": "Asymmetric", } } } }) } } impl HasBuilder for AsymmetricKey { type Builder = AsymmetricKeyBuilder; fn builder(&self) -> Self::Builder { match self { Self::Public(pak) => AsymmetricKeyBuilder::Public(pak.builder()), Self::Secret(sak) => AsymmetricKeyBuilder::Secret(sak.builder()), } } } impl HasByteSource for AsymmetricKey { fn byte_source(&self) -> ByteSource { match self { Self::Public(pak) => pak.byte_source(), Self::Secret(sak) => sak.byte_source(), } } } #[derive(Serialize, Deserialize, Debug, Copy, Clone)] #[serde(tag = "t", content = "c")] pub enum AsymmetricKeyBuilder { Public(PublicAsymmetricKeyBuilder), Secret(SecretAsymmetricKeyBuilder), } impl TryFrom<TypeBuilderContainer> for AsymmetricKeyBuilder { type Error = CryptoError; fn try_from(builder: TypeBuilderContainer) -> Result<Self, Self::Error> { match builder.0 { TypeBuilder::Key(KeyBuilder::Asymmetric(akb)) => Ok(akb), _ => Err(CryptoError::NotDowncastable), } } } impl From<AsymmetricKeyBuilder> for TypeBuilder { fn from(akb: AsymmetricKeyBuilder) -> TypeBuilder { TypeBuilder::Key(KeyBuilder::Asymmetric(akb)) } } impl Builder for AsymmetricKeyBuilder { type Output = AsymmetricKey; fn build(&self, bytes: Option<&[u8]>) -> Result<Self::Output, CryptoError> { match self { Self::Public(pakb) => Ok(AsymmetricKey::Public(pakb.build(bytes)?)), Self::Secret(sakb) => Ok(AsymmetricKey::Secret(sakb.build(bytes)?)), } } } #[derive(Serialize, Deserialize, Debug, Clone)] #[serde(tag = "t", content = "c")] pub enum PublicAsymmetricKey { SodiumOxide(SodiumOxidePublicAsymmetricKey), } impl HasIndex for PublicAsymmetricKey { type Index = Document; fn get_index() -> Option<Self::Index> { Some(bson::doc! { "c": { "builder": { "t": "Key", "c": { "t": "Asymmetric", "c": { "t": "Public" } } } } }) } } impl HasBuilder for PublicAsymmetricKey { type Builder = PublicAsymmetricKeyBuilder; fn builder(&self) -> Self::Builder { match self { Self::SodiumOxide(sopak) => PublicAsymmetricKeyBuilder::SodiumOxide(sopak.builder()), } } } impl HasByteSource for PublicAsymmetricKey { fn byte_source(&self) -> ByteSource { match self { Self::SodiumOxide(sopak) => sopak.byte_source(), } } } #[derive(Serialize, Deserialize, Debug, Copy, Clone)] #[serde(tag = "t", content = "c")] pub enum PublicAsymmetricKeyBuilder { SodiumOxide(SodiumOxidePublicAsymmetricKeyBuilder), } impl TryFrom<TypeBuilderContainer> for PublicAsymmetricKeyBuilder { type Error = CryptoError; fn try_from(builder: TypeBuilderContainer) -> Result<Self, Self::Error> { match builder.0 { TypeBuilder::Key(KeyBuilder::Asymmetric(AsymmetricKeyBuilder::Public(pakb))) => { Ok(pakb) } _ => Err(CryptoError::NotDowncastable), } } } impl From<PublicAsymmetricKeyBuilder> for TypeBuilder { fn from(pakb: PublicAsymmetricKeyBuilder) -> TypeBuilder { TypeBuilder::Key(KeyBuilder::Asymmetric(AsymmetricKeyBuilder::Public(pakb))) } } impl Builder for PublicAsymmetricKeyBuilder { type Output = PublicAsymmetricKey; fn build(&self, bytes: Option<&[u8]>) -> Result<Self::Output, CryptoError> { match self { Self::SodiumOxide(sopakb) => Ok(PublicAsymmetricKey::SodiumOxide(sopakb.build(bytes)?)), } } } #[derive(Serialize, Deserialize, Debug, Clone)] #[serde(tag = "t", content = "c")] pub enum SecretAsymmetricKey { SodiumOxide(SodiumOxideSecretAsymmetricKey), } impl HasIndex for SecretAsymmetricKey { type Index = Document; fn get_index() -> Option<Self::Index> { Some(bson::doc! { "c": { "builder": { "t": "Key", "c": { "t": "Asymmetric", "c": { "t": "Secret" } } } } }) } } impl HasBuilder for SecretAsymmetricKey { type Builder = SecretAsymmetricKeyBuilder; fn builder(&self) -> Self::Builder { match self { Self::SodiumOxide(sosak) => SecretAsymmetricKeyBuilder::SodiumOxide(sosak.builder()), } } } impl HasByteSource for SecretAsymmetricKey { fn byte_source(&self) -> ByteSource { match self { Self::SodiumOxide(sosak) => sosak.byte_source(), } } } #[derive(Serialize, Deserialize, Debug, Copy, Clone)] #[serde(tag = "t", content = "c")] pub enum SecretAsymmetricKeyBuilder { SodiumOxide(SodiumOxideSecretAsymmetricKeyBuilder), } impl TryFrom<TypeBuilderContainer> for SecretAsymmetricKeyBuilder { type Error = CryptoError; fn try_from(builder: TypeBuilderContainer) -> Result<Self, Self::Error> { match builder.0 { TypeBuilder::Key(KeyBuilder::Asymmetric(AsymmetricKeyBuilder::Secret(sakb))) => { Ok(sakb) } _ => Err(CryptoError::NotDowncastable), } } } impl From<SecretAsymmetricKeyBuilder> for TypeBuilder { fn from(sakb: SecretAsymmetricKeyBuilder) -> TypeBuilder { TypeBuilder::Key(KeyBuilder::Asymmetric(AsymmetricKeyBuilder::Secret(sakb))) } } impl Builder for SecretAsymmetricKeyBuilder { type Output = SecretAsymmetricKey; fn build(&self, bytes: Option<&[u8]>) -> Result<Self::Output, CryptoError> { match self { Self::SodiumOxide(sosakb) => Ok(SecretAsymmetricKey::SodiumOxide(sosakb.build(bytes)?)), } } }