Esempio n. 1
0
 public static void VerifyFile(byte[] publicKey, string signatureFilePath, string filePath)
 {
     try
     {
         bool validSignature = DigitalSignatures.VerifySignature(signatureFilePath, filePath, publicKey);
         if (!validSignature)
         {
             DisplayMessage.FilePathMessage(filePath, "Bad signature.");
             return;
         }
         DisplayMessage.FilePathMessage(filePath, "Good signature.");
         string comment = DigitalSignatures.GetComment();
         DisplayMessage.Message($"Authenticated comment: {comment}");
     }
     catch (Exception ex) when(ExceptionFilters.Cryptography(ex))
     {
         Logging.LogException(ex.ToString(), Logging.Severity.Error);
         if (ex is ArgumentOutOfRangeException)
         {
             DisplayMessage.Exception(ex.GetType().Name, ex.Message);
             return;
         }
         DisplayMessage.Exception(ex.GetType().Name, "Unable to verify signature.");
     }
 }
Esempio n. 2
0
 public static byte[] GetPrivateKeyFromFile(string privateKeyPath)
 {
     try
     {
         string encodedPrivateKey = File.ReadAllText(privateKeyPath);
         if (encodedPrivateKey.Length != Constants.EncryptionPrivateKeyLength && encodedPrivateKey.Length != Constants.SigningPrivateKeyLength)
         {
             DisplayMessage.Error(ValidationMessages.PrivateKeyFile);
             return(null);
         }
         byte[] privateKey = Convert.FromBase64String(encodedPrivateKey);
         ValidateKeyVersion(privateKey);
         return(privateKey);
     }
     catch (Exception ex) when(ExceptionFilters.AsymmetricKeyHandling(ex))
     {
         if (ex is ArgumentOutOfRangeException)
         {
             DisplayMessage.Exception(ex.GetType().Name, ex.Message);
             return(null);
         }
         DisplayMessage.Exception(ex.GetType().Name, "Unable to retrieve private key.");
         return(null);
     }
 }
Esempio n. 3
0
 private static byte[] UseKeyfile(byte[] passwordBytes, string keyfilePath)
 {
     try
     {
         byte[] keyfileBytes = Keyfiles.ReadKeyfile(keyfilePath);
         return(passwordBytes == null ? keyfileBytes : CombineKeyfileAndPassword(passwordBytes, keyfileBytes));
     }
     catch (Exception ex) when(ExceptionFilters.FileAccess(ex))
     {
         DisplayMessage.Exception(ex.GetType().Name, "Unable to read keyfile. The keyfile has not been used.");
         return(passwordBytes);
     }
 }
Esempio n. 4
0
 public static void LogException(string exceptionMessage, Severity severity)
 {
     try
     {
         string logMessage = $"Exception Severity = {severity}" + Environment.NewLine + exceptionMessage + Environment.NewLine;
         if (!Directory.Exists(_logdirectoryPath))
         {
             Directory.CreateDirectory(_logdirectoryPath);
         }
         File.AppendAllText(_logFilePath, logMessage);
     }
     catch (Exception ex) when(ExceptionFilters.FileAccess(ex))
     {
         DisplayMessage.Exception(ex.GetType().Name, "Unable to log exception.");
     }
 }
Esempio n. 5
0
 public static void CheckForUpdates()
 {
     try
     {
         bool updateAvailable = Updates.CheckForUpdates();
         if (updateAvailable)
         {
             Console.WriteLine("An update is available for Kryptor. Visit <https://github.com/samuel-lucas6/Kryptor/releases> to update.");
             return;
         }
         Console.WriteLine("Kryptor is up-to-date.");
     }
     catch (Exception ex) when(ExceptionFilters.CheckForUpdates(ex))
     {
         DisplayMessage.Exception(ex.GetType().Name, "Unable to check for updates.");
     }
 }
Esempio n. 6
0
 private static byte[] GetPublicKeyFromFile(string publicKeyPath)
 {
     try
     {
         string encodedPublicKey = File.ReadAllText(publicKeyPath);
         if (encodedPublicKey.Length != Constants.PublicKeyLength)
         {
             DisplayMessage.Error(ValidationMessages.PublicKeyString);
             return(null);
         }
         return(Convert.FromBase64String(encodedPublicKey));
     }
     catch (Exception ex) when(ExceptionFilters.AsymmetricKeyHandling(ex))
     {
         DisplayMessage.Exception(ex.GetType().Name, "Unable to retrieve public key.");
         return(null);
     }
 }
Esempio n. 7
0
 public static byte[] EncryptionPublicKeyString(char[] encodedPublicKey)
 {
     try
     {
         byte[] publicKey = Convert.FromBase64CharArray(encodedPublicKey, offset: 0, encodedPublicKey.Length);
         if (publicKey == null)
         {
             return(null);
         }
         byte[] keyAlgorithm = GetKeyAlgorithm(publicKey);
         ValidateEncryptionKey(keyAlgorithm);
         return(RemoveKeyAlgorithmHeader(publicKey));
     }
     catch (Exception ex) when(ExceptionFilters.AsymmetricKeyHandling(ex))
     {
         DisplayMessage.Exception(ex.GetType().Name, "Please enter a valid encryption public key.");
         return(null);
     }
 }
Esempio n. 8
0
 public static byte[] SigningPublicKeyFile(string publicKeyPath)
 {
     try
     {
         byte[] publicKey = GetPublicKeyFromFile(publicKeyPath);
         if (publicKey == null)
         {
             return(null);
         }
         byte[] keyAlgorithm = GetKeyAlgorithm(publicKey);
         ValidateSigningKey(keyAlgorithm);
         return(RemoveKeyAlgorithmHeader(publicKey));
     }
     catch (Exception ex) when(ExceptionFilters.AsymmetricKeyHandling(ex))
     {
         DisplayMessage.Exception(ex.GetType().Name, "Please specify a valid signing public key.");
         return(null);
     }
 }
Esempio n. 9
0
 public static byte[] EncryptionPrivateKeyFile(string privateKeyPath)
 {
     try
     {
         byte[] privateKey = GetPrivateKeyFromFile(privateKeyPath);
         if (privateKey == null)
         {
             return(null);
         }
         byte[] keyAlgorithm = GetKeyAlgorithm(privateKey);
         ValidateEncryptionKey(keyAlgorithm);
         return(privateKey);
     }
     catch (Exception ex) when(ExceptionFilters.AsymmetricKeyHandling(ex))
     {
         DisplayMessage.Exception(ex.GetType().Name, "Please specify a valid encryption private key.");
         return(null);
     }
 }
Esempio n. 10
0
 public static void VerifyFile(string signatureFilePath, string filePath, byte[] publicKey)
 {
     try
     {
         bool validSignature = DigitalSignatures.VerifySignature(signatureFilePath, filePath, publicKey, out string comment);
         if (!validSignature)
         {
             DisplayMessage.FilePathMessage(filePath, "Bad signature.");
             return;
         }
         DisplayMessage.FilePathMessage(filePath, "Good signature.");
         DisplayMessage.Message($"Authenticated comment: {comment}");
     }
     catch (Exception ex) when(ExceptionFilters.Cryptography(ex))
     {
         if (ex is ArgumentException)
         {
             DisplayMessage.FilePathMessage(signatureFilePath, ex.Message);
             return;
         }
         DisplayMessage.Exception(ex.GetType().Name, "Unable to verify signature.");
     }
 }