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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
|
using ACMESharp.Protocol;
using ACMESharp.Protocol.Resources;
using Autofac;
using PKISharp.WACS.Clients.Acme;
using PKISharp.WACS.Configuration;
using PKISharp.WACS.DomainObjects;
using PKISharp.WACS.Extensions;
using PKISharp.WACS.Plugins.Base.Options;
using PKISharp.WACS.Plugins.Interfaces;
using PKISharp.WACS.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace PKISharp.WACS
{
/// <summary>
/// This part of the code handles the actual creation/renewal of ACME certificates
/// </summary>
internal class RenewalExecutor
{
private readonly MainArguments _args;
private readonly IAutofacBuilder _scopeBuilder;
private readonly ILifetimeScope _container;
private readonly ILogService _log;
private readonly IInputService _input;
private readonly ExceptionHandler _exceptionHandler;
public RenewalExecutor(
MainArguments args, IAutofacBuilder scopeBuilder,
ILogService log, IInputService input,
ExceptionHandler exceptionHandler, IContainer container)
{
_args = args;
_scopeBuilder = scopeBuilder;
_log = log;
_input = input;
_exceptionHandler = exceptionHandler;
_container = container;
}
public async Task<RenewResult?> Execute(Renewal renewal, RunLevel runLevel)
{
using var ts = _scopeBuilder.Target(_container, renewal, runLevel);
using var es = _scopeBuilder.Execution(ts, renewal, runLevel);
// Generate the target
var targetPlugin = es.Resolve<ITargetPlugin>();
var (disabled, disabledReason) = targetPlugin.Disabled;
if (disabled)
{
throw new Exception($"Target plugin is not available. {disabledReason}");
}
var target = await targetPlugin.Generate();
if (target is INull)
{
throw new Exception($"Target plugin did not generate a target");
}
if (!target.IsValid(_log))
{
throw new Exception($"Target plugin generated an invalid target");
}
// Check if our validation plugin is (still) up to the task
var validationPlugin = es.Resolve<IValidationPluginOptionsFactory>();
if (!validationPlugin.CanValidate(target))
{
throw new Exception($"Validation plugin is unable to validate the target. A wildcard host was introduced into a HTTP validated renewal.");
}
// Check if renewal is needed
if (!runLevel.HasFlag(RunLevel.ForceRenew) && !renewal.Updated)
{
_log.Verbose("Checking {renewal}", renewal.LastFriendlyName);
if (!renewal.IsDue())
{
var cs = es.Resolve<ICertificateService>();
var cache = cs.CachedInfo(renewal, target);
if (cache != null)
{
_log.Information("Renewal for {renewal} is due after {date}", renewal.LastFriendlyName, renewal.GetDueDate());
return null;
}
else if (!renewal.New)
{
_log.Information(LogType.All, "Renewal for {renewal} running prematurely due to detected target change", renewal.LastFriendlyName);
}
}
else if (!renewal.New)
{
_log.Information(LogType.All, "Renewing certificate for {renewal}", renewal.LastFriendlyName);
}
}
else if (runLevel.HasFlag(RunLevel.ForceRenew))
{
_log.Information(LogType.All, "Force renewing certificate for {renewal}", renewal.LastFriendlyName);
}
// Create the order
var client = es.Resolve<AcmeClient>();
var identifiers = target.GetHosts(false);
_log.Verbose("Creating certificate order for hosts: {identifiers}", identifiers);
var order = await client.CreateOrder(identifiers);
// Check if the order is valid
if ((order.Payload.Status != AcmeClient.OrderReady &&
order.Payload.Status != AcmeClient.OrderPending) ||
order.Payload.Error != null)
{
_log.Error("Failed to create order {url}: {detail}", order.OrderUrl, order.Payload.Error.Detail);
return OnRenewFail(new Challenge() { Error = "Unable to create order" });
}
else
{
_log.Verbose("Order {url} created", order.OrderUrl);
}
// Answer the challenges
foreach (var authUrl in order.Payload.Authorizations)
{
// Get authorization details
_log.Verbose("Handle authorization {n}/{m}",
order.Payload.Authorizations.ToList().IndexOf(authUrl) + 1,
order.Payload.Authorizations.Length + 1);
var authorization = await client.GetAuthorizationDetails(authUrl);
// Find a targetPart that matches the challenge
var targetPart = target.Parts.
FirstOrDefault(tp => tp.GetHosts(false).
Any(h => authorization.Identifier.Value == h.Replace("*.", "")));
if (targetPart == null)
{
return OnRenewFail(new Challenge()
{
Error = "Unable to match challenge to target"
});
}
// Run the validation plugin
var challenge = await Authorize(es, runLevel, renewal.ValidationPluginOptions, targetPart, authorization);
if (challenge.Status != AcmeClient.AuthorizationValid)
{
return OnRenewFail(challenge);
}
}
return await OnValidationSuccess(es, renewal, target, order, runLevel);
}
/// <summary>
/// Steps to take on authorization failed
/// </summary>
/// <param name="auth"></param>
/// <returns></returns>
private RenewResult OnRenewFail(Challenge challenge)
{
var errors = challenge?.Error;
if (errors != null)
{
return new RenewResult($"Authorization failed: {errors.ToString()}");
}
else
{
return new RenewResult($"Authorization failed");
}
}
/// <summary>
/// Steps to take on succesful (re)authorization
/// </summary>
/// <param name="target"></param>
private async Task<RenewResult> OnValidationSuccess(ILifetimeScope renewalScope, Renewal renewal, Target target, OrderDetails order, RunLevel runLevel)
{
RenewResult? result = null;
try
{
var certificateService = renewalScope.Resolve<ICertificateService>();
var csrPlugin = target.CsrBytes == null ? renewalScope.Resolve<ICsrPlugin>() : null;
if (csrPlugin != null)
{
var (disabled, disabledReason) = csrPlugin.Disabled;
if (disabled)
{
return new RenewResult($"CSR plugin is not available. {disabledReason}");
}
}
var oldCertificate = certificateService.CachedInfo(renewal);
var newCertificate = await certificateService.RequestCertificate(csrPlugin, runLevel, renewal, target, order);
// Test if a new certificate has been generated
if (newCertificate == null)
{
return new RenewResult("No certificate generated");
}
else
{
result = new RenewResult(newCertificate);
}
// Early escape for testing validation only
if (renewal.New &&
runLevel.HasFlag(RunLevel.Test) &&
!await _input.PromptYesNo($"[--test] Do you want to install the certificate?", true))
{
return new RenewResult("User aborted");
}
// Run store plugin(s)
var storePluginOptions = new List<StorePluginOptions>();
var storePlugins = new List<IStorePlugin>();
try
{
var steps = renewal.StorePluginOptions.Count();
for (var i = 0; i < steps; i++)
{
var storeOptions = renewal.StorePluginOptions[i];
var storePlugin = (IStorePlugin)renewalScope.Resolve(storeOptions.Instance);
if (!(storePlugin is INull))
{
if (steps > 1)
{
_log.Information("Store step {n}/{m}: {name}...", i + 1, steps, storeOptions.Name);
}
else
{
_log.Information("Store with {name}...", storeOptions.Name);
}
var (disabled, disabledReason) = storePlugin.Disabled;
if (disabled)
{
return new RenewResult($"Store plugin is not available. {disabledReason}");
}
await storePlugin.Save(newCertificate);
storePlugins.Add(storePlugin);
storePluginOptions.Add(storeOptions);
}
}
}
catch (Exception ex)
{
var reason = _exceptionHandler.HandleException(ex, "Unable to store certificate");
result.ErrorMessage = $"Store failed: {reason}";
result.Success = false;
return result;
}
// Run installation plugin(s)
try
{
var steps = renewal.InstallationPluginOptions.Count();
for (var i = 0; i < steps; i++)
{
var installOptions = renewal.InstallationPluginOptions[i];
var installPlugin = (IInstallationPlugin)renewalScope.Resolve(
installOptions.Instance,
new TypedParameter(installOptions.GetType(), installOptions));
if (!(installPlugin is INull))
{
if (steps > 1)
{
_log.Information("Installation step {n}/{m}: {name}...", i + 1, steps, installOptions.Name);
}
else
{
_log.Information("Installing with {name}...", installOptions.Name);
}
var (disabled, disabledReason) = installPlugin.Disabled;
if (disabled)
{
return new RenewResult($"Installation plugin is not available. {disabledReason}");
}
await installPlugin.Install(storePlugins, newCertificate, oldCertificate);
}
}
}
catch (Exception ex)
{
var reason = _exceptionHandler.HandleException(ex, "Unable to install certificate");
result.Success = false;
result.ErrorMessage = $"Install failed: {reason}";
}
// Delete the old certificate if not forbidden, found and not re-used
for (var i = 0; i < storePluginOptions.Count; i++)
{
if (!storePluginOptions[i].KeepExisting &&
oldCertificate != null &&
newCertificate.Certificate.Thumbprint != oldCertificate.Certificate.Thumbprint)
{
try
{
await storePlugins[i].Delete(oldCertificate);
}
catch (Exception ex)
{
_log.Error(ex, "Unable to delete previous certificate");
//result.Success = false; // not a show-stopper, consider the renewal a success
result.ErrorMessage = $"Delete failed: {ex.Message}";
}
}
}
if ((renewal.New || renewal.Updated) && !_args.NoTaskScheduler)
{
if (runLevel.HasFlag(RunLevel.Test) &&
!await _input.PromptYesNo($"[--test] Do you want to automatically renew this certificate?", true))
{
// Early out for test runs
return new RenewResult("User aborted");
}
else
{
// Make sure the Task Scheduler is configured
await renewalScope.Resolve<TaskSchedulerService>().EnsureTaskScheduler(runLevel, false);
}
}
return result;
}
catch (Exception ex)
{
_exceptionHandler.HandleException(ex);
while (ex.InnerException != null)
{
ex = ex.InnerException;
}
// Result might still contain the Thumbprint of the certificate
// that was requested and (partially? installed, which might help
// with debugging
if (result == null)
{
result = new RenewResult(ex.Message);
}
else
{
result.Success = false;
result.ErrorMessage = ex.Message;
}
}
return result;
}
/// <summary>
/// Make sure we have authorization for every host in target
/// </summary>
/// <param name="target"></param>
/// <returns></returns>
private async Task<Challenge> Authorize(
ILifetimeScope execute, RunLevel runLevel,
ValidationPluginOptions options, TargetPart targetPart,
Authorization authorization)
{
var invalid = new Challenge { Status = AcmeClient.AuthorizationInvalid };
var valid = new Challenge { Status = AcmeClient.AuthorizationValid };
var client = execute.Resolve<AcmeClient>();
var identifier = authorization.Identifier.Value;
IValidationPlugin? validationPlugin = null;
try
{
if (authorization.Status == AcmeClient.AuthorizationValid)
{
if (!runLevel.HasFlag(RunLevel.Test) &&
!runLevel.HasFlag(RunLevel.IgnoreCache))
{
_log.Information("Cached authorization result: {Status}", authorization.Status);
return valid;
}
if (runLevel.HasFlag(RunLevel.IgnoreCache))
{
// Due to the IgnoreCache flag (--force switch)
// we are going to attempt to re-authorize the
// domain even though its already autorized.
// On failure, we can still use the cached result.
// This helps for migration scenarios.
invalid = valid;
}
}
_log.Information("Authorize identifier: {identifier}", identifier);
_log.Verbose("Challenge types available: {challenges}", authorization.Challenges.Select(x => x.Type ?? "[Unknown]"));
var challenge = authorization.Challenges.FirstOrDefault(c => string.Equals(c.Type, options.ChallengeType, StringComparison.CurrentCultureIgnoreCase));
if (challenge == null)
{
if (authorization.Status == AcmeClient.AuthorizationValid)
{
var usedType = authorization.Challenges.
Where(x => x.Status == AcmeClient.AuthorizationValid).
FirstOrDefault();
_log.Warning("Expected challenge type {type} not available for {identifier}, already validated using {valided}.",
options.ChallengeType,
authorization.Identifier.Value,
usedType?.Type ?? "[unknown]");
return valid;
}
else
{
_log.Error("Expected challenge type {type} not available for {identifier}.",
options.ChallengeType,
authorization.Identifier.Value);
invalid.Error = "Expected challenge type not available";
return invalid;
}
}
// We actually have to do validation now
using var validation = _scopeBuilder.Validation(execute, options, targetPart, identifier);
try
{
validationPlugin = validation.Resolve<IValidationPlugin>();
}
catch (Exception ex)
{
_log.Error(ex, "Error resolving validation plugin");
}
if (validationPlugin == null)
{
_log.Error("Validation plugin not found or not created.");
invalid.Error = "Validation plugin not found or not created.";
return invalid;
}
var (disabled, disabledReason) = validationPlugin.Disabled;
if (disabled)
{
_log.Error($"Validation plugin is not available. {disabledReason}");
invalid.Error = "Validation plugin is not available.";
return invalid;
}
_log.Information("Authorizing {dnsIdentifier} using {challengeType} validation ({name})",
identifier,
options.ChallengeType,
options.Name);
try
{
var details = await client.DecodeChallengeValidation(authorization, challenge);
await validationPlugin.PrepareChallenge(details);
}
catch (Exception ex)
{
_log.Error(ex, "Error preparing for challenge answer");
invalid.Error = "Error preparing for challenge answer";
return invalid;
}
_log.Debug("Submitting challenge answer");
challenge = await client.AnswerChallenge(challenge);
if (challenge.Status != AcmeClient.AuthorizationValid)
{
if (challenge.Error != null)
{
_log.Error(challenge.Error.ToString());
}
_log.Error("Authorization result: {Status}", challenge.Status);
invalid.Error = challenge.Error;
return invalid;
}
else
{
_log.Information("Authorization result: {Status}", challenge.Status);
return valid;
}
}
catch (Exception ex)
{
_log.Error("Error authorizing {renewal}", targetPart);
_exceptionHandler.HandleException(ex);
invalid.Error = ex.Message;
return invalid;
}
finally
{
if (validationPlugin != null)
{
try
{
_log.Verbose("Starting post-validation cleanup");
await validationPlugin.CleanUp();
_log.Verbose("Post-validation cleanup was succesful");
}
catch (Exception ex)
{
_log.Warning("An error occured during post-validation cleanup: {ex}", ex.Message);
}
}
}
}
}
}
|