View Javadoc

1   /********************************************************************************
2    *InternetCafe is a software solution that helps the management of Cybercafes 
3    * according with the ITALIAN DECREE LAW ON ANTI-TERROR MEASURES, 27 JULY 2005.
4    * Copyright (C) 2006  Guido Angelo Ingenito
5   
6    * This program is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU General Public License
8    * as published by the Free Software Foundation; either version 2
9    * of the License, or (at your option) any later version.
10  
11   * This program is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.
15   * 
16   * You should have received a copy of the GNU General Public License
17   * along with this program; if not, write to the Free Software
18   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19   *******************************************************************************/
20  package base.jdbs;
21  
22  import java.io.File;
23  import java.io.IOException;
24  
25  import org.apache.log4j.Logger;
26  
27  import base.jdbs.cryptography.asymmetric.KeyPair;
28  import base.jdbs.cryptography.asymmetric.RSAAsymmetricCipher;
29  import base.jdbs.cryptography.symmetric.DESSymmetricCipher;
30  import base.jdbs.cryptography.symmetric.SymmetricKey;
31  import base.util.FileUtil;
32  import base.util.ZipUtil;
33  
34  /***
35   * This class provided methods and utils to disassembly a previously created backup artifact.
36   * @author Guido Angelo Ingenito
37   */
38  public class BackupArtifactDisassembler {
39  
40  	private static final transient Logger logger = Logger.getLogger(BackupArtifactDisassembler.class.getName());
41  
42  	public static void disassemblyBackupArtifact(SymmetricKey symmetricKey, KeyPair keyPair, BackupDescriptor backupDescriptor, File outputDirectory) throws IOException{
43  		
44  		String originalCRC32 = FileUtil.fileContent(backupDescriptor.getCrc32ChecksumFile());
45  		logger.debug("originalCRC32: "+originalCRC32);
46  		boolean crc32Check = FileUtil.checkCRC32Checksum(backupDescriptor.getBackupFile(),new Long(originalCRC32));
47  		if(crc32Check)logger.debug("CRC32 Check - VALID");
48  		else logger.warn("CRC32 - INVALID");
49  		
50  		
51  		String originalMD5Digest = FileUtil.fileContent(backupDescriptor.getMd5DigestFile());
52  		logger.debug("originalMD5Digest: "+originalMD5Digest);
53  		boolean md5Check = FileUtil.checkMD5Digest(backupDescriptor.getBackupFile(),originalMD5Digest);
54  		if(md5Check)logger.debug("MD5 Check - VALID");
55  		else logger.warn("MD5 Check - INVALID");
56  		
57  		
58  		RSAAsymmetricCipher asymmetricCipher = new RSAAsymmetricCipher();
59  		
60  		boolean signatureCheck = asymmetricCipher.checkSignature(backupDescriptor.getMd5SignedFile(),backupDescriptor.getMd5DigestFile(), keyPair.getPublicKey());
61  		if(signatureCheck)logger.debug("Signature Check - VALID");
62  		else logger.warn("Signature Check - INVALID");
63  		
64  		final File encryptionDir = new File(backupDescriptor.getArtifactDirectory(),BackupArtifactAssembler.ENCRYPTION_DIRECTORY_NAME);
65  		final File decryptionDir = new File(backupDescriptor.getArtifactDirectory(),BackupArtifactAssembler.DECRYPTION_DIRECTORY_NAME);
66  		
67  		logger.debug("Backup Encryption Directory: "+ encryptionDir);
68  		encryptionDir.mkdir();
69  		logger.debug("Backup Decryption Directory: "+ decryptionDir);
70  		decryptionDir.mkdir();
71  		
72  		logger.debug("The Backup Security Level is : "+backupDescriptor.getBackup().getSecurityLevel());
73  		if(backupDescriptor.getBackup().getSecurityLevel().equals(SecurityLevel.PRIVATE)){
74  			logger.debug("Backup has PRIVATE content, unzipping the content under the encryption directory");
75  			ZipUtil.unZipFile(backupDescriptor.getBackupFile(),encryptionDir);
76  			logger.debug("Backup successfully unzipped, starting the decryption.");
77  			DESSymmetricCipher symmetricCipher = new DESSymmetricCipher(symmetricKey);
78  			logger.debug("This is the list of file o be decrypted:");
79  			File[] toBeDecrypted = FileUtil.allFileContent(encryptionDir);
80  			File[] outputFile = new File[toBeDecrypted.length];
81  			
82  			
83  			for(int i=0;i<toBeDecrypted.length;i++){
84  				logger.debug("To Be Decrypted: "+toBeDecrypted[i]);
85  				int lastIndexOfBakcupName = toBeDecrypted[i].getAbsolutePath().lastIndexOf(backupDescriptor.getBackup().getName());
86  				int pathLength = toBeDecrypted[i].getAbsolutePath().length();
87  				logger.debug("lastIndexOfBakcupName: "+lastIndexOfBakcupName + "pathLength: "+pathLength);
88  				String originalFileName = toBeDecrypted[i].getAbsolutePath().substring(lastIndexOfBakcupName,pathLength);
89  				logger.debug("Original File Name: "+ originalFileName);
90  				
91  				outputFile[i] = new File(outputDirectory,originalFileName);
92  				outputFile[i].getParentFile().mkdirs();
93  				outputFile[i].createNewFile();
94  				
95  				symmetricCipher.decrypt(toBeDecrypted[i],outputFile[i]);
96  			}
97  		}
98  		else{
99  			logger.debug("Backup has private content, unzipping the content under the decryption directory");
100 			ZipUtil.unZipFile(backupDescriptor.getBackupFile(),decryptionDir);
101 			File[] toBeResotored = FileUtil.allFileContent(decryptionDir);
102 			for(int i=0;i<toBeResotored.length;i++)
103 			{
104 				logger.debug("To Be Restored: "+toBeResotored[i]);
105 				int lastIndexOfBakcupName = toBeResotored[i].getAbsolutePath().lastIndexOf(backupDescriptor.getBackup().getName());
106 				int pathLength = toBeResotored[i].getAbsolutePath().length();
107 				logger.debug("lastIndexOfBakcupName: "+lastIndexOfBakcupName + "pathLength: "+pathLength);
108 				String originalFileName = toBeResotored[i].getAbsolutePath().substring(lastIndexOfBakcupName,pathLength);
109 				logger.debug("Original File Name: "+ originalFileName);
110 				
111 				File outputFile = new File(outputDirectory,originalFileName);
112 				outputFile.getParentFile().mkdirs();
113 				outputFile.createNewFile();
114 				FileUtil.copyFile(toBeResotored[i],outputFile);
115 			}
116 			
117 		}
118 		
119 		logger.debug("Encryption Directory Deletion Result: " + FileUtil.deleteDirectory(encryptionDir));
120 		logger.debug("Decryption Directory Deletion Result: " + FileUtil.deleteDirectory(decryptionDir));
121 		
122 		
123 	
124 	}
125 	
126 }