public Node EnableRoomEncryption(EnableRoomEncryptionRequest request) { _client.Executor.CheckApiServerVersion(); #region Parameter Validation request.MustNotNull(nameof(request)); request.DataRoomRescueKeyPassword.MustNotNullOrEmptyOrWhitespace(nameof(request.DataRoomRescueKeyPassword), true); request.Id.MustPositive(nameof(request.Id)); #endregion ApiUserKeyPair apiDataRoomRescueKey = null; if (request.DataRoomRescueKeyPassword != null) { try { UserKeyPair cryptoPair = Crypto.Sdk.Crypto.GenerateUserKeyPair(request.DataRoomRescueKeyPassword); apiDataRoomRescueKey = UserMapper.ToApiUserKeyPair(cryptoPair); } catch (CryptoException ce) { DracoonClient.Log.Debug(Logtag, $"Generation of user key pair failed with '{ce.Message}'!"); throw new DracoonCryptoException(CryptoErrorMapper.ParseCause(ce), ce); } } ApiEnableRoomEncryptionRequest apiEnableRoomEncryptionRequest = RoomMapper.ToApiEnableRoomEncryptionRequest(request, apiDataRoomRescueKey); IRestRequest restRequest = _client.Builder.PutEnableRoomEncryption(request.Id, apiEnableRoomEncryptionRequest); ApiNode result = _client.Executor.DoSyncApiCall <ApiNode>(restRequest, RequestType.PutEnableRoomEncryption); return(NodeMapper.FromApiNode(result)); }
internal UserKeyPair GenerateNewUserKeyPair(string encryptionPassword) { try { return(Crypto.Sdk.Crypto.GenerateUserKeyPair(encryptionPassword)); } catch (CryptoException ce) { DracoonClient.Log.Debug(Logtag, "Generation of user key pair failed with " + ce.Message); throw new DracoonCryptoException(CryptoErrorMapper.ParseCause(ce), ce); } }
private PlainFileKey DecryptFileKey(EncryptedFileKey encryptedFileKey) { try { return(Crypto.Sdk.Crypto.DecryptFileKey(encryptedFileKey, _userPrivateKey, Client.EncryptionPassword)); } catch (CryptoException ce) { string message = "Decryption of file key for encrypted download " + ActionId + " failed!"; DracoonClient.Log.Debug(Logtag, message); throw new DracoonCryptoException(CryptoErrorMapper.ParseCause(ce), ce); } }
private EncryptedFileKey EncryptFileKey(PlainFileKey plainFileKey) { try { return(Crypto.Sdk.Crypto.EncryptFileKey(plainFileKey, _userPublicKey)); } catch (CryptoException ce) { string message = "Encryption of file key for upload " + ActionId + " failed!"; DracoonClient.Log.Debug(LogTag, message); throw new DracoonCryptoException(CryptoErrorMapper.ParseCause(ce)); } }
private PlainFileKey CreateFileKey() { try { return(Crypto.Sdk.Crypto.GenerateFileKey(_userPublicKey.Version)); } catch (CryptoException ce) { string message = "Creation of file key for upload " + ActionId + " failed!"; DracoonClient.Log.Debug(LogTag, message); throw new DracoonCryptoException(CryptoErrorMapper.ParseCause(ce)); } }
internal PlainFileKey DecryptFileKey(EncryptedFileKey encryptedFileKey, UserPrivateKey userPrivateKey, long?nodeId = null) { try { return(Crypto.Sdk.Crypto.DecryptFileKey(encryptedFileKey, userPrivateKey, _client.EncryptionPassword)); } catch (CryptoException ce) { string message = "Decryption file key for node " + (nodeId.HasValue ? nodeId.Value.ToString() : "NULL") + " failed with " + ce.Message; DracoonClient.Log.Debug(Logtag, message); throw new DracoonCryptoException(CryptoErrorMapper.ParseCause(ce), ce); } }
internal EncryptedFileKey EncryptFileKey(PlainFileKey plainFileKey, UserPublicKey userPublicKey, long?nodeId = null) { try { return(Crypto.Sdk.Crypto.EncryptFileKey(plainFileKey, userPublicKey)); } catch (CryptoException ce) { string message = "Encryption file key for node " + (nodeId.HasValue ? nodeId.Value.ToString() : "NULL") + " failed with " + ce.Message; DracoonClient.Log.Debug(Logtag, message); throw new DracoonCryptoException(CryptoErrorMapper.ParseCause(ce)); } }
public void ParseCause(Exception param, DracoonCryptoCode expected) { // ARRANGE // ACT DracoonCryptoCode actual = CryptoErrorMapper.ParseCause(param); // ASSERT Assert.Equal(expected.Code, actual.Code); Assert.Equal(expected.Text, actual.Text); }
private void EncryptedDownload(Uri downloadUri, PlainFileKey plainFileKey) { FileDecryptionCipher cipher; try { cipher = Crypto.Sdk.Crypto.CreateFileDecryptionCipher(plainFileKey); } catch (CryptoException ce) { string message = "Creation of decryption engine for encrypted download " + ActionId + " failed!"; DracoonClient.Log.Debug(Logtag, message); throw new DracoonCryptoException(CryptoErrorMapper.ParseCause(ce), ce); } try { ProgressReportTimer = Stopwatch.StartNew(); long downloadedByteCount = 0; while (downloadedByteCount < AssociatedNode.Size.GetValueOrDefault(0)) { byte[] chunk = DownloadChunk(downloadUri, downloadedByteCount, AssociatedNode.Size.GetValueOrDefault(0)); EncryptedDataContainer encryptedContainer = new EncryptedDataContainer(chunk, null); PlainDataContainer plainContainer = cipher.ProcessBytes(encryptedContainer); OutputStream.Write(plainContainer.Content, 0, plainContainer.Content.Length); downloadedByteCount += chunk.Length; } byte[] encryptionTag = Convert.FromBase64String(plainFileKey.Tag); EncryptedDataContainer tagContainer = new EncryptedDataContainer(null, encryptionTag); PlainDataContainer finalContainer = cipher.DoFinal(tagContainer); OutputStream.Write(finalContainer.Content, 0, finalContainer.Content.Length); if (LastNotifiedProgressValue != downloadedByteCount) { // Notify 100 percent progress NotifyProgress(ActionId, downloadedByteCount, AssociatedNode.Size.GetValueOrDefault(0)); } } catch (CryptoException ce) { const string message = "Decryption of file failed while downloading!"; DracoonClient.Log.Debug(Logtag, message); throw new DracoonFileIOException(message, ce); } catch (IOException ioe) { if (IsInterrupted) { throw new ThreadInterruptedException(); } const string message = "Write to stream failed!"; DracoonClient.Log.Debug(Logtag, message); throw new DracoonFileIOException(message, ioe); } finally { ProgressReportTimer.Stop(); } }
public void GenerateNewUserKeyPair_Fail() { // ARRANGE IInternalDracoonClient c = FactoryClients.InternalDracoonClientMock(true); DracoonAccountImpl a = new DracoonAccountImpl(c); Mock.Arrange(() => Crypto.Sdk.Crypto.GenerateUserKeyPair(Arg.AnyString)).Throws(new CryptoException()).Occurs(1); Mock.Arrange(() => CryptoErrorMapper.ParseCause(Arg.IsAny <Exception>())).Returns(DracoonCryptoCode.UNKNOWN_ERROR).Occurs(1); // ACT - ASSERT Assert.Throws <DracoonCryptoException>(() => a.GenerateNewUserKeyPair(c.EncryptionPassword)); Mock.Assert(() => Crypto.Sdk.Crypto.GenerateUserKeyPair(Arg.AnyString)); Mock.Assert(() => CryptoErrorMapper.ParseCause(Arg.IsAny <Exception>())); }
internal UserKeyPair GetAndCheckUserKeyPair() { try { IRestRequest request = _client.Builder.GetUserKeyPair(); ApiUserKeyPair result = _client.Executor.DoSyncApiCall <ApiUserKeyPair>(request, RequestType.GetUserKeyPair); UserKeyPair userKeyPair = UserMapper.FromApiUserKeyPair(result); if (Crypto.Sdk.Crypto.CheckUserKeyPair(userKeyPair, _client.EncryptionPassword)) { return(userKeyPair); } throw new DracoonCryptoException(DracoonCryptoCode.INVALID_PASSWORD_ERROR); } catch (CryptoException ce) { DracoonClient.Log.Debug(Logtag, $"Check of user key pair failed with '{ce.Message}'!"); throw new DracoonCryptoException(CryptoErrorMapper.ParseCause(ce), ce); } }
private void EncryptedUpload(ref PlainFileKey plainFileKey) { DracoonClient.Log.Debug(LogTag, "Uploading file [" + FileUploadRequest.Name + "] in encrypted proxied way."); FileEncryptionCipher cipher; try { cipher = Crypto.Sdk.Crypto.CreateFileEncryptionCipher(plainFileKey); } catch (CryptoException ce) { string message = "Creation of encryption engine for encrypted upload " + ActionId + " failed!"; DracoonClient.Log.Debug(LogTag, message); throw new DracoonCryptoException(CryptoErrorMapper.ParseCause(ce)); } try { long uploadedByteCount = 0; byte[] buffer = new byte[DracoonClient.HttpConfig.ChunkSize]; int bytesRead = 0; while ((bytesRead = InputStream.Read(buffer, 0, buffer.Length)) > 0) { EncryptedDataContainer encryptedContainer = EncryptChunk(cipher, bytesRead, buffer, false); ProcessEncryptedChunk(new Uri(UploadToken.UploadUrl), encryptedContainer, uploadedByteCount, cipher, false); uploadedByteCount += encryptedContainer.Content.Length; } EncryptedDataContainer finalEncryptedContainer = EncryptChunk(cipher, bytesRead, buffer, true); plainFileKey.Tag = ProcessEncryptedChunk(new Uri(UploadToken.UploadUrl), finalEncryptedContainer, uploadedByteCount, cipher, true); uploadedByteCount += finalEncryptedContainer.Content.Length; if (LastNotifiedProgressValue != uploadedByteCount) { // Notify 100 percent progress NotifyProgress(ActionId, uploadedByteCount, OptionalFileSize); } } catch (IOException ioe) { if (IsInterrupted) { throw new ThreadInterruptedException(); } string message = "Read from stream failed!"; DracoonClient.Log.Debug(LogTag, message); throw new DracoonFileIOException(message, ioe); } finally { ProgressReportTimer?.Stop(); } }
public void GetAndCheckUserKeyPair_Fail() { // ARRANGE IInternalDracoonClient c = FactoryClients.InternalDracoonClientMock(true); DracoonAccountImpl a = new DracoonAccountImpl(c); Mock.Arrange(() => c.Builder.GetUserKeyPair()).Returns(FactoryRestSharp.GetUserKeyPairMock()); Mock.Arrange(() => c.Executor.DoSyncApiCall <ApiUserKeyPair>(Arg.IsAny <IRestRequest>(), RequestType.GetUserKeyPair, 0)) .Returns(FactoryUser.ApiUserKeyPair); Mock.Arrange(() => Crypto.Sdk.Crypto.CheckUserKeyPair(Arg.IsAny <UserKeyPair>(), Arg.AnyString)).Throws(new CryptoException()); Mock.Arrange(() => CryptoErrorMapper.ParseCause(Arg.IsAny <Exception>())).Returns(DracoonCryptoCode.UNKNOWN_ERROR).Occurs(1); try { // ACT a.GetAndCheckUserKeyPair(); } catch (DracoonCryptoException e) { // ASSERT Assert.NotEqual(DracoonCryptoCode.INVALID_PASSWORD_ERROR.Code, e.ErrorCode.Code); Mock.Assert(() => CryptoErrorMapper.ParseCause(Arg.IsAny <Exception>())); } }
private List <ApiS3FileUploadPart> EncryptedS3Upload(ref PlainFileKey plainFileKey) { DracoonClient.Log.Debug(LogTag, "Uploading file [" + FileUploadRequest.Name + "] via encrypted s3 direct upload."); FileEncryptionCipher cipher; try { cipher = Crypto.Sdk.Crypto.CreateFileEncryptionCipher(plainFileKey); } catch (CryptoException ce) { DracoonClient.Log.Debug(LogTag, "Creation of encryption engine for encrypted upload " + ActionId + " failed!"); throw new DracoonCryptoException(CryptoErrorMapper.ParseCause(ce)); } try { int chunkSize = DefineS3ChunkSize(); int s3UrlBatchSize = DefineS3BatchSize(chunkSize); long uploadedByteCount = 0; byte[] buffer = new byte[chunkSize]; int bytesRead, offset = 0; while ((bytesRead = InputStream.Read(buffer, offset, buffer.Length - offset)) >= 0) { int nextByte = InputStream.ReadByte(); // Check if further bytes are available byte[] chunk; EncryptedDataContainer container = EncryptChunk(cipher, bytesRead + offset, buffer, false); if (nextByte == -1) { // It is the last block EncryptedDataContainer finalContainer = EncryptChunk(cipher, bytesRead + offset, buffer, true); plainFileKey.Tag = Convert.ToBase64String(finalContainer.Tag); chunk = new byte[container.Content.Length + finalContainer.Content.Length]; Buffer.BlockCopy(finalContainer.Content, 0, chunk, container.Content.Length, finalContainer.Content.Length); } else { chunk = new byte[container.Content.Length]; } Buffer.BlockCopy(container.Content, 0, chunk, 0, container.Content.Length); if (chunk.Length < chunkSize) { S3Urls = RequestS3Urls(S3Parts.Count + 1, 1, chunk.Length); } else if (S3Urls.Count == 0) { S3Urls = RequestS3Urls(S3Parts.Count + 1, s3UrlBatchSize, chunkSize); } string partETag = UploadS3ChunkWebClient(S3Urls.Dequeue(), chunk, uploadedByteCount); S3Parts.Add(new ApiS3FileUploadPart() { PartEtag = partETag, PartNumber = S3Parts.Count + 1 }); uploadedByteCount += chunk.Length; if (nextByte != -1) { // Do it every time if the current block isn't the last Buffer.SetByte(buffer, 0, (byte)nextByte); offset = 1; } else { break; } } if (LastNotifiedProgressValue != uploadedByteCount) { // Notify 100 percent progress NotifyProgress(ActionId, uploadedByteCount, OptionalFileSize); } return(S3Parts); } catch (IOException ioe) { if (IsInterrupted) { throw new ThreadInterruptedException(); } string message = "Read from stream failed!"; DracoonClient.Log.Debug(LogTag, message); throw new DracoonFileIOException(message, ioe); } finally { ProgressReportTimer?.Stop(); } }