markus / MarkusAutoUpdate / src / NetSparkle.Tools.DSAHelper / Program.cs @ d8f5045e
이력 | 보기 | 이력해설 | 다운로드 (6.94 KB)
1 |
using System; |
---|---|
2 |
using System.Collections.Generic; |
3 |
using System.Linq; |
4 |
using System.Text; |
5 |
using System.IO; |
6 |
using System.Security.Cryptography; |
7 |
using NetSparkleUpdater.Enums; |
8 |
|
9 |
namespace NetSparkleUpdater.DSAHelper |
10 |
{ |
11 |
class Program |
12 |
{ |
13 |
private static string _dsaPrivKey = "NetSparkle_DSA.priv"; |
14 |
private static string _dsaPubKey = "NetSparkle_DSA.pub"; |
15 |
|
16 |
static void Main(string[] args) |
17 |
{ |
18 |
try |
19 |
{ |
20 |
// check if we have some parameters |
21 |
if (args.Count() < 1) |
22 |
{ |
23 |
Usage(); |
24 |
return; |
25 |
} |
26 |
|
27 |
// check what parameter we have |
28 |
switch (args[0].ToLower()) |
29 |
{ |
30 |
case "/genkey_pair": |
31 |
{ |
32 |
// show headline |
33 |
ShowHeadLine(); |
34 |
|
35 |
// verify if output file exists |
36 |
if (File.Exists(_dsaPrivKey) || File.Exists(_dsaPubKey)) |
37 |
{ |
38 |
Console.WriteLine("Error: Output files are currently exists"); |
39 |
Environment.ExitCode = -1; |
40 |
return; |
41 |
} |
42 |
|
43 |
// start key generation |
44 |
Console.WriteLine("Generating key pair with 1024 Bits..."); |
45 |
DSACryptoServiceProvider prv = new DSACryptoServiceProvider(); |
46 |
|
47 |
Console.WriteLine("Storing private key to " + _dsaPrivKey); |
48 |
using (StreamWriter sw = new StreamWriter(_dsaPrivKey)) |
49 |
{ |
50 |
sw.Write(prv.ToXmlString(true)); |
51 |
} |
52 |
|
53 |
Console.WriteLine("Storing public key to " + _dsaPubKey); |
54 |
using (StreamWriter sw = new StreamWriter(_dsaPubKey)) |
55 |
{ |
56 |
sw.Write(prv.ToXmlString(false)); |
57 |
} |
58 |
|
59 |
Console.WriteLine(""); |
60 |
} |
61 |
break; |
62 |
case "/sign_update": |
63 |
{ |
64 |
if (args.Count() != 3) |
65 |
{ |
66 |
Usage(); |
67 |
Environment.ExitCode = -1; |
68 |
return; |
69 |
} |
70 |
|
71 |
// get parameter |
72 |
String binary = args[1]; |
73 |
String privKey = args[2]; |
74 |
|
75 |
if (!File.Exists(binary)) |
76 |
{ |
77 |
Console.Error.WriteLine("Target binary " + binary + " does not exists"); |
78 |
Environment.ExitCode = -1; |
79 |
return; |
80 |
} |
81 |
|
82 |
if (!File.Exists(privKey)) |
83 |
{ |
84 |
Console.Error.WriteLine("Private key file does not exists"); |
85 |
Environment.ExitCode = -1; |
86 |
return; |
87 |
} |
88 |
|
89 |
Console.WriteLine(Utilities.GetDSASignature(binary, privKey)); |
90 |
} |
91 |
break; |
92 |
case "/verify_update": |
93 |
{ |
94 |
if (args.Count() != 4) |
95 |
{ |
96 |
Usage(); |
97 |
Environment.ExitCode = -1; |
98 |
return; |
99 |
} |
100 |
|
101 |
// get parameter |
102 |
string binary = args[1]; |
103 |
string pubKeyFile = args[2]; |
104 |
string sign = args[3]; |
105 |
|
106 |
sign = sign.TrimStart('"'); |
107 |
sign = sign.TrimEnd('"'); |
108 |
|
109 |
NetSparkleUpdater.SignatureVerifiers.DSAChecker dsaVerif = |
110 |
new NetSparkleUpdater.SignatureVerifiers.DSAChecker(SecurityMode.UseIfPossible, null, pubKeyFile); |
111 |
switch (dsaVerif.VerifySignatureOfFile(sign, binary)) |
112 |
{ |
113 |
case ValidationResult.Valid: |
114 |
Console.WriteLine("Binary " + binary + " is valid"); |
115 |
break; |
116 |
case ValidationResult.Invalid: |
117 |
Console.WriteLine("Binary " + binary + " is NOT valid"); |
118 |
break; |
119 |
case ValidationResult.Unchecked: |
120 |
Console.WriteLine("Binary " + binary + " could not be checked"); |
121 |
break; |
122 |
} |
123 |
} |
124 |
break; |
125 |
default: |
126 |
Usage(); |
127 |
break; |
128 |
} |
129 |
} |
130 |
catch (Exception e) |
131 |
{ |
132 |
Console.WriteLine("Something went wrong :-("); |
133 |
Console.WriteLine(e.StackTrace); |
134 |
} |
135 |
} |
136 |
|
137 |
static private void Usage() |
138 |
{ |
139 |
ShowHeadLine(); |
140 |
|
141 |
Console.WriteLine("NetSparkle.DSAHelper.exe /genkey_pair"); |
142 |
Console.WriteLine(""); |
143 |
Console.WriteLine("Generates a public and a private DSA key pair which is stored in the current"); |
144 |
Console.WriteLine("working directory. The private is stored in the file NetSparkle_DSA.priv"); |
145 |
Console.WriteLine("The public key will be stored in a file named NetSparkle_DSA.pub. Add the"); |
146 |
Console.WriteLine("public key file as resource to your application."); |
147 |
Console.WriteLine(""); |
148 |
Console.WriteLine("NetSparkle.DSAHelper.exe /sign_update {YourPackage.msi} {NetSparkle_DSA.priv}"); |
149 |
Console.WriteLine(""); |
150 |
Console.WriteLine("Allows to sign an existing update package unattended. YourPackage.msi has to be"); |
151 |
Console.WriteLine("a valid path to the package binary as self (mostly Windows Installer packages)."); |
152 |
Console.WriteLine("The NetSparkle_DSA.priv has to be a path to the generated DAS private key,"); |
153 |
Console.WriteLine("which has to be used for signing."); |
154 |
Console.WriteLine(""); |
155 |
Console.WriteLine("NetSparkle.DSAHelper.exe /verify_update {YourPackage.msi} {NetSparkle_DSA.pub} \"{Base64SignatureString}\""); |
156 |
Console.WriteLine(""); |
157 |
|
158 |
} |
159 |
|
160 |
private static void ShowHeadLine() |
161 |
{ |
162 |
Console.WriteLine("NetSparkle DSA Helper"); |
163 |
Console.WriteLine("(c) 2011 Dirk Eisenberg under the terms of MIT license"); |
164 |
Console.WriteLine(""); |
165 |
} |
166 |
} |
167 |
} |