GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — Encrypted Messaging Encrypted Messaging
Demis Bellot edited this page
This page has moved to docs.servicestack.net/encrypted-messagingOne of the benefits of adopting a message-based design is being able to easily layer functionality and generically add value to all Services, we've seen this recently with Auto Batched Requests which automatically enables each Service to be batched and executed in a single HTTP Request. Similarly the new Encrypted Messaging feature enables a secure channel for all Services (inc Auto Batched Requests :) offering protection to clients who can now easily send and receive encrypted messages over unsecured HTTP!
Encrypted Messaging support is enabled by registering the plugin: Plugins.Add(new EncryptedMessagesFeature { PrivateKeyXml = ServerRsaPrivateKeyXml }); Where If you don't have an existing one, a new one can be generated with: var rsaKeyPair = RsaUtils.CreatePublicAndPrivateKeyPair(); string ServerRsaPrivateKeyXml = rsaKeyPair.PrivateKey; Once generated, it's important the Private Key is kept confidential as anyone with access will be able to decrypt the encrypted messages! Whilst most obfuscation efforts are ultimately futile the goal should be to contain the private key to your running Web Application, limiting access as much as possible. Once registered, the EncryptedMessagesFeature enables the 2 Services below:
To communicate clients need access to the Server's Public Key, it doesn't matter who has accessed the Public Key only that clients use the real Servers Public Key. It's therefore not advisable to download the Public Key over unsecure It's safer instead to download the public key over a trusted Since var client = new JsonServiceClient(BaseUrl); string publicKeyXml = client.Get(new GetPublicKey()); If the registered string publicKeyXml = client.Get<string>("/my-publickey"); //or with HttpUtils string publicKeyXml = BaseUrl.CombineWith("/my-publickey").GetStringFromUrl();
Once they have the Server's Public Key, clients can use it to get an var client = new JsonServiceClient(BaseUrl); IEncryptedClient encryptedClient = client.GetEncryptedClient(publicKeyXml); Once configured, clients have access to the familiar typed Service Client API's and productive workflow they're used to with the generic Service Clients, sending typed Request DTO's and returning the typed Response DTO's - rendering the underlying encrypted messages a transparent implementation detail: HelloResponse response = encryptedClient.Send(new Hello { Name = "World" }); response.Result.Print(); //Hello, World! REST Services Example: HelloResponse response = encryptedClient.Get(new Hello { Name = "World" }); Auto-Batched Requests Example: var requests = new[] { "Foo", "Bar", "Baz" }.Map(x => new HelloSecure { Name = x }); var responses = encryptedClient.SendAll(requests); When using the try { var response = encryptedClient.Send(new Hello()); } catch (WebServiceException ex) { ex.ResponseStatus.ErrorCode.Print(); //= ArgumentNullException ex.ResponseStatus.Message.Print(); //= Value cannot be null. } Many encrypted messaging solutions use Client Certificates which Servers can use to cryptographically verify a client's identity - providing an alternative to HTTP-based Authentication. We've decided against using this as it would've forced an opinionated implementation and increased burden of PKI certificate management and configuration onto Clients and Servers - reducing the applicability and instant utility of this feature. We can instead leverage the existing Session-based Authentication Model in ServiceStack letting clients continue to use the existing Auth functionality and Auth Providers they're already used to, e.g: var authResponse = encryptedClient.Send(new Authenticate { provider = CredentialsAuthProvider.Name, UserName = "[email protected]", Password = "p@55w0rd", }); Encrypted Messages have their cookies stripped so they're no longer visible in the clear which minimizes their exposure to Session hijacking. This does pose the problem of how we can call authenticated Services if the encrypted HTTP Client is no longer sending Session Cookies? Without the use of clear-text Cookies or HTTP Headers there's no longer an established Authenticated Session for the [Authenticate] public class HelloAuthenticated : IReturn<HelloAuthenticatedResponse>, IHasSessionId { public string SessionId { get; set; } public string Name { get; set; } } var response = encryptedClient.Send(new HelloAuthenticated { SessionId = authResponse.SessionId, Name = "World" }); Here we're injecting the returned Authenticated encryptedClient.SessionId = authResponse.SessionId; var response = encryptedClient.Send(new HelloAuthenticated { Name = "World" });
Another potential use-case is to only use Encrypted Messaging when sending any sensitive information and the normal Service Client for other requests. In which case we can Authenticate and send the user's password with the var authResponse = encryptedClient.Send(new Authenticate { provider = CredentialsAuthProvider.Name, UserName = "[email protected]", Password = "p@55w0rd", }); But then fallback to using the normal client.SetCookie("ss-id", authResponse.SessionId); After which the ServiceClient "establishes an authenticated session" and can be used to make Authenticated requests, e.g: var response = await client.GetAsync(new HelloAuthenticated { Name = "World" });
The Encrypted Messaging Feature follows a Hybrid Cryptosystem which uses RSA Public Keys for Asymmetric Encryption combined with the performance of AES Symmetric Encryption making it suitable for encrypting large message payloads. The authenticity of Encrypted Data are then verified with HMAC SHA-256, essentially following an Encrypt-then-MAC strategy. The key steps in the process are outlined below:
On the Server, the
A visual of how this all fits together in captured in the high-level diagram below:
All Request and Response DTO's get encrypted and embedded in the public class EncryptedMessage : IReturn<EncryptedMessageResponse> { public string KeyId { get; set; } public string EncryptedSymmetricKey { get; set; } public string EncryptedBody { get; set; } } public class EncryptedMessageResponse { public string EncryptedBody { get; set; } } The diagram also expands the
One artifact visible in the above process was the use of a By supporting multiple private keys, the Encrypted Messaging feature allows the seamless transition to a new Private Key without affecting existing clients who have yet to adopt the latest Public Key. Transitioning to a new Private Key just involves taking the existing Private Key and adding it to the
Plugins.Add(new EncryptedMessagesFeature { PrivateKey = NewPrivateKey, FallbackPrivateKeys = { PreviousKey2015, PreviousKey2014, }, }); Since anyone who has a copy of the Private Key can decrypt encrypted messages, rotating the private key clients use limits the amount of exposure an adversary who has managed to get a hold of a compromised private key has. i.e. if the current Private Key was somehow compromised, an attacker with access to the encrypted network packets will be able to read each message sent that was encrypted with the compromised private key up until the Server introduces a new Private Key which clients switches over to.
Clone this wiki locally |
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |