Skip to content

Commit

Permalink
Switched to Brotli compression algo and refactored key exchange utili…
Browse files Browse the repository at this point in the history
…ty into IKeyExchange/KeyExchange (impl) for e.z. IoC.
  • Loading branch information
GlitchedPolygons committed Jan 12, 2020
1 parent e3b991d commit 2ddad00
Show file tree
Hide file tree
Showing 53 changed files with 312 additions and 132 deletions.
2 changes: 1 addition & 1 deletion src/Extensions/StringExtensions.cs
@@ -1,6 +1,6 @@
/*
Glitched Epistle - Client
Copyright (C) 2019 Raphael Beck
Copyright (C) 2020 Raphael Beck
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Expand Down
4 changes: 2 additions & 2 deletions src/GlitchedEpistle.Client.csproj
Expand Up @@ -50,9 +50,9 @@
<ItemGroup>
<PackageReference Include="GlitchedPolygons.ExtensionMethods" Version="1.0.6" />
<PackageReference Include="GlitchedPolygons.RepositoryPattern.SQLite" Version="1.0.2" />
<PackageReference Include="GlitchedPolygons.Services.CompressionUtility" Version="1.1.0" />
<PackageReference Include="GlitchedPolygons.Services.CompressionUtility" Version="1.2.0" />
<PackageReference Include="GlitchedPolygons.Services.Cryptography.Asymmetric" Version="1.0.1" />
<PackageReference Include="GlitchedPolygons.Services.Cryptography.Symmetric" Version="1.0.0" />
<PackageReference Include="GlitchedPolygons.Services.Cryptography.Symmetric" Version="1.0.1" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
<PackageReference Include="RestSharp" Version="106.6.10" />
</ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/Models/Convo.cs
@@ -1,6 +1,6 @@
/*
Glitched Epistle - Client
Copyright (C) 2019 Raphael Beck
Copyright (C) 2020 Raphael Beck
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Expand Down
2 changes: 1 addition & 1 deletion src/Models/DTOs/ConvoChangeMetadataRequestDto.cs
@@ -1,6 +1,6 @@
/*
Glitched Epistle - Client
Copyright (C) 2019 Raphael Beck
Copyright (C) 2020 Raphael Beck
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Expand Down
2 changes: 1 addition & 1 deletion src/Models/DTOs/ConvoCreationRequestDto.cs
@@ -1,6 +1,6 @@
/*
Glitched Epistle - Client
Copyright (C) 2019 Raphael Beck
Copyright (C) 2020 Raphael Beck
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Expand Down
2 changes: 1 addition & 1 deletion src/Models/DTOs/ConvoDeletionRequestDto.cs
@@ -1,6 +1,6 @@
/*
Glitched Epistle - Client
Copyright (C) 2019 Raphael Beck
Copyright (C) 2020 Raphael Beck
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Expand Down
2 changes: 1 addition & 1 deletion src/Models/DTOs/ConvoJoinRequestDto.cs
@@ -1,6 +1,6 @@
/*
Glitched Epistle - Client
Copyright (C) 2019 Raphael Beck
Copyright (C) 2020 Raphael Beck
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Expand Down
2 changes: 1 addition & 1 deletion src/Models/DTOs/ConvoKickUserRequestDto.cs
@@ -1,6 +1,6 @@
/*
Glitched Epistle - Client
Copyright (C) 2019 Raphael Beck
Copyright (C) 2020 Raphael Beck
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Expand Down
2 changes: 1 addition & 1 deletion src/Models/DTOs/ConvoLeaveRequestDto.cs
@@ -1,6 +1,6 @@
/*
Glitched Epistle - Client
Copyright (C) 2019 Raphael Beck
Copyright (C) 2020 Raphael Beck
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Expand Down
2 changes: 1 addition & 1 deletion src/Models/DTOs/ConvoMetadataDto.cs
@@ -1,6 +1,6 @@
/*
Glitched Epistle - Client
Copyright (C) 2019 Raphael Beck
Copyright (C) 2020 Raphael Beck
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Expand Down
2 changes: 1 addition & 1 deletion src/Models/DTOs/PostMessageParamsDto.cs
@@ -1,6 +1,6 @@
/*
Glitched Epistle - Client
Copyright (C) 2019 Raphael Beck
Copyright (C) 2020 Raphael Beck
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Expand Down
4 changes: 2 additions & 2 deletions src/Models/DTOs/UserChangePasswordRequestDto.cs
@@ -1,6 +1,6 @@
/*
Glitched Epistle - Client
Copyright (C) 2019 Raphael Beck
Copyright (C) 2020 Raphael Beck
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -47,7 +47,7 @@ public class UserChangePasswordRequestDto

/// <summary>
/// New (encrypted) private key.<para> </para>
/// Needs to be PEM-formatted and encrypted into <c>byte[]</c> and then gzipped and base-64 encoded.
/// Needs to be PEM-formatted and encrypted into <c>byte[]</c> and then compressed and base-64 encoded.
/// </summary>
[JsonProperty("npkey")]
public string NewPrivateKey { get; set; }
Expand Down
6 changes: 3 additions & 3 deletions src/Models/DTOs/UserCreationRequestDto.cs
@@ -1,6 +1,6 @@
/*
Glitched Epistle - Client
Copyright (C) 2019 Raphael Beck
Copyright (C) 2020 Raphael Beck
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -37,14 +37,14 @@ public class UserCreationRequestDto : IEquatable<UserCreationRequestDto>

/// <summary>
/// The user's public RSA key.<para> </para>
/// PEM-formatted, and then gzipped via <c>Encoding.UTF8.GetBytes(string)</c> using <c>CompressionLevel.Fastest</c> and ultimately base-64 encoded.
/// PEM-formatted, and then compressed via <c>Encoding.UTF8.GetBytes(string)</c> using <c>CompressionLevel.Fastest</c> and ultimately base-64 encoded.
/// </summary>
[JsonProperty(PropertyName = "key")]
public string PublicKey { get; set; }

/// <summary>
/// The user's private message decryption RSA key.<para> </para>
/// PEM-formatted and encrypted into <c>byte[]</c> and then gzipped and base-64 encoded.
/// PEM-formatted and encrypted into <c>byte[]</c> and then compressed and base-64 encoded.
/// </summary>
[JsonProperty("pkey")]
public string PrivateKey { get; set; }
Expand Down
2 changes: 1 addition & 1 deletion src/Models/DTOs/UserCreationResponseDto.cs
@@ -1,6 +1,6 @@
/*
Glitched Epistle - Client
Copyright (C) 2019 Raphael Beck
Copyright (C) 2020 Raphael Beck
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Expand Down
2 changes: 1 addition & 1 deletion src/Models/DTOs/UserLoginRequestDto.cs
@@ -1,6 +1,6 @@
/*
Glitched Epistle - Client
Copyright (C) 2019 Raphael Beck
Copyright (C) 2020 Raphael Beck
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Expand Down
4 changes: 2 additions & 2 deletions src/Models/DTOs/UserLoginSuccessResponseDto.cs
@@ -1,6 +1,6 @@
/*
Glitched Epistle - Client
Copyright (C) 2019 Raphael Beck
Copyright (C) 2020 Raphael Beck
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -41,7 +41,7 @@ public class UserLoginSuccessResponseDto : IEquatable<UserLoginSuccessResponseDt
public string PublicKey { get; set; }

/// <summary>
/// The user's private key; PEM-formatted and encrypted into <c>byte[]</c> and then gzipped and base-64 encoded.
/// The user's private key; PEM-formatted and encrypted into <c>byte[]</c> and then compressed and base-64 encoded.
/// </summary>
[JsonProperty("pkey")]
public string PrivateKey { get; set; }
Expand Down
6 changes: 3 additions & 3 deletions src/Models/EpistleRequestBody.cs
@@ -1,6 +1,6 @@
/*
Glitched Epistle - Client
Copyright (C) 2019 Raphael Beck
Copyright (C) 2020 Raphael Beck
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -44,8 +44,8 @@ public class EpistleRequestBody
/// <summary>
/// The request body. These are the endpoint parameters.<para> </para>
/// Typically, this is some request DTO (like for example <see cref="ConvoCreationRequestDto"/>)
/// that was serialized into JSON and gzipped.<para> </para>
/// If the body is really short and/or represents only a single value, you can also assign the value directly instead of serializing + gzipping it.
/// that was serialized into JSON and compressed.<para> </para>
/// If the body is really short and/or represents only a single value, you can also assign the value directly instead of serializing + compressing it.
/// </summary>
[JsonProperty("body")]
public string Body { get; set; }
Expand Down
2 changes: 1 addition & 1 deletion src/Models/Message.cs
@@ -1,6 +1,6 @@
/*
Glitched Epistle - Client
Copyright (C) 2019 Raphael Beck
Copyright (C) 2020 Raphael Beck
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Expand Down
2 changes: 1 addition & 1 deletion src/Models/User.cs
@@ -1,6 +1,6 @@
/*
Glitched Epistle - Client
Copyright (C) 2019 Raphael Beck
Copyright (C) 2020 Raphael Beck
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Expand Down
2 changes: 1 addition & 1 deletion src/Services/Convos/ConvoPasswordProvider.cs
@@ -1,6 +1,6 @@
/*
Glitched Epistle - Client
Copyright (C) 2019 Raphael Beck
Copyright (C) 2020 Raphael Beck
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Expand Down
2 changes: 1 addition & 1 deletion src/Services/Convos/ConvoRepositorySQLite.cs
@@ -1,6 +1,6 @@
/*
Glitched Epistle - Client
Copyright (C) 2019 Raphael Beck
Copyright (C) 2020 Raphael Beck
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Expand Down
2 changes: 1 addition & 1 deletion src/Services/Convos/IConvoPasswordProvider.cs
@@ -1,6 +1,6 @@
/*
Glitched Epistle - Client
Copyright (C) 2019 Raphael Beck
Copyright (C) 2020 Raphael Beck
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Expand Down
2 changes: 1 addition & 1 deletion src/Services/Convos/IMessageRepository.cs
@@ -1,6 +1,6 @@
/*
Glitched Epistle - Client
Copyright (C) 2019 Raphael Beck
Copyright (C) 2020 Raphael Beck
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Expand Down
2 changes: 1 addition & 1 deletion src/Services/Convos/MessageRepositorySQLite.cs
@@ -1,6 +1,6 @@
/*
Glitched Epistle - Client
Copyright (C) 2019 Raphael Beck
Copyright (C) 2020 Raphael Beck
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Expand Down
@@ -1,6 +1,6 @@
/*
/*
Glitched Epistle - Client
Copyright (C) 2019 Raphael Beck
Copyright (C) 2020 Raphael Beck
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Expand All @@ -16,15 +16,15 @@
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

using GlitchedPolygons.Services.CompressionUtility;
using System.Threading.Tasks;
using GlitchedPolygons.Services.Cryptography.Symmetric;
using GlitchedPolygons.Services.Cryptography.Asymmetric;
using GlitchedPolygons.GlitchedEpistle.Client.Models;
using GlitchedPolygons.GlitchedEpistle.Client.Services.Web.Users;
using GlitchedPolygons.GlitchedEpistle.Client.Services.Web.Convos;
using GlitchedPolygons.GlitchedEpistle.Client.Services.Cryptography.Messages;

namespace GlitchedPolygons.GlitchedEpistle.Client.Utilities
namespace GlitchedPolygons.GlitchedEpistle.Client.Services.Cryptography.KeyExchange
{
/// <summary>
/// This key utility was extracted first of all, obviously,
Expand All @@ -34,7 +34,7 @@ namespace GlitchedPolygons.GlitchedEpistle.Client.Utilities
/// you can Shift-F12/navigate into the various message exchange/encryption relevant implementations
/// from here and convince yourself whether this product is for you or not, by reading the source code.
/// </summary>
public static class KeyExchangeUtility
public interface IKeyExchange
{
/// <summary>
/// Encrypts <paramref name="privateKeyPem"/> into a portable <c>string</c>
Expand All @@ -53,13 +53,15 @@ public static class KeyExchangeUtility
/// <param name="privateKeyPem">The user's private RSA key (PEM-formatted <c>string</c>).</param>
/// <param name="userPassword">The user's password (NOT its SHA512!).</param>
/// <returns><c>string</c> that contains the encrypted and compressed <paramref name="privateKeyPem"/>.</returns>
public static string EncryptAndCompressPrivateKey(string privateKeyPem, string userPassword)
{
ICompressionUtility gzip = new GZipUtility();
ISymmetricCryptography crypto = new SymmetricCryptography();

return gzip.Compress(crypto.EncryptWithPassword(privateKeyPem, userPassword));
}
string EncryptAndCompressPrivateKey(string privateKeyPem, string userPassword);

/// <summary>
/// Asynchronous variant of <see cref="EncryptAndCompressPrivateKey"/>.
/// </summary>
/// <param name="privateKeyPem">The user's private RSA key (PEM-formatted <c>string</c>).</param>
/// <param name="userPassword">The user's password (NOT its SHA512!).</param>
/// <returns><c>string</c> that contains the encrypted and compressed <paramref name="privateKeyPem"/>.</returns>
Task<string> EncryptAndCompressPrivateKeyAsync(string privateKeyPem, string userPassword);

/// <summary>
/// Decompresses and decrypts a private RSA key
Expand All @@ -69,26 +71,43 @@ public static string EncryptAndCompressPrivateKey(string privateKeyPem, string u
/// <param name="encryptedCompressedKey">The encrypted and compressed private key that you'd get from/to the backend (THE SERVER NEVER HAS YOUR PRIVATE KEY IN PLAIN TEXT).</param>
/// <param name="userPassword">The user's password (NOT the hash).</param>
/// <returns>The raw PEM-formatted private RSA Key (ready to be assigned to <see cref="User.PrivateKeyPem"/>).</returns>
public static string DecompressAndDecryptPrivateKey(string encryptedCompressedKey, string userPassword)
{
ICompressionUtility gzip = new GZipUtility();
ISymmetricCryptography crypto = new SymmetricCryptography();

return crypto.DecryptWithPassword(gzip.Decompress(encryptedCompressedKey), userPassword);
}
string DecompressAndDecryptPrivateKey(string encryptedCompressedKey, string userPassword);

/// <summary>
/// Returns the gzipped, base-64 encoded <paramref name="publicKeyPem"/>... ready to be exchanged with the backend.
/// <param name="publicKeyPem">The public RSA key (PEM-formatted) to compress.</param>
/// Asynchronous variant of <see cref="DecompressAndDecryptPrivateKey"/>.
/// </summary>
public static string CompressPublicKey(string publicKeyPem) => new GZipUtility().Compress(publicKeyPem);
/// <param name="encryptedCompressedKey">The encrypted and compressed private key that you'd get from/to the backend (THE SERVER NEVER HAS YOUR PRIVATE KEY IN PLAIN TEXT).</param>
/// <param name="userPassword">The user's password (NOT the hash).</param>
/// <returns>The raw PEM-formatted private RSA Key (ready to be assigned to <see cref="User.PrivateKeyPem"/>).</returns>
Task<string> DecompressAndDecryptPrivateKeyAsync(string encryptedCompressedKey, string userPassword);

/// <summary>
/// Returns the compressed, base-64 encoded <paramref name="publicKeyPem"/>... ready to be exchanged with the backend.
/// </summary>
/// <param name="publicKeyPem">The public RSA key (PEM-formatted) to compress.</param>
/// <returns>The compressed key.</returns>
string CompressPublicKey(string publicKeyPem);

/// <summary>
/// Asynchronous variant of <see cref="CompressPublicKey"/>.
/// </summary>
/// <param name="publicKeyPem">The public RSA key (PEM-formatted) to compress.</param>
/// <returns>The compressed key.</returns>
Task<string> CompressPublicKeyAsync(string publicKeyPem);

/// <summary>
/// Decompresses the <paramref name="compressedPublicKeyPem"/> that is
/// coming from a backend request's response and was initially compressed using <see cref="CompressPublicKey"/>.
/// </summary>
/// <param name="compressedPublicKeyPem">The compressed public key pem <c>string</c>.</param>
/// <returns>The decompressed <paramref name="compressedPublicKeyPem"/>.</returns>
public static string DecompressPublicKey(string compressedPublicKeyPem) => new GZipUtility().Decompress(compressedPublicKeyPem);
string DecompressPublicKey(string compressedPublicKeyPem);

/// <summary>
/// Asynchronous variant of <see cref="DecompressPublicKey"/>.
/// </summary>
/// <param name="compressedPublicKeyPem">The compressed public key pem <c>string</c>.</param>
/// <returns>The decompressed <paramref name="compressedPublicKeyPem"/>.</returns>
Task<string> DecompressPublicKeyAsync(string compressedPublicKeyPem);
}
}

0 comments on commit 2ddad00

Please sign in to comment.