public static bool VerifyFile(byte[] certificateRawData, string verifyFilename) { try { FileCertVerifier verifier = new FileCertVerifier(certificateRawData); return(verifier.VerifyFile(verifyFilename)); } catch (CryptographicException) { return(false); } }
private static void VerifyFile(FileCertVerifier verifier, string checkFilePath) { try { Console.WriteLine("Check file: {0}", checkFilePath); Console.WriteLine("File certicicate valid: {0}", verifier.VerifyFileCertificate(checkFilePath)); Console.WriteLine("File signature valid: {0}", verifier.VerifyFileSignature(checkFilePath)); Console.WriteLine("File valid: {0}", verifier.VerifyFile(checkFilePath)); } catch (CryptographicException e) { Console.WriteLine("Verify error: {0}", e); } }
public InstallStatus Install(string zipFileName, string destinationFolder) { if (!Directory.Exists(destinationFolder)) { _logger.Error($"Install directory is not exist: {destinationFolder}"); return(InstallStatus.FileError); } DirectoryInfo?unpackDataDir = null; DirectoryInfo?backupDataDir = null; var dataPathDir = new DirectoryInfo(GameConstants.GetDataFolderPath(destinationFolder)); try { var unpackDataDirPath = Path.Combine(destinationFolder, "temp_" + Path.GetRandomFileName()); unpackDataDir = Directory.CreateDirectory(unpackDataDirPath); if (!Unpack(zipFileName, unpackDataDir.FullName)) { _logger.Error($"Failed unpack install package to: {unpackDataDirPath}"); return(InstallStatus.PackageError); } var newLibraryPath = Path.Combine(unpackDataDir.FullName, GameConstants.PatcherOriginalName); using var libraryCertVerifier = new FileCertVerifier(Resources.CoreSigning); if (!libraryCertVerifier.VerifyFile(newLibraryPath)) { _logger.Error("Core certificate is invalid. Abort installation"); return(InstallStatus.VerifyError); } if (dataPathDir.Exists) { var backupDataDirPath = Path.Combine(destinationFolder, "backup_" + Path.GetRandomFileName()); Directory.Move(dataPathDir.FullName, backupDataDirPath); backupDataDir = new DirectoryInfo(backupDataDirPath); } Directory.Move(GameConstants.GetDataFolderPath(unpackDataDir.FullName), dataPathDir.FullName); if (backupDataDir != null) { FileUtils.DeleteDirectoryNoThrow(backupDataDir, true); backupDataDir = null; } InstallCore(newLibraryPath, destinationFolder); } catch (CryptographicException e) { _logger.Error(e, "Exception during verify core"); return(InstallStatus.VerifyError); } catch (IOException e) { _logger.Error(e, "I/O exception during install"); return(InstallStatus.FileError); } catch (Exception e) { _logger.Error(e, "Unexpected exception during install"); return(InstallStatus.UnknownError); } finally { if (unpackDataDir != null) { FileUtils.DeleteDirectoryNoThrow(unpackDataDir, true); } if (backupDataDir != null) { RestoreDirectory(backupDataDir, dataPathDir); } } return(InstallStatus.Success); }
public InstallStatus Install(string sourceFolder, string destinationFolder, FilesIndex.DiffList diffList) { if (!Directory.Exists(destinationFolder)) { _logger.Error($"Install directory is not exist: {destinationFolder}"); return(InstallStatus.FileError); } if (diffList.ChangedFiles.Count == 0 && diffList.IsReuseNotChangeFileNames()) { foreach (var removeFile in diffList.RemoveFiles) { FileUtils.DeleteFileNoThrow(Path.Combine(destinationFolder, removeFile)); } return(InstallStatus.Success); } var movedReusedFiles = new Dictionary <string, string>(); DirectoryInfo?backupDataDir = null; var dataPathDir = new DirectoryInfo(GameConstants.GetDataFolderPath(destinationFolder)); try { var newLibraryPath = Path.Combine(sourceFolder, GameConstants.PatcherOriginalName); bool newLibraryExist = File.Exists(newLibraryPath); if (newLibraryExist) { using var libraryCertVerifier = new FileCertVerifier(Resources.CoreSigning); if (!libraryCertVerifier.VerifyFile(newLibraryPath)) { _logger.Error("Core certificate is invalid. Abort installation"); return(InstallStatus.VerifyError); } } foreach (var reusePair in diffList.ReuseFiles) { if (!reusePair.Key.Equals(GameConstants.PatcherOriginalName, StringComparison.OrdinalIgnoreCase)) { var sourceReusePath = Path.Combine(destinationFolder, reusePair.Key); var destReusePath = Path.Combine(sourceFolder, reusePair.Value); Directory.CreateDirectory(Path.GetDirectoryName(destReusePath)); File.Move(sourceReusePath, destReusePath); movedReusedFiles.Add(destReusePath, sourceReusePath); } } if (dataPathDir.Exists) { var backupDataDirPath = Path.Combine(destinationFolder, "backup_" + Path.GetRandomFileName()); Directory.Move(dataPathDir.FullName, backupDataDirPath); backupDataDir = new DirectoryInfo(backupDataDirPath); } Directory.Move(GameConstants.GetDataFolderPath(sourceFolder), dataPathDir.FullName); if (backupDataDir != null) { FileUtils.DeleteDirectoryNoThrow(backupDataDir, true); backupDataDir = null; } if (newLibraryExist) { InstallCore(newLibraryPath, destinationFolder); } movedReusedFiles.Clear(); } catch (CryptographicException e) { _logger.Error(e, "Exception during verify core"); return(InstallStatus.VerifyError); } catch (IOException e) { _logger.Error(e, "I/O exception during install"); return(InstallStatus.FileError); } catch (Exception e) { _logger.Error(e, "Unexpected exception during install"); return(InstallStatus.UnknownError); } finally { if (backupDataDir != null) { RestoreDirectory(backupDataDir, dataPathDir); } foreach (var pair in movedReusedFiles) { FileUtils.MoveFileNoThrow(pair.Key, pair.Value); } } return(InstallStatus.Success); }
public InstallStatus Install(string zipFileName, string destinationFolder) { DirectoryInfo?unpackDataDir = null; DirectoryInfo?backupDataDir = null; var dataPathDir = new DirectoryInfo(GameConstants.GetDataFolderPath(destinationFolder)); try { var unpackDataDirPath = Path.Combine(destinationFolder, "temp_" + Path.GetRandomFileName()); unpackDataDir = Directory.CreateDirectory(unpackDataDirPath); if (!Unpack(zipFileName, unpackDataDir.FullName)) { return(InstallStatus.PackageError); } var newLibraryPath = Path.Combine(unpackDataDir.FullName, GameConstants.PatcherOriginalName); using var libraryCertVerifier = new FileCertVerifier(Resources.CoreSigning); if (!libraryCertVerifier.VerifyFile(newLibraryPath)) { return(InstallStatus.VerifyError); } if (dataPathDir.Exists) { var backupDataDirPath = Path.Combine(destinationFolder, "backup_" + Path.GetRandomFileName()); Directory.Move(dataPathDir.FullName, backupDataDirPath); backupDataDir = new DirectoryInfo(backupDataDirPath); } Directory.Move(GameConstants.GetDataFolderPath(unpackDataDir.FullName), dataPathDir.FullName); if (backupDataDir != null) { FileUtils.DeleteDirectoryNoThrow(backupDataDir, true); backupDataDir = null; } var enabledLibraryPath = GameConstants.GetEnabledPatcherPath(destinationFolder); if (File.Exists(enabledLibraryPath)) { File.Delete(enabledLibraryPath); File.Move(newLibraryPath, enabledLibraryPath); } else { var disabledLibraryPath = GameConstants.GetDisabledPatcherPath(destinationFolder); if (File.Exists(disabledLibraryPath)) { File.Delete(disabledLibraryPath); } File.Move(newLibraryPath, disabledLibraryPath); } } catch (CryptographicException) { return(InstallStatus.VerifyError); } catch (IOException) { return(InstallStatus.FileError); } catch (Exception) { return(InstallStatus.UnknownError); } finally { if (unpackDataDir != null) { FileUtils.DeleteDirectoryNoThrow(unpackDataDir, true); } if (backupDataDir != null) { RestoreDirectory(backupDataDir, dataPathDir); } } return(InstallStatus.Success); }