using ServiceShared; using ServiceShared.Crypto; using ServiceShared.Models.Request; using ServiceShared.Models.Response; namespace ServiceOutside.Service { public static class ServiceInside { /// /// URL of ServiceInside /// private static string ServiceInsideURL; /// /// Sets the URL of ServiceInside /// /// Url of ServiceInside public static void SetServiceInsideURL(string serviceInsideURL) { ServiceInsideURL = serviceInsideURL; } /// /// Sends subscribe request to the inside service /// /// Subscribe object for inside service public static void Subscribe(Subscribe subscribe) { try { KeyPair keyPair = Curve25519.GenerateKeyPair(); EncryptedRequest encryptedRequest = new EncryptedRequest("Subscribe", subscribe, ServiceInsideDeriveKey(keyPair)); ServiceShared.Https.Request.PostWithoutResponse(ServiceInsideURL, "serviceoutside", "subscribe", encryptedRequest, keyPair); } catch (Exception ex) { Log.Critical(ex, "ServiceInside.Service.ServiceInside", "Subscribe"); } } /// /// Sends unsubscribe request to the inside service /// /// Subscribe object for inside service public static void Unsubscribe(Subscribe subscribe) { try { KeyPair keyPair = Curve25519.GenerateKeyPair(); EncryptedRequest encryptedRequest = new EncryptedRequest("Subscribe", subscribe, ServiceInsideDeriveKey(keyPair)); ServiceShared.Https.Request.PostWithoutResponse(ServiceInsideURL, "serviceoutside", "unsubscribe", encryptedRequest, keyPair); } catch (Exception ex) { Log.Critical(ex, "ServiceInside.Service.ServiceInside", "Subscribe"); } } /// /// Send DeleteDevice request to the inside service /// /// CheckResults object, that contains pgs public static EncryptedResponse DeleteDevice(DeleteDevice deleteDevice) { EncryptedResponse result = null; try { KeyPair keyPair = Curve25519.GenerateKeyPair(); EncryptedRequest encryptedRequest = new EncryptedRequest("DeleteDevice", deleteDevice, ServiceInsideDeriveKey(keyPair)); result = ServiceShared.Https.Request.Post(ServiceInsideURL, "serviceoutside", "delete_device", encryptedRequest, keyPair); if(result != null) { result.Decrypt(ServiceInsideDeriveKey(keyPair)); } } catch (Exception ex) { Log.Critical(ex, "ServiceInside.Service.ServiceInside", "DeleteDevice"); } return result; } /// /// Notifies the inside service, that the results was successfully picked up by patient and can be removed from the private storage in the inside service /// /// CheckResults object, that contains pgs and file checksum public static void PickedUp(CheckFileChecksum checkFileChecksum) { try { KeyPair keyPair = Curve25519.GenerateKeyPair(); EncryptedRequest encryptedRequest = new EncryptedRequest("CheckFileChecksum", checkFileChecksum, ServiceInsideDeriveKey(keyPair)); ServiceShared.Https.Request.PostWithoutResponse(ServiceInsideURL, "serviceoutside", "pickedup", encryptedRequest, keyPair); } catch (Exception ex) { Log.Critical(ex, "ServiceInside.Service.ServiceInside", "PickedUp"); } } /// /// Checks if results is already available for the pgs and get as encrypted download object from the inside service /// /// CheckResults object, that contains pgs /// Returns Download, that contains encrypted results public static Download GetDownload(CheckResults checkResults) { Download result = null; try { KeyPair keyPair = Curve25519.GenerateKeyPair(); EncryptedRequest encryptedRequest = new EncryptedRequest("CheckResults", checkResults, ServiceInsideDeriveKey(keyPair)); EncryptedResponse response = ServiceShared.Https.Request.Post(ServiceInsideURL, "serviceoutside", "get_download", encryptedRequest, keyPair); if(response != null) { result = response.Decrypt(ServiceInsideDeriveKey(keyPair)); } } catch (Exception ex) { Log.Critical(ex, "ServiceInside.Service.ServiceInside", "GetDownload"); } return result; } /// /// Shared Derive key from inside service over (exchange controller) /// /// private static byte[] ServiceInsideDeriveKey(KeyPair keyPair) { byte[] result = null; try { PublicKey publicKey = ServiceShared.Https.Request.GetPublicKey(ServiceInsideURL); if (publicKey != null && !string.IsNullOrEmpty(publicKey.key)) { byte[] base64Encoded = Convert.FromBase64String(publicKey.key); result = keyPair.GetSharedKey(publicKey.key); } else { Log.Critical(new Exception("Could not get public key from the inside service"), "ServiceOutside.Service.ServiceInside", "ServiceInsideDeriveKey"); } } catch (Exception ex) { Log.Critical(ex, "ServiceInside.Service.ServiceInside", "ServiceOutsideDeriveKey"); } return result; } } }