Skip to content

Commit 1c66291

Browse files
Fix flow order in sign executor
1 parent b07d671 commit 1c66291

File tree

3 files changed

+91
-88
lines changed

3 files changed

+91
-88
lines changed

src/Otor.MsixHero.Cli.Verbs/Resources/Localization.Designer.cs

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Otor.MsixHero.Cli/Executors/Standard/SignVerbExecutor.cs

Lines changed: 88 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -61,24 +61,7 @@ public override async Task<int> Execute()
6161

6262
var config = await _configurationService.GetCurrentConfigurationAsync().ConfigureAwait(false);
6363

64-
if (config.Signing?.Source == CertificateSource.Unknown)
65-
{
66-
// workaround for some migration issues
67-
if (!string.IsNullOrEmpty(config.Signing.PfxPath))
68-
{
69-
config.Signing.Source = CertificateSource.Pfx;
70-
}
71-
else if (!string.IsNullOrEmpty(config.Signing.Thumbprint))
72-
{
73-
config.Signing.Source = CertificateSource.Personal;
74-
}
75-
else
76-
{
77-
await this.Console.WriteError(Resources.Localization.CLI_Executor_Sign_Error_NoConfig).ConfigureAwait(false);
78-
return 1;
79-
}
80-
}
81-
64+
// Signing with thumbprint
8265
if (this.Verb.ThumbPrint != null)
8366
{
8467
return await this.SignStore(
@@ -87,6 +70,7 @@ public override async Task<int> Execute()
8770
!this.Verb.NoPublisherUpdate).ConfigureAwait(false);
8871
}
8972

73+
// Signing with PFX
9074
if (this.Verb.PfxFilePath != null)
9175
{
9276
return await this.SignPfx(
@@ -96,13 +80,14 @@ public override async Task<int> Execute()
9680
!this.Verb.NoPublisherUpdate).ConfigureAwait(false);
9781
}
9882

83+
// Signing with Device Guard (interactive)
9984
if (this.Verb.DeviceGuardInteractive)
10085
{
10186
return await this.SignDeviceGuardInteractive(
102-
this.Verb.TimeStampUrl ?? config.Signing?.TimeStampServer,
103-
!this.Verb.NoPublisherUpdate).ConfigureAwait(false);
87+
this.Verb.TimeStampUrl ?? config.Signing?.TimeStampServer, !this.Verb.NoPublisherUpdate).ConfigureAwait(false);
10488
}
10589

90+
// Signing with Device Guard
10691
if (this.Verb.DeviceGuardFile != null)
10792
{
10893
var json = JObject.Parse(await File.ReadAllTextAsync(this.Verb.DeviceGuardFile).ConfigureAwait(false));
@@ -126,72 +111,10 @@ public override async Task<int> Execute()
126111
!this.Verb.NoPublisherUpdate).ConfigureAwait(false);
127112
}
128113

129-
await this.Console.WriteInfo(Resources.Localization.CLI_Executor_Sign_UsingCurrent).ConfigureAwait(false);
130-
131-
switch (config.Signing?.Source)
132-
{
133-
case CertificateSource.Pfx:
134-
string password = null;
135-
136-
if (!string.IsNullOrEmpty(config.Signing?.EncodedPassword))
137-
{
138-
var crypto = new Crypto();
139-
140-
try
141-
{
142-
password = crypto.UnprotectUnsafe(config.Signing?.EncodedPassword);
143-
}
144-
catch
145-
{
146-
Logger.Warn().WriteLine("It seems that your are using the old-way of protecting password. MSIX Hero will try to use the legacy method now, but consider updating your settings so that the password will be safely encrypted.");
147-
await this.Console.WriteWarning("Could not use the configured password. Decryption of the string from settings failed.").ConfigureAwait(false);
148-
149-
try
150-
{
151-
// ReSharper disable StringLiteralTypo
152-
#pragma warning disable CS0618
153-
password = crypto.DecryptString(config.Signing?.EncodedPassword, @"$%!!ASddahs55839AA___ąółęńśSdcvv");
154-
#pragma warning restore CS0618
155-
// ReSharper restore StringLiteralTypo
156-
}
157-
catch (Exception)
158-
{
159-
Logger.Error().WriteLine(Resources.Localization.CLI_Executor_Sign_Error_DecryptFailed);
160-
await this.Console.WriteError(Resources.Localization.CLI_Executor_Sign_Error_DecryptFailed).ConfigureAwait(false);
161-
return StandardExitCodes.ErrorSettings;
162-
}
163-
}
164-
}
165-
166-
return await this.SignPfx(
167-
config.Signing?.PfxPath?.Resolved,
168-
password,
169-
this.Verb.TimeStampUrl ?? config.Signing?.TimeStampServer,
170-
!this.Verb.NoPublisherUpdate).ConfigureAwait(false);
171-
case CertificateSource.Personal:
172-
return await this.SignStore(
173-
config.Signing.Thumbprint,
174-
this.Verb.TimeStampUrl ?? config.Signing?.TimeStampServer,
175-
!this.Verb.NoPublisherUpdate);
176-
case CertificateSource.DeviceGuard:
177-
if (config.Signing.DeviceGuard == null)
178-
{
179-
Logger.Error().WriteLine(Resources.Localization.CLI_Executor_Sign_Error_DeviceGuardNoConfig);
180-
await this.Console.WriteError(Resources.Localization.CLI_Executor_Sign_Error_DeviceGuardNoConfig).ConfigureAwait(false);
181-
return StandardExitCodes.ErrorSettings;
182-
}
183-
184-
return await this.SignDeviceGuard(
185-
config.Signing.DeviceGuard.FromConfiguration(),
186-
this.Verb.TimeStampUrl ?? config.Signing?.TimeStampServer,
187-
!this.Verb.NoPublisherUpdate);
188-
default:
189-
Logger.Error().WriteLine(Resources.Localization.CLI_Executor_Sign_Error_NoCertAndDefaultConfig);
190-
await this.Console.WriteError(Resources.Localization.CLI_Executor_Sign_Error_NoCertAndDefaultConfig).ConfigureAwait(false);
191-
return StandardExitCodes.ErrorSettings;
192-
}
114+
// Fallback - try to get MSIX Hero default settings
115+
return await this.SignDefault().ConfigureAwait(false);
193116
}
194-
117+
195118
private static string GetOptionName(string propertyName)
196119
{
197120
var property = typeof(SignVerb).GetProperty(propertyName);
@@ -329,6 +252,86 @@ private async Task<int> AssertCorrectCommandLine()
329252

330253
return StandardExitCodes.ErrorSuccess;
331254
}
255+
256+
private async Task<int> SignDefault()
257+
{
258+
var config = await this._configurationService.GetCurrentConfigurationAsync().ConfigureAwait(false);
259+
260+
if (config.Signing?.Source == CertificateSource.Unknown)
261+
{
262+
// workaround for some migration issues
263+
if (!string.IsNullOrEmpty(config.Signing.PfxPath))
264+
{
265+
config.Signing.Source = CertificateSource.Pfx;
266+
}
267+
else if (!string.IsNullOrEmpty(config.Signing.Thumbprint))
268+
{
269+
config.Signing.Source = CertificateSource.Personal;
270+
}
271+
else
272+
{
273+
await this.Console.WriteError(Resources.Localization.CLI_Executor_Sign_Error_NoConfig).ConfigureAwait(false);
274+
return 1;
275+
}
276+
}
277+
278+
await this.Console.WriteInfo(Resources.Localization.CLI_Executor_Sign_UsingCurrent).ConfigureAwait(false);
279+
280+
switch (config.Signing?.Source)
281+
{
282+
case CertificateSource.Pfx:
283+
string password = null;
284+
285+
if (!string.IsNullOrEmpty(config.Signing?.EncodedPassword))
286+
{
287+
var crypto = new Crypto();
288+
289+
try
290+
{
291+
password = crypto.UnprotectUnsafe(config.Signing?.EncodedPassword);
292+
}
293+
catch
294+
{
295+
Logger.Warn().WriteLine("It seems that your are using the old-way of protecting password. MSIX Hero will try to use the legacy method now, but consider updating your settings so that the password will be safely encrypted.");
296+
await this.Console.WriteWarning("Could not use the configured password. Decryption of the string from settings failed.").ConfigureAwait(false);
297+
298+
try
299+
{
300+
// ReSharper disable StringLiteralTypo
301+
#pragma warning disable CS0618
302+
password = crypto.DecryptString(config.Signing?.EncodedPassword, @"$%!!ASddahs55839AA___ąółęńśSdcvv");
303+
#pragma warning restore CS0618
304+
// ReSharper restore StringLiteralTypo
305+
}
306+
catch (Exception)
307+
{
308+
Logger.Error().WriteLine(Resources.Localization.CLI_Executor_Sign_Error_DecryptFailed);
309+
await this.Console.WriteError(Resources.Localization.CLI_Executor_Sign_Error_DecryptFailed).ConfigureAwait(false);
310+
return StandardExitCodes.ErrorSettings;
311+
}
312+
}
313+
}
314+
return await this.SignPfx(config.Signing?.PfxPath?.Resolved, password, this.Verb.TimeStampUrl ?? config.Signing?.TimeStampServer, !this.Verb.NoPublisherUpdate).ConfigureAwait(false);
315+
316+
case CertificateSource.Personal:
317+
return await this.SignStore(config.Signing.Thumbprint, this.Verb.TimeStampUrl ?? config.Signing?.TimeStampServer, !this.Verb.NoPublisherUpdate);
318+
319+
case CertificateSource.DeviceGuard:
320+
if (config.Signing.DeviceGuard == null)
321+
{
322+
Logger.Error().WriteLine(Resources.Localization.CLI_Executor_Sign_Error_DeviceGuardNoConfig);
323+
await this.Console.WriteError(Resources.Localization.CLI_Executor_Sign_Error_DeviceGuardNoConfig).ConfigureAwait(false);
324+
return StandardExitCodes.ErrorSettings;
325+
}
326+
327+
return await this.SignDeviceGuard(config.Signing.DeviceGuard.FromConfiguration(), this.Verb.TimeStampUrl ?? config.Signing?.TimeStampServer, !this.Verb.NoPublisherUpdate);
328+
329+
default:
330+
Logger.Error().WriteLine(Resources.Localization.CLI_Executor_Sign_Error_NoCertAndDefaultConfig);
331+
await this.Console.WriteError(Resources.Localization.CLI_Executor_Sign_Error_NoCertAndDefaultConfig).ConfigureAwait(false);
332+
return StandardExitCodes.ErrorSettings;
333+
}
334+
}
332335

333336
private async Task<int> SignDeviceGuard(DeviceGuardConfig cfg, string timestamp, bool updatePublisherName)
334337
{

src/Otor.MsixHero.Cli/Resources/Localization.Designer.cs

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)