Browse Source

dont need to encrypt/decrypt with client keys

pull/2/head
Kyle Spearrin 4 years ago
parent
commit
10288199bc
  1. 20
      src/CryptoAgent/Controllers/MiscController.cs
  2. 17
      src/CryptoAgent/Controllers/UserKeysController.cs
  3. 11
      src/CryptoAgent/Models/UserKeyGetRequestModel.cs
  4. 6
      src/CryptoAgent/Models/UserKeyRequestModel.cs
  5. 60
      src/CryptoAgent/Services/CryptoService.cs
  6. 5
      src/CryptoAgent/Services/ICryptoService.cs

20
src/CryptoAgent/Controllers/MiscController.cs

@ -1,21 +1,11 @@ @@ -1,21 +1,11 @@
using Bit.CryptoAgent.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Threading.Tasks;
namespace Bit.CryptoAgent.Controllers
{
public class MiscController : Controller
{
private readonly IRsaKeyService _rsaKeyService;
public MiscController(
IRsaKeyService rsaKeyService)
{
_rsaKeyService = rsaKeyService;
}
[HttpGet("~/alive")]
[HttpGet("~/now")]
[AllowAnonymous]
@ -23,13 +13,5 @@ namespace Bit.CryptoAgent.Controllers @@ -23,13 +13,5 @@ namespace Bit.CryptoAgent.Controllers
{
return DateTime.UtcNow;
}
[HttpGet("~/public-key")]
[AllowAnonymous]
public async Task<IActionResult> GetPublicKey()
{
var key = await _rsaKeyService.GetPublicKeyAsync();
return new OkObjectResult(new { PublicKey = Convert.ToBase64String(key) });
}
}
}

17
src/CryptoAgent/Controllers/UserKeysController.cs

@ -33,11 +33,10 @@ namespace Bit.CryptoAgent.Controllers @@ -33,11 +33,10 @@ namespace Bit.CryptoAgent.Controllers
_userKeyRepository = userKeyRepository;
}
[HttpPost("get")]
public async Task<IActionResult> Get([FromBody] UserKeyGetRequestModel model)
[HttpGet]
public async Task<IActionResult> Get()
{
var userId = GetProperUserId().Value;
var publicKey = Convert.FromBase64String(model.PublicKey);
var user = await _userKeyRepository.ReadAsync(userId);
if (user == null)
{
@ -45,12 +44,10 @@ namespace Bit.CryptoAgent.Controllers @@ -45,12 +44,10 @@ namespace Bit.CryptoAgent.Controllers
}
user.LastAccessDate = DateTime.UtcNow;
await _userKeyRepository.UpdateAsync(user);
var key = await _cryptoService.AesDecryptAsync(user.Key);
var encKey = await _cryptoService.RsaEncryptAsync(key, publicKey);
var response = new UserKeyResponseModel
{
Key = Convert.ToBase64String(encKey)
};
Key = await _cryptoService.AesDecryptToB64Async(user.Key)
};
return new JsonResult(response);
}
@ -63,11 +60,10 @@ namespace Bit.CryptoAgent.Controllers @@ -63,11 +60,10 @@ namespace Bit.CryptoAgent.Controllers
{
return new BadRequestResult();
}
var key = await _cryptoService.RsaDecryptAsync(Convert.FromBase64String(model.Key));
user = new UserKeyModel
{
Id = userId,
Key = await _cryptoService.AesEncryptToB64Async(key)
Key = await _cryptoService.AesEncryptToB64Async(model.Key)
};
await _userKeyRepository.CreateAsync(user);
return new OkResult();
@ -82,11 +78,10 @@ namespace Bit.CryptoAgent.Controllers @@ -82,11 +78,10 @@ namespace Bit.CryptoAgent.Controllers
{
return new BadRequestResult();
}
var key = await _cryptoService.RsaDecryptAsync(Convert.FromBase64String(model.Key));
user = new UserKeyModel
{
Id = userId,
Key = await _cryptoService.AesEncryptToB64Async(key)
Key = await _cryptoService.AesEncryptToB64Async(model.Key)
};
await _userKeyRepository.UpdateAsync(user);
return new OkResult();

11
src/CryptoAgent/Models/UserKeyGetRequestModel.cs

@ -1,11 +0,0 @@ @@ -1,11 +0,0 @@
using System;
using System.ComponentModel.DataAnnotations;
namespace Bit.CryptoAgent.Models
{
public class UserKeyGetRequestModel
{
[Required]
public string PublicKey { get; set; }
}
}

6
src/CryptoAgent/Models/UserKeyRequestModel.cs

@ -1,8 +1,4 @@ @@ -1,8 +1,4 @@
using Bit.CryptoAgent.Services;
using System;
using System.Threading.Tasks;
namespace Bit.CryptoAgent.Models
namespace Bit.CryptoAgent.Models
{
public class UserKeyRequestModel
{

60
src/CryptoAgent/Services/CryptoService.cs

@ -93,48 +93,6 @@ namespace Bit.CryptoAgent.Services @@ -93,48 +93,6 @@ namespace Bit.CryptoAgent.Services
return Convert.ToBase64String(encData);
}
// RSA Encrypt
public async Task<byte[]> RsaEncryptAsync(byte[] data, byte[] publicKey = null)
{
if (data == null)
{
return null;
}
if (publicKey == null)
{
return await _rsaKeyService.EncryptAsync(data);
}
var encData = await _cryptoFunctionService.RsaEncryptAsync(data, publicKey);
return encData;
}
// RSA Decrypt
public async Task<byte[]> RsaDecryptAsync(byte[] data)
{
if (data == null)
{
return null;
}
return await _rsaKeyService.DecryptAsync(data);
}
// RSA Verify
public async Task<bool> RsaVerifyAsync(byte[] data, byte[] signature, byte[] publicKey = null)
{
if (data == null || signature == null)
{
return false;
}
if (publicKey == null)
{
return await _rsaKeyService.VerifyAsync(data, signature);
}
return await _cryptoFunctionService.RsaVerifyAsync(data, signature, publicKey);
}
// Helpers
private async Task<byte[]> GetSymmetricKeyAsync()
@ -158,5 +116,23 @@ namespace Bit.CryptoAgent.Services @@ -158,5 +116,23 @@ namespace Bit.CryptoAgent.Services
return _symmetricKey;
}
private async Task<byte[]> RsaEncryptAsync(byte[] data)
{
if (data == null)
{
return null;
}
return await _rsaKeyService.EncryptAsync(data);
}
private async Task<byte[]> RsaDecryptAsync(byte[] data)
{
if (data == null)
{
return null;
}
return await _rsaKeyService.DecryptAsync(data);
}
}
}

5
src/CryptoAgent/Services/ICryptoService.cs

@ -12,8 +12,5 @@ namespace Bit.CryptoAgent.Services @@ -12,8 +12,5 @@ namespace Bit.CryptoAgent.Services
Task<byte[]> AesEncryptAsync(string b64Data, byte[] key = null);
Task<string> AesEncryptToB64Async(byte[] data, byte[] key = null);
Task<string> AesEncryptToB64Async(string b64Data, byte[] key = null);
Task<byte[]> RsaEncryptAsync(byte[] data, byte[] publicKey = null);
Task<byte[]> RsaDecryptAsync(byte[] data);
Task<bool> RsaVerifyAsync(byte[] data, byte[] signature, byte[] publicKey = null);
}
}
}

Loading…
Cancel
Save