} // ctor #endregion #region -- Load/Save ------------------------------------------------------ public void Load() { if (!File.Exists(fileName)) { return; } var xDoc = XDocument.Load(fileName); state = xDoc.Root.GetAttribute("v", AcmeState.Installed); accountKey = xDoc.Root.GetNode("account", null); switch (state) { case AcmeState.Pending: orderUri = new Uri(xDoc.Root.GetNode("order", null), UriKind.Absolute); token = xDoc.Root.GetNode("token", null); keyAuthz = xDoc.Root.GetNode("authz", null); pfxContent = null; break; case AcmeState.Installed: case AcmeState.Issued: var pfxString = xDoc.Root.GetNode("pfx", null); pfxContent = pfxString == null ? null : Convert.FromBase64String(pfxString); orderUri = null; token = null; keyAuthz = null; break; } } // proc Load
} // func GetAccountStateAsync #endregion #region -- Order ---------------------------------------------------------- public async Task NewOrderAsync() { var acme = CreateAcme(); // place new order var newOrder = await acme.NewOrder(new string[] { commonName }); // get token var authz = (await newOrder.Authorizations()).First(); var http = await authz.Http(); // save request orderUri = newOrder.Location; token = http.Token; keyAuthz = http.KeyAuthz; pfxContent = null; // change state state = AcmeState.Pending; await SaveAsync(); } // func NewOrderAsync
} // func GetChallengeStateAsync #endregion #region -- Generate ------------------------------------------------------- public async Task <byte[]> GenerateKeyAsync(string pfxPassword = null) { if (orderUri == null) { return(null); } var acme = CreateAcme(); var order = acme.Order(orderUri); // check state var status = await GetChallengeStateAsync(order); if (status != ChallengeStatus.Valid) { return(null); } // generate key var newPrivateKey = KeyFactory.NewKey(KeyAlgorithm.ES256); var certififcateChain = await order.Generate(new CsrInfo() { CommonName = commonName }, newPrivateKey); var pfxBuilder = certififcateChain.ToPfx(newPrivateKey); // clear challenge orderUri = null; token = null; keyAuthz = null; // mark issued pfxContent = pfxBuilder.Build(commonName, pfxPassword ?? String.Empty); state = AcmeState.Issued; await SaveAsync(); return(pfxContent); } // func GenerateKeyAsync
public Task SetValidAsync() { // set state state = AcmeState.Installed; return(SaveAsync()); } // func SetValidAsync