Beispiel #1
0
        public async Task <CertificateRequestResult> GetCertificateAsync(IEnumerable <string> hostNames, string pfxPassword, IChallengeResponseProvider challengeManager, bool skipTest = false)
        {
            if (challengeManager == null)
            {
                throw new ArgumentNullException(nameof(challengeManager));
            }
            if (client == null)
            {
                throw new ObjectDisposedException(nameof(AutoAcmeContext));
            }
            if (context == null)
            {
                throw new InvalidOperationException("Not logged in");
            }

            // Test authorization
            if (!skipTest)
            {
                Log.WriteLine("Testing authorization:");
                Log.Indent();
                var probeResult = await challengeManager.TestAsync(hostNames).ConfigureAwait(true);

                Log.Unindent();
                if (!probeResult)
                {
                    throw new Exception("Test authorization failed");
                }
            }

            // Prepare order
            Log.WriteLine("Preparing order");
            Log.Indent();
            var orderContext = await context.NewOrder(hostNames.ToArray()).ConfigureAwait(true);

            var certKey = KeyFactory.NewKey(AcmeEnvironment.CfgStore.KeyAlgorithm);

            Log.Unindent();

            // Get authorization
            Log.WriteLine("Getting authorization:");
            Log.Indent();
            var authorizations = await orderContext.Authorizations().ConfigureAwait(true);

            var authorizationResult = await challengeManager.ValidateAsync(this, authorizations).ConfigureAwait(true);

            Log.Unindent();
            if (!authorizationResult)
            {
                throw new Exception($"Authorization failed with status {authorizationResult}");
            }

            // Get certificate
            Log.WriteLine("Processing certificate:");
            Log.Indent();
            Log.Write("Requesting certificate...");
            var certChain = await orderContext.Generate(new CsrInfo()
            {
                CommonName = hostNames.First()
            }, certKey).ConfigureAwait(true);

            Log.WriteLine("OK");

            // Export PFX
            Log.Write("Exporting PFX...");
            var pfxBuilder = certChain.ToPfx(certKey);

            pfxBuilder.FullChain = false;
            var pfxData = pfxBuilder.Build(hostNames.First(), pfxPassword);

            Log.WriteLine("OK");
            Log.Unindent();
            return(new CertificateRequestResult {
                Certificate = new X509Certificate2(certChain.Certificate.ToDer()),
                PrivateKey = new KeyInfo()
                {
                    PrivateKeyInfo = certKey.ToDer()
                },
                PfxData = pfxData
            });
        }
Beispiel #2
0
 public CertificateRequestResult GetCertificate(IEnumerable <string> hostNames, string pfxPassword, IChallengeResponseProvider challengeManager, bool skipTest = false)
 {
     return(GetCertificateAsync(hostNames, pfxPassword, challengeManager, skipTest).Result);
 }