-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
87 lines (73 loc) · 2.15 KB
/
Program.cs
File metadata and controls
87 lines (73 loc) · 2.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
using System.Diagnostics;
using iText.Kernel.Exceptions;
namespace PdfSignabilityCheckerTool;
internal static class Program
{
static int Main(string[] args)
{
bool treatUnencryptedAsSignable = args.Contains(Constants.TreatUnencryptedAsSignable);
bool isDebug = Debugger.IsAttached;
if (!Console.IsInputRedirected && !isDebug)
{
Console.Error.WriteLine(Constants.NoInputErrorMessage);
Environment.Exit(1);
}
Stream stdin;
if (isDebug)
{
string? filePath = null;
Debugger.Break();
stdin = new MemoryStream(File.ReadAllBytes(filePath!));
}
else
{
stdin = Console.OpenStandardInput();
}
bool isSignable;
using var ms = new MemoryStream();
stdin.CopyTo(ms);
try
{
PdfSignabilityChecker pdfSignabilityChecker = new(ms, GetSigFieldName(args));
isSignable = pdfSignabilityChecker.IsSignable(treatUnencryptedAsSignable);
}
catch (BadPasswordException)
{
Console.Error.WriteLine(Constants.PasswordProtectedErrorMessage);
WriteFalse();
return 2;
}
catch (PdfException ex) when (ex.Message.Contains("Certificate is not provided"))
{
Console.Error.WriteLine(Constants.CertificateProtectedErrorMessage);
WriteFalse();
return 66;
}
catch (Exception ex)
{
Console.Error.WriteLine($"Error: {ex}");
WriteFalse();
return 99;
}
stdin.Close();
Console.Write(isSignable
? "true"
: "false");
return isSignable ? 0 : 1;
}
private static string GetSigFieldName(string[] args)
{
for (int i = 0; i < args.Length; i++)
{
if (args[i].Equals("-sigfieldname", StringComparison.OrdinalIgnoreCase) && i + 1 < args.Length)
{
return args[i + 1];
}
}
return "";
}
private static void WriteFalse()
{
Console.Write("false");
}
}