コード例 #1
0
        public async Task <AcmeProtocolClient> CreateClientAsync()
        {
            var account    = LoadState <AccountDetails>("account.json");
            var accountKey = LoadState <AccountKey>("account_key.json");
            var directory  = LoadState <ServiceDirectory>("directory.json");

            var acmeProtocolClient = new AcmeProtocolClient(_baseUri, directory, account, accountKey?.GenerateSigner());

            if (directory == null)
            {
                directory = await acmeProtocolClient.GetDirectoryAsync();

                acmeProtocolClient.Directory = directory;
            }

            await acmeProtocolClient.GetNonceAsync();

            if (acmeProtocolClient.Account == null)
            {
                account = await acmeProtocolClient.CreateAccountAsync(new[] { "mailto:" + _options.Contacts }, true);

                accountKey = new AccountKey
                {
                    KeyType   = acmeProtocolClient.Signer.JwsAlg,
                    KeyExport = acmeProtocolClient.Signer.Export()
                };

                SaveState(account, "account.json");
                SaveState(accountKey, "account_key.json");
                SaveState(directory, "directory.json");

                acmeProtocolClient.Account = account;
            }

            return(acmeProtocolClient);
        }
コード例 #2
0
        private async Task <bool> ConfigureAcmeClient()
        {
            var httpClientHandler = new HttpClientHandler()
            {
                Proxy = _proxyService.GetWebProxy(),
            };
            var httpClient = new HttpClient(httpClientHandler)
            {
                BaseAddress = new Uri(_arguments.MainArguments.GetBaseUri())
            };
            IJwsTool signer        = null;
            var      accountSigner = AccountSigner;

            if (accountSigner != null)
            {
                signer = accountSigner.JwsTool();
            }
            _client = new AcmeProtocolClient(httpClient, signer: signer)
            {
                BeforeHttpSend = (x, r) =>
                {
                    _log.Debug("Send {method} request to {uri}", r.Method, r.RequestUri);
                },
            };
            _client.Directory = await _client.GetDirectoryAsync();

            await _client.GetNonceAsync();

            _client.Account = await LoadAccount(signer);

            if (_client.Account == null)
            {
                throw new Exception("AcmeClient was unable to find or create an account");
            }
            return(true);
        }
コード例 #3
0
        public async Task FinalizeOrder()
        {
            using (var http = _server.CreateClient())
            {
                var dir = await GetDir();

                var signer = new Crypto.JOSE.Impl.RSJwsTool();
                signer.Init();
                using (var acme = new AcmeProtocolClient(http, dir,
                                                         signer: signer))
                {
                    await acme.GetNonceAsync();

                    var acct = await acme.CreateAccountAsync(new[] { "mailto:[email protected]" });

                    acme.Account = acct;

                    var dnsIds = new[] {
                        "foo.mock.acme2.zyborg.io",
                        "foo-alt-1.mock.acme2.zyborg.io",
                        "foo-alt-2.mock.acme2.zyborg.io",
                        "foo-alt-3.mock.acme2.zyborg.io",
                    };
                    var order = await acme.CreateOrderAsync(dnsIds);

                    Assert.IsNotNull(order?.OrderUrl);
                    Assert.AreEqual(dnsIds.Length, order.Payload.Authorizations?.Length);
                    Assert.AreEqual(dnsIds.Length, order.Payload.Identifiers?.Length);

                    var authzUrl = order.Payload.Authorizations[0];
                    var authz    = await acme.GetAuthorizationDetailsAsync(authzUrl);

                    Assert.IsNotNull(authz);
                    Assert.IsFalse(authz.Wildcard ?? false);
                    Assert.AreEqual(dnsIds[0], authz.Identifier.Value);

                    foreach (var chlng in authz.Challenges)
                    {
                        var chlng2 = await acme.AnswerChallengeAsync(chlng.Url);

                        Assert.IsNotNull(chlng2);
                        Assert.AreEqual("valid", chlng2.Status);
                    }

                    var kpr = PkiKeyPair.GenerateRsaKeyPair(2048);
                    var csr = new PkiCertificateSigningRequest($"cn={dnsIds[0]}", kpr,
                                                               PkiHashAlgorithm.Sha256);
                    csr.CertificateExtensions.Add(
                        PkiCertificateExtension.CreateDnsSubjectAlternativeNames(dnsIds.Skip(1)));
                    var csrDer = csr.ExportSigningRequest(PkiEncodingFormat.Der);

                    var finalizedOrder = await acme.FinalizeOrderAsync(order.Payload.Finalize, csrDer);

                    Assert.AreEqual("valid", finalizedOrder.Payload.Status);
                    Assert.IsNotNull(finalizedOrder.Payload.Certificate);

                    var getResp = await acme.GetAsync(finalizedOrder.Payload.Certificate);

                    getResp.EnsureSuccessStatusCode();

                    using (var fs = new FileStream(
                               @"C:\local\prj\bek\ACMESharp\ACMESharpCore\test\ACMESharp.MockServer.UnitTests\finalize-cert.pem",
                               FileMode.Create))
                    {
                        await getResp.Content.CopyToAsync(fs);
                    }
                }
            }
        }
コード例 #4
0
        protected async Task DoTheWorkAsync(object state)
        {
            _logger.LogInformation("** DOING WORKING *****************************************");
            _logger.LogInformation($"DNS Names:  {string.Join(",", _options.DnsNames)}");

            if (_state.Certificate != null)
            {
                var now = DateTime.Now;
                if (_state.Certificate.NotBefore > now && _state.Certificate.NotAfter < now)
                {
                    _logger.LogInformation("Existing certificate is Good!");
                    return;
                }
                {
                    _logger.LogWarning("Existing Certificate is Expired!");
                }
            }
            else
            {
                _logger.LogWarning("Missing Certificate");
            }

            var acmeUrl = new Uri(_options.CaUrl);

            using (var acme = new AcmeProtocolClient(acmeUrl))
            {
                _state.ServiceDirectory = await acme.GetDirectoryAsync();

                Save(_state.ServiceDirectoryFile, _state.ServiceDirectory);
                acme.Directory = _state.ServiceDirectory;

                Save(_state.TermsOfServiceFile,
                     await acme.GetTermsOfServiceAsync());

                await acme.GetNonceAsync();

                if (!await ResolveAccount(acme))
                {
                    return;
                }

                if (!await ResolveOrder(acme))
                {
                    return;
                }

                if (!await ResolveChallenges(acme))
                {
                    return;
                }

                if (!await ResolveAuthorizations(acme))
                {
                    return;
                }

                if (!await ResolveCertificate(acme))
                {
                    return;
                }
            }
        }
コード例 #5
0
ファイル: Program.cs プロジェクト: divinci/ACMESharpCore
        async Task CreateAccount()
        {
            var acmeUrl = new Uri(Program.LetsEncryptV2StagingEndpoint);

            _http             = new HttpClient();
            _http.BaseAddress = acmeUrl;

            var sample = Encoding.UTF8.GetBytes("abcdefg");

            // var ecKeys = PkiKeyPair.GenerateEcdsaKeyPair(256);
            // var ecPrvKey = ecKeys.PrivateKey.Export(PkiEncodingFormat.Pem);
            // var ecPubKey = ecKeys.PublicKey.Export(PkiEncodingFormat.Pem);

            // WriteLine(
            //     "getting from: " + acmeUrl
            //     + "\r\necPrv: " + Convert.ToBase64String(ecPrvKey)
            //     + "\r\necPub: " + Convert.ToBase64String(ecPubKey)
            // );

            // var signer = new ACMESharp.Crypto.JOSE.Impl.ESJwsTool();
            // signer.Init();
            // WriteLine($"ECKeys: {signer.Export()}");

            var signer = new PkiJwsTool(256);

            signer.Init();
            var signerExport = signer.Export();

            signer = new PkiJwsTool(256);
            signer.Import(signerExport);
            WriteLine($"ECKeys: {signerExport}");

            var sig1 = signer.Sign(sample);

            WriteLine($"Sig: {Convert.ToBase64String(sig1)}");
            sig1 = signer.Sign(sample);
            WriteLine($"Sig: {Convert.ToBase64String(sig1)}");
            sig1 = signer.Sign(sample);
            WriteLine($"Sig: {Convert.ToBase64String(sig1)}");
            WriteLine($"Vfy: {signer.Verify(sample, sig1)}");
            WriteLine($"JWK: {JsonConvert.SerializeObject(signer.ExportJwk())}");
            WriteLine("Sig1Hex: " + BitConverter.ToString(sig1));

            // var ecAlt = JsonConvert.SerializeObject(signer.KeyPair.ExportEcParameters());
            // WriteLine($"ECAlt:  {ecAlt}");
            // var signer2 = new ACMESharp.Crypto.JOSE.Impl.ESJwsTool();
            // signer2.HashSize = 256;
            // signer2.Init();
            // signer2.Import(ecAlt);

            // WriteLine($"ECKeys2: {signer2.Export()}");
            // var sig2 = signer2.Sign(sample);
            // WriteLine($"Sig2: {Convert.ToBase64String(sig2)}");
            // sig2 = signer2.Sign(sample);
            // WriteLine($"Sig2: {Convert.ToBase64String(sig2)}");
            // sig2 = signer2.Sign(sample);
            // WriteLine($"Sig2: {Convert.ToBase64String(sig2)}");
            // WriteLine($"Vfy2: {signer2.Verify(sample, sig2)}");
            // WriteLine($"JWK2: {JsonConvert.SerializeObject(signer2.ExportJwk())}");
            // WriteLine("Sig2Hex: " + BitConverter.ToString(sig2));

            var lineSeps = "\r\n".ToCharArray();

            using (var acme = new AcmeProtocolClient(_http, signer: signer))
            {
                acme.BeforeAcmeSign = (s, o) => WriteLine($"BEFORE_SIGN({s}, {JsonConvert.SerializeObject(o)})");
                acme.BeforeHttpSend = (s, m) => WriteLine($"BEFORE_SEND({s}, {m.Content?.ReadAsStringAsync().Result}");


                var dir = await acme.GetDirectoryAsync();

                acme.Directory = dir;

                await acme.GetNonceAsync();

                var c    = _contacts.Split(lineSeps, StringSplitOptions.RemoveEmptyEntries).Select(x => x.Trim());
                var acct = await acme.CreateAccountAsync(c, _agreeTos);

                var acctStr = JsonConvert.SerializeObject(acct, Formatting.Indented);

                Console.WriteLine("Got Account: " + acctStr);
            }

            signer = new PkiJwsTool(256);
            signer.Import(signerExport);

            using (var acme = new AcmeProtocolClient(_http, signer: signer))
            {
                acme.BeforeAcmeSign = (s, o) => WriteLine($"BEFORE_SIGN({s}, {JsonConvert.SerializeObject(o)})");
                acme.BeforeHttpSend = (s, m) => WriteLine($"BEFORE_SEND({s}, {m.Content?.ReadAsStringAsync().Result}");

                var dir = await acme.GetDirectoryAsync();

                acme.Directory = dir;

                await acme.GetNonceAsync();

                var d     = _dnsNames.Split(lineSeps, StringSplitOptions.RemoveEmptyEntries).Select(x => x.Trim());
                var order = await acme.CreateOrderAsync(d);

                var orderStr = JsonConvert.SerializeObject(order, Formatting.Indented);

                Console.WriteLine("Got Order: " + orderStr);
            }
        }
コード例 #6
0
 private async Task LoadSettings()
 {
     // load settings related to this provider
     await _client.GetNonceAsync();
 }
コード例 #7
0
ファイル: AcmeClient.cs プロジェクト: thdtjsdn/win-acme
        private async Task <bool> ConfigureAcmeClient()
        {
            var httpClientHandler = new HttpClientHandler()
            {
                Proxy = _proxyService.GetWebProxy(),
            };
            var httpClient = new HttpClient(httpClientHandler)
            {
                BaseAddress = new Uri(_arguments.MainArguments.GetBaseUri())
            };

            _log.Verbose("Loading ACME account signer...");
            IJwsTool signer        = null;
            var      accountSigner = AccountSigner;

            if (accountSigner != null)
            {
                signer = accountSigner.JwsTool();
            }

            _log.Verbose("Constructing ACME protocol client...");
            try
            {
                _client = new AcmeProtocolClient(httpClient, signer: signer);
            }
            catch (CryptographicException)
            {
                if (signer == null)
                {
                    // There has been a problem generate a signer for the
                    // new account, possibly because some EC curve is not
                    // on available on the system? So we give it another
                    // shot with a less fancy RSA signer
                    _log.Verbose("First chance error generating new signer, retrying with RSA instead of ECC");
                    signer = new RSJwsTool {
                        KeySize = new Rsa(_log, new RsaOptions()).GetRsaKeyBits()
                    };
                    signer.Init();
                    _client = new AcmeProtocolClient(httpClient, signer: signer);
                }
                else
                {
                    throw;
                }
            }
            _client.BeforeHttpSend = (x, r) =>
            {
                _log.Debug("Send {method} request to {uri}", r.Method, r.RequestUri);
            };
            _client.AfterHttpSend = (x, r) =>
            {
                _log.Verbose("Request completed with status {s}", r.StatusCode);
            };
            _client.Directory = await _client.GetDirectoryAsync();

            await _client.GetNonceAsync();

            _client.Account = await LoadAccount(signer);

            if (_client.Account == null)
            {
                throw new Exception("AcmeClient was unable to find or create an account");
            }
            return(true);
        }
コード例 #8
0
ファイル: AcmeHostedService.cs プロジェクト: yahyaguzide/daud
        protected async Task DoTheWorkAsync(object state)
        {
            var cts = new CancellationTokenSource();

            cts.CancelAfter(15000);
            try
            {
                var suggestion = await _registryClient.Registry.SuggestAsync(_gameConfiguration.PublicURL, cts.Token);

                if (suggestion != "localhost")
                {
                    _options.DnsNames = new[] { suggestion }
                }
                ;
                else
                {
                    if (!WarnedLocalMode)
                    {
                        _logger.LogWarning("registry reports we are not accessible on http TCP/80 on our public IP. If you're in development mode, this is fine.");
                    }

                    WarnedLocalMode = true;
                    return;
                }
            }
            catch (Exception e)
            {
                _logger.LogError("** failed to get suggestion from registry: " + e);
            }

            if (_options.DnsNames == null)
            {
                return;
            }

            _logger.LogInformation("** Checking LetsEncrypt status *****************************************");
            _logger.LogInformation($"DNS Names:  {string.Join(",", _options.DnsNames)}");

            if (_state.Certificate != null)
            {
                var now = DateTime.Now;
                if (_state.Certificate.NotAfter > now)
                {
                    _logger.LogInformation("Existing certificate is Good!");
                    return;
                }
                else
                {
                    _logger.LogWarning($"Existing Certificate is Expired! {_state.Certificate.NotAfter} > {now}");
                }
            }
            else
            {
                _logger.LogWarning("Missing Certificate");
            }

            try
            {
                Directory.Delete(_state.RootDir, true);
            }
            catch (Exception) { }
            Reinitialize();


            try
            {
                var acmeUrl = new Uri(_options.CaUrl);
                using (var acme = new AcmeProtocolClient(acmeUrl))
                {
                    _state.ServiceDirectory = await acme.GetDirectoryAsync();

                    Save(_state.ServiceDirectoryFile, _state.ServiceDirectory);
                    acme.Directory = _state.ServiceDirectory;

                    Save(_state.TermsOfServiceFile,
                         await acme.GetTermsOfServiceAsync());

                    await acme.GetNonceAsync();

                    if (!await ResolveAccount(acme))
                    {
                        return;
                    }

                    if (!await ResolveOrder(acme))
                    {
                        return;
                    }

                    if (!await ResolveChallenges(acme))
                    {
                        return;
                    }

                    if (!await ResolveAuthorizations(acme))
                    {
                        return;
                    }

                    if (!await ResolveCertificate(acme))
                    {
                        return;
                    }
                }
            }
            catch (Exception e)
            {
                _logger.LogWarning($"Exception while getting SSL Certificate: {e}");
            }
        }
コード例 #9
0
        private async void createOrderButton_Click(object sender, EventArgs e)
        {
            var url = ResolveCaServerEndpoint();

            if (url == null)
            {
                return;
            }

            var ids = ResolveDnsIdentifiers();

            if (ids == null)
            {
                return;
            }

            var dateRange = ResolveOrderDateRange();

            if (dateRange == null)
            {
                return;
            }

            await InvokeWithWaitCursor(async() =>
            {
                var signer = new PkiJwsTool(256);
                signer.Import(_account.JwsSigner);

                using (var acme = new AcmeProtocolClient(url, signer: signer,
                                                         acct: _account.Details))
                {
                    var dir        = await acme.GetDirectoryAsync();
                    acme.Directory = dir;

                    await acme.GetNonceAsync();

                    var details = await acme.CreateOrderAsync(ids,
                                                              dateRange.Value.notBefore, dateRange.Value.notAfter);

                    var order = new DbOrder
                    {
                        FirstOrderUrl = details.OrderUrl,
                        Details       = details,
                    };
                    Repo.Saveorder(order);
                    _lastOrder = order;

                    RebindOrderControls();
                    SetStatus("Order created and saved");

                    await RefreshOrderAuthorizations(acme);
                }

                Repo.Saveorder(_lastOrder);
                RebindOrderControls();
                SetStatus("Order created and Authorization resolved and saved");

                await DecodeOrderAuthorizationChallenges(signer);

                Repo.Saveorder(_lastOrder);
                RebindOrderControls();
                SetStatus("Order created, Authorizations resolved and Challenges decoded and saved");
            });
        }
コード例 #10
0
ファイル: DomainController.cs プロジェクト: franklbt/mfork
        public async Task <IActionResult> Post([FromBody] DomainRequest request)
        {
            if (!IsValidDomain(request.Domain))
            {
                return(BadRequest(new DomainResponse {
                    Error = "Invalid domain"
                }));
            }

            var domain    = string.Join(".", request.Domain.Split(".").TakeLast(2));
            var subDomain = string.Join(".", request.Domain.Split(".").SkipLast(2));

            var credentials = new AzureCredentialsFactory()
                              .FromServicePrincipal(
                _configuration["Azure:ClientId"],
                _configuration["Azure:ClientSecret"],
                _configuration["Azure:TenantId"],
                AzureEnvironment.AzureGlobalCloud
                );

            var azure = Azure
                        .Configure()
                        .WithRetryPolicy(new RetryPolicy(new TransientErrorIgnoreStrategy(), 0))
                        .Authenticate(credentials)
                        .WithSubscription(_configuration["Azure:SubscriptionId"]);

            var webApp = await azure.AppServices.WebApps.GetByIdAsync(
                _configuration["Azure:AppId"]);

            try
            {
                webApp.Update()
                .DefineHostnameBinding()
                .WithThirdPartyDomain(domain)
                .WithSubDomain(subDomain)
                .WithDnsRecordType(CustomHostNameDnsRecordType.CName)
                .Attach()
                .Apply();
            }
            catch (Exception e)
            {
                return(BadRequest(new DomainResponse {
                    Error = "Unable to validate domain ownership"
                }));
            }

            _ = Task.Run(async() =>
            {
                using var airtableBase = new AirtableBase(_configuration["Airtable:Key"], _configuration["Airtable:Base"]);
                try
                {
                    HttpClient httpClient = new HttpClient {
                        BaseAddress = new Uri(_configuration["Acme:Endpoint"])
                    };
                    AcmeProtocolClient acme = new AcmeProtocolClient(httpClient, usePostAsGet: true);

                    var acmeDir    = await acme.GetDirectoryAsync();
                    acme.Directory = acmeDir;

                    await acme.GetNonceAsync();

                    var account  = await acme.CreateAccountAsync(new[] { "mailto:" + _configuration["Acme:Email"] }, true);
                    acme.Account = account;

                    var order = await acme.CreateOrderAsync(new[] { request.Domain });
                    if (order.Payload.Status == "invalid")
                    {
                        return;
                    }

                    var authorizationUrl = order.Payload.Authorizations.FirstOrDefault();
                    if (string.IsNullOrEmpty(authorizationUrl))
                    {
                        return;
                    }
                    var authorization = await acme.GetAuthorizationDetailsAsync(authorizationUrl);

                    foreach (var challenge in authorization.Challenges.Where(x => x.Type == "http-01").ToList())
                    {
                        var challengeValidationDetails = (Http01ChallengeValidationDetails)
                                                         AuthorizationDecoder.DecodeChallengeValidation(authorization, challenge.Type, acme.Signer);

                        var path        = challengeValidationDetails.HttpResourcePath;
                        var token       = path.Split("/", StringSplitOptions.RemoveEmptyEntries).Last();
                        var value       = challengeValidationDetails.HttpResourceValue;
                        var contentType = challengeValidationDetails.HttpResourceContentType;

                        await airtableBase.CreateRecord("Acme", new Fields
                        {
                            FieldsCollection = new Dictionary <string, object>
                            {
                                ["Token"]       = token,
                                ["Value"]       = value,
                                ["ContentType"] = contentType
                            }
                        });

                        await Task.Delay(10 * 1000);
                        var challengeUpdated = await acme.AnswerChallengeAsync(challenge.Url);
                    }

                    //Wait for challenge to be resolved
                    var waitUntil = DateTime.Now.AddSeconds(300);
                    Authorization authorizationUpdated;
                    do
                    {
                        await Task.Delay(10 * 1000);
                        authorizationUpdated = await acme.GetAuthorizationDetailsAsync(authorizationUrl);
                    } while (authorizationUpdated.Status != "valid" && DateTime.Now < waitUntil);

                    if (authorizationUpdated.Status != "valid")
                    {
                        return;
                    }

                    //Generate certificate private key and CSR (Certificate signing request)
                    var keyPair = PkiKeyPair.GenerateEcdsaKeyPair(256);
                    var csr     = new PkiCertificateSigningRequest($"CN={request.Domain}", keyPair, PkiHashAlgorithm.Sha256);
                    var certCsr = csr.ExportSigningRequest(PkiEncodingFormat.Der);

                    order = await acme.FinalizeOrderAsync(order.Payload.Finalize, certCsr);
                    if (order.Payload.Status != "valid")
                    {
                        return;
                    }

                    if (string.IsNullOrEmpty(order.Payload.Certificate))
                    {
                        //Wait for certificate
                        var waitUntil2 = DateTime.Now.AddSeconds(300);
                        while (DateTime.Now < waitUntil2)
                        {
                            await Task.Delay(10 * 1000);
                            order = await acme.GetOrderDetailsAsync(order.OrderUrl, existing: order);

                            if (!string.IsNullOrEmpty(order.Payload.Certificate))
                            {
                                break;
                            }
                        }
                    }
                    if (string.IsNullOrEmpty(order.Payload.Certificate))
                    {
                        return;
                    }

                    var certResp = await acme.GetAsync(order.Payload.Certificate);
                    if (!certResp.IsSuccessStatusCode)
                    {
                        return;
                    }

                    var certByteArray = await certResp.Content.ReadAsByteArrayAsync();

                    //Export PFX file
                    var pfxPassword = _configuration["Acme:PfxPassword"];
                    var privateKey  = keyPair.PrivateKey;

                    using var cert = new X509Certificate2(certByteArray);

                    X509Chain chain = new X509Chain();
                    chain.Build(cert);
                    List <PkiCertificate> chainList = new List <PkiCertificate>();
                    foreach (var e in chain.ChainElements)
                    {
                        chainList.Add(PkiCertificate.From(e.Certificate));
                    }

                    var pfx = chainList[0].Export(PkiArchiveFormat.Pkcs12,
                                                  chain: chainList.Skip(1),
                                                  privateKey: privateKey,
                                                  password: pfxPassword?.ToCharArray());

                    webApp.Update()
                    .DefineSslBinding()
                    .ForHostname(request.Domain)
                    .WithPfxByteArrayToUpload(pfx, pfxPassword)
                    .WithSniBasedSsl()
                    .Attach()
                    .Apply();
                }
                catch (Exception e)
                {
                    await airtableBase.CreateRecord("Logs", new Fields
                    {
                        FieldsCollection = new Dictionary <string, object>
                        {
                            ["Hostname"] = request.Domain,
                            ["Event"]    = "exception-thrown",
                            ["Data"]     = JsonConvert.SerializeObject(e)
                        }
                    });
                }
            });



            return(Ok(new DomainResponse {
                IsSuccessful = true
            }));
        }