Correction d'un bug taille de fichier lors de l'envoie storebackend

This commit is contained in:
Jérémi N ‘EndMove’ 2022-03-12 18:00:03 +01:00
parent 46cbcaf705
commit 64492c852e
Signed by: EndMove
GPG Key ID: 65C4A02E1F5371A4
4 changed files with 40 additions and 17 deletions

View File

@ -11,6 +11,7 @@ import lightcontainer.utils.FileReceiver;
import lightcontainer.utils.SHA; import lightcontainer.utils.SHA;
import lightcontainer.utils.ShaHasher; import lightcontainer.utils.ShaHasher;
import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
@ -41,7 +42,7 @@ public class SavefileRule extends ProtocolReader {
public class Result extends ProtocolResult { public class Result extends ProtocolResult {
// Variables // Variables
private String filename; private String filename;
private final int size; private int size;
// Construct // Construct
public Result(Context context, String filename, int size) { public Result(Context context, String filename, int size) {
@ -69,8 +70,9 @@ public class SavefileRule extends ProtocolReader {
String key = getContext().getAesKey(); String key = getContext().getAesKey();
String iv = AES_GCM.generateIV(); String iv = AES_GCM.generateIV();
if (!fileReceiver.receiveFile(reader, this.filename, this.size, key, iv)) // retrieve file and new size
throw new IOException(); int encryptedFileSize = fileReceiver.receiveFile(reader, this.filename, this.size, key, iv);
if (encryptedFileSize < 0) throw new IOException();
String fileHash = SHA.hashFile(storagePath, this.filename); String fileHash = SHA.hashFile(storagePath, this.filename);
@ -80,7 +82,7 @@ public class SavefileRule extends ProtocolReader {
getContext().putDataString("iv", iv); getContext().putDataString("iv", iv);
getContext().putDataString("fileNameSalt", fileNameSalt); getContext().putDataString("fileNameSalt", fileNameSalt);
this.setResultCommand(protocolRep.executeWriter(getContext(), SendfileRule.NAME, this.filename, String.valueOf(this.size), fileHash), ResultCmdReceiver.STOREBACKEND); this.setResultCommand(protocolRep.executeWriter(getContext(), SendfileRule.NAME, this.filename, String.valueOf(encryptedFileSize), fileHash), ResultCmdReceiver.STOREBACKEND);
} catch (IOException | SHA.ShaException e) { } catch (IOException | SHA.ShaException e) {
this.setResultCommand(protocolRep.executeWriter(getContext(), SaveFileErrorRule.NAME), ResultCmdReceiver.CLIENT); this.setResultCommand(protocolRep.executeWriter(getContext(), SaveFileErrorRule.NAME), ResultCmdReceiver.CLIENT);
e.printStackTrace(); e.printStackTrace();

View File

@ -44,7 +44,7 @@ public class AES_GCM {
encryptStream( encryptStream(
new FileInputStream(inFile), new FileInputStream(inFile),
new FileOutputStream(outFile), new FileOutputStream(outFile),
(int)inFile.length(), inFile.length(),
IVFile, IVFile,
keyFile keyFile
); );
@ -52,7 +52,7 @@ public class AES_GCM {
decryptStream( decryptStream(
new FileInputStream(outFile), new FileInputStream(outFile),
new FileOutputStream(clearFile), new FileOutputStream(clearFile),
(int)outFile.length(), outFile.length(),
IVFile, IVFile,
keyFile keyFile
); );
@ -211,7 +211,7 @@ public class AES_GCM {
* *
* @throws AesGcmException Exception if an error occur. * @throws AesGcmException Exception if an error occur.
*/ */
public static void encryptStream(InputStream in, OutputStream out, int fileSize, String key, String IV) throws AesGcmException { public static void encryptStream(InputStream in, OutputStream out, long fileSize, String key, String IV) throws AesGcmException {
byte[] buffer = new byte[1024]; byte[] buffer = new byte[1024];
int currentSize = 0; int currentSize = 0;
int bytes; int bytes;
@ -266,7 +266,7 @@ public class AES_GCM {
* *
* @throws AesGcmException Exception if an error occur. * @throws AesGcmException Exception if an error occur.
*/ */
public static void decryptStream(InputStream in, OutputStream out, int fileSize, String key, String IV) throws AesGcmException { public static void decryptStream(InputStream in, OutputStream out, long fileSize, String key, String IV) throws AesGcmException {
byte[] buffer = new byte[1024]; byte[] buffer = new byte[1024];
int currentSize = 0; int currentSize = 0;
int bytes; int bytes;

View File

@ -1,17 +1,13 @@
package lightcontainer.utils; package lightcontainer.utils;
import javax.crypto.Cipher; import java.io.*;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
public class FileReceiver { public class FileReceiver {
private String path; private String path;
public FileReceiver(String path) { this.path = path; } public FileReceiver(String path) { this.path = path; }
public boolean receiveFile(InputStream input, String fileName, int fileSize, String key, String iv) { public int receiveFile(InputStream input, String fileName, int fileSize, String key, String iv) {
BufferedOutputStream bosFile = null; BufferedOutputStream bosFile = null;
try { try {
@ -21,11 +17,13 @@ public class FileReceiver {
bosFile.flush(); bosFile.flush();
bosFile.close(); bosFile.close();
return true;
File f = new File(String.format("%s/%s", path, fileName));
return (int)f.length();
} catch(IOException | AES_GCM.AesGcmException ex) { } catch(IOException | AES_GCM.AesGcmException ex) {
ex.printStackTrace(); ex.printStackTrace();
if(bosFile != null) { try { bosFile.close(); } catch(Exception e) {} } if(bosFile != null) { try { bosFile.close(); } catch(Exception e) {} }
return false; return -1;
} }
} }

View File

@ -1 +1,24 @@
{"unicast_port":8000,"multicast_ip":"226.66.66.1","multicast_port":15502,"network_interface":"My network interface","tls":true,"storagePath":"D:\\ffe","users":[{"name":"endmove","password":"1f82407cace28cd7d97d23f82e8235d9da97575d033a12334fc71d3517f6a90fa04af829df3c722c38d3b68842e4ca2b","aes_key":"p0G+iViPp656O6aMKgcXSDT/e9/00wQH/ztUWf4tyr4=","passwordSalt":"ItYiXkwPa84Gwb6dGHQvXQ==","files":[{"name":"096a4b2f5673e1f1e1f4cb7e1a569f06cc31064f5dd254948f5d7c44769fd02a098f48ce60c333fae14cfcb29cacc87e","fileNameSalt":"v9ByBhvxds33+6Ha7c8oPQ==","size":12648,"iv":"OM9l2Rt6dqLDsGS3bQuuEQ==","storage":["lightcontainerSB01"]}]},{"name":"aaaaa","password":"5d628c274ebb008324f1e199d3bfff0a3fe839730a7f2355e82850d7acca5e5ca64db9071abf3d91034295695f84a617","aes_key":"qlTH6TijnfMRnrS0Qf+k6IPKGp5LoRMXGxCq16e+mF4=","passwordSalt":"Ns8Al6DpqPsIDlCSRBVTEg==","files":[]}]} {
"unicast_port": 8000,
"multicast_ip": "226.66.66.1",
"multicast_port": 15502,
"network_interface": "My network interface",
"tls": true,
"storagePath": "D:\\ffe",
"users": [
{
"name": "endmove",
"password": "1f82407cace28cd7d97d23f82e8235d9da97575d033a12334fc71d3517f6a90fa04af829df3c722c38d3b68842e4ca2b",
"aes_key": "p0G+iViPp656O6aMKgcXSDT/e9/00wQH/ztUWf4tyr4=",
"passwordSalt": "ItYiXkwPa84Gwb6dGHQvXQ==",
"files": []
},
{
"name": "aaaaa",
"password": "5d628c274ebb008324f1e199d3bfff0a3fe839730a7f2355e82850d7acca5e5ca64db9071abf3d91034295695f84a617",
"aes_key": "qlTH6TijnfMRnrS0Qf+k6IPKGp5LoRMXGxCq16e+mF4=",
"passwordSalt": "Ns8Al6DpqPsIDlCSRBVTEg==",
"files": []
}
]
}