예제 #1
0
        public async Task RenewCertificate(string certName, params string[] hostnames)
        {
            this.stopwatch.Restart();

            await EnsureClient();

            if (this.client == null)
            {
                throw new Exception("EnsureClient failed to ensure client had a value, check configuration/permissions");
            }

            if (hostnames.Count() == 0)
            {
                throw new ArgumentException(nameof(hostnames), "One or more hostnames must be provided for the certificate");
            }

            var order = await client.CreateOrderAsync(hostnames);

            logger.LogInformation("[{0}]@{1}ms Created order: {2} ", certName, stopwatch.ElapsedMilliseconds, order.OrderUrl);

            await PerformChallengesAsync(order);

            logger.LogInformation("[{0}]@{1}ms Challenges complete", certName, stopwatch.ElapsedMilliseconds);

            // At this point all authorizations should be valid
            var(certCsr, keys) = this.GenerateCertificateRequest(hostnames);
            order = await client.FinalizeOrderAsync(order.Payload.Finalize, certCsr);

            logger.LogInformation("[{0}]@{1}ms Order finalized", certName, stopwatch.ElapsedMilliseconds);

            if (order.Payload.Status != "valid")
            {
                throw new Exception("Order isn't valid: " + order.Payload.Error);
            }

            logger.LogInformation("[{0}]@{1}ms Fetching cert and generating PFX", certName, stopwatch.ElapsedMilliseconds);
            var certBytes = await client.GetOrderCertificateAsync(order);

            var privateBytes = keys.PrivateKey.Export(PkiEncodingFormat.Pem);
            var pfx          = CertExporter.ExportPfx(certBytes, privateBytes);

            await this.certStore.StorePfxCertificateAsync(certName, pfx);

            logger.LogInformation("[{0}]@{1}ms Saved certificate to '{2}'", certName, stopwatch.ElapsedMilliseconds, certStore.GetType().Name);

            stopwatch.Stop();
        }