Skip to content

Commit

Permalink
[PM-1348] Fix AWS expecting symmetric key (#34)
Browse files Browse the repository at this point in the history
* [PM-1348] switch to asymmetric key for aws encrypt/decrypt

* [PM-1348] provide setting to use symmetric key on aws

* [PM-1348] import system for exceptions

* [PM-1348] add InvalidKeyTypeException

* [PM-1348] allow InvalidKeyTypeException to be serializable

* [PM-1348] add context to exception message

* [PM-1348] actually add context to exception message
  • Loading branch information
jlf0dev authored Apr 26, 2023
1 parent 1fb8a1d commit 9cb7334
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 8 deletions.
20 changes: 20 additions & 0 deletions src/KeyConnector/Exceptions/InvalidKeyTypeException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;
using System.Runtime.Serialization;

namespace Bit.KeyConnector.Exceptions
{
[Serializable]
public class InvalidKeyTypeException : Exception
{
public InvalidKeyTypeException()
: base("This type of key cannot perform this action.") { }

public InvalidKeyTypeException(string message) : base(message) { }

public InvalidKeyTypeException(string message, Exception innerException)
: base(message, innerException) { }

protected InvalidKeyTypeException(SerializationInfo info, StreamingContext context)
: base(info, context) { }
}
}
1 change: 1 addition & 0 deletions src/KeyConnector/KeyConnectorSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public class RsaKeySettings
public string AwsAccessKeySecret { get; set; }
public string AwsRegion { get; set; }
public string AwsKeyId { get; set; }
public bool AwsUseSymmetricEncryption { get; set; }
// pkcs11
// Providers:
// yubihsm
Expand Down
23 changes: 21 additions & 2 deletions src/KeyConnector/Services/AwsKmsRsaKeyService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Amazon;
using Amazon.KeyManagementService;
using Amazon.KeyManagementService.Model;
using Bit.KeyConnector.Exceptions;

namespace Bit.KeyConnector.Services
{
Expand All @@ -26,7 +27,10 @@ public async Task<byte[]> EncryptAsync(byte[] data)
var request = new EncryptRequest
{
KeyId = _settings.RsaKey.AwsKeyId,
Plaintext = dataStream
Plaintext = dataStream,
EncryptionAlgorithm = _settings.RsaKey.AwsUseSymmetricEncryption
? EncryptionAlgorithmSpec.SYMMETRIC_DEFAULT
: EncryptionAlgorithmSpec.RSAES_OAEP_SHA_256
};
var response = await _kmsClient.EncryptAsync(request);
return response.CiphertextBlob.ToArray();
Expand All @@ -38,14 +42,21 @@ public async Task<byte[]> DecryptAsync(byte[] data)
var request = new DecryptRequest
{
KeyId = _settings.RsaKey.AwsKeyId,
CiphertextBlob = dataStream
CiphertextBlob = dataStream,
EncryptionAlgorithm = _settings.RsaKey.AwsUseSymmetricEncryption
? EncryptionAlgorithmSpec.SYMMETRIC_DEFAULT
: EncryptionAlgorithmSpec.RSAES_OAEP_SHA_256
};
var response = await _kmsClient.DecryptAsync(request);
return response.Plaintext.ToArray();
}

public async Task<byte[]> SignAsync(byte[] data)
{
if (_settings.RsaKey.AwsUseSymmetricEncryption)
{
throw new InvalidKeyTypeException("Cannot sign using symmetric key");
}
using var dataStream = new MemoryStream(data);
var request = new SignRequest
{
Expand All @@ -60,6 +71,10 @@ public async Task<byte[]> SignAsync(byte[] data)

public async Task<bool> VerifyAsync(byte[] data, byte[] signature)
{
if (_settings.RsaKey.AwsUseSymmetricEncryption)
{
throw new InvalidKeyTypeException("Cannot sign using symmetric key");
}
using var dataStream = new MemoryStream(data);
using var signatureStream = new MemoryStream(data);
var request = new VerifyRequest
Expand All @@ -76,6 +91,10 @@ public async Task<bool> VerifyAsync(byte[] data, byte[] signature)

public async Task<byte[]> GetPublicKeyAsync()
{
if (_settings.RsaKey.AwsUseSymmetricEncryption)
{
throw new InvalidKeyTypeException("Cannot retrieve public key as symmetric keys do not have public keys");
}
var request = new GetPublicKeyRequest
{
KeyId = _settings.RsaKey.AwsKeyId
Expand Down
2 changes: 1 addition & 1 deletion src/KeyConnector/Services/GoogleCloudKmsRsaKeyService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Security.Cryptography;
using System.Threading.Tasks;
using Google.Cloud.Kms.V1;
Expand Down
18 changes: 13 additions & 5 deletions src/KeyConnector/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Globalization;
using System.Security.Claims;
using Bit.KeyConnector.Repositories;
Expand Down Expand Up @@ -54,8 +54,11 @@ public void ConfigureServices(IServiceCollection services)
services.AddHostedService<HostedServices.DatabaseMigrationHostedService>();
}

services.AddHealthChecks()
.AddCheck<RsaHealthCheckService>("RsaHealthCheckService");
if (!settings.RsaKey.AwsUseSymmetricEncryption)
{
services.AddHealthChecks()
.AddCheck<RsaHealthCheckService>("RsaHealthCheckService");
}
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env, KeyConnectorSettings settings)
Expand All @@ -73,9 +76,14 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env, KeyConne
app.UseAuthentication();
app.UseAuthorization();

app.UseEndpoints(endpoints => {
app.UseEndpoints(endpoints =>
{
endpoints.MapDefaultControllerRoute();
endpoints.MapHealthChecks("~/health").AllowAnonymous();
if (!settings.RsaKey.AwsUseSymmetricEncryption)
{
endpoints.MapHealthChecks("~/health").AllowAnonymous();
}
});
}

Expand Down

0 comments on commit 9cb7334

Please sign in to comment.