-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathAcmeOrder.cs
More file actions
251 lines (214 loc) · 7.04 KB
/
AcmeOrder.cs
File metadata and controls
251 lines (214 loc) · 7.04 KB
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
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Security.Cryptography.X509Certificates;
using System.Threading.Tasks;
using Waher.Content.Xml;
using Waher.Security.PKCS;
namespace Waher.Security.ACME
{
/// <summary>
/// ACME Order status enumeration
/// </summary>
public enum AcmeOrderStatus
{
/// <summary>
/// The server does not believe that the client has fulfilled the requirements.
/// Check the "authorizations" array for entries that are still pending.
/// </summary>
pending,
/// <summary>
/// The server agrees that the requirements have been fulfilled, and is awaiting finalization.
/// Submit a finalization request.
/// </summary>
ready,
/// <summary>
/// The certificate is being issued. Send a GET request after the time given in the
/// "Retry-After" header field of the response, if any.
/// </summary>
processing,
/// <summary>
/// The server has issued the certificate and provisioned its URL to the "certificate" field
/// of the order. Download the certificate.
/// </summary>
valid,
/// <summary>
/// The certificate will not be issued. Consider this order process abandoned.
/// </summary>
invalid
};
/// <summary>
/// Represents an ACME order.
/// </summary>
public class AcmeOrder : AcmeResource
{
private readonly AcmeOrderStatus status;
private readonly DateTime? expires = null;
private readonly DateTime? notBefore = null;
private readonly DateTime? notAfter = null;
private readonly AcmeIdentifier[] identifiers = null;
private AcmeAuthorization[] authorizations = null;
private readonly Uri[] authorizationUris = null;
private readonly AcmeException error = null; // TODO: Problem document
private readonly Uri finalize = null;
private readonly Uri certificate = null;
internal AcmeOrder(AcmeClient Client, Uri AccountLocation, Uri Location,
IEnumerable<KeyValuePair<string, object>> Obj, HttpResponseMessage Response)
: base(Client, AccountLocation, Location)
{
foreach (KeyValuePair<string, object> P in Obj)
{
switch (P.Key)
{
case "status":
if (!Enum.TryParse(P.Value as string, out this.status))
throw new ArgumentException("Invalid ACME order status: " + P.Value.ToString(), "status");
break;
case "expires":
if (XML.TryParse(P.Value as string, out DateTime TP))
this.expires = TP;
else
throw new ArgumentException("Invalid date and time value.", "expires");
break;
case "notBefore":
if (XML.TryParse(P.Value as string, out TP))
this.notBefore = TP;
else
throw new ArgumentException("Invalid date and time value.", "notBefore");
break;
case "notAfter":
if (XML.TryParse(P.Value as string, out TP))
this.notAfter = TP;
else
throw new ArgumentException("Invalid date and time value.", "notAfter");
break;
case "identifiers":
if (P.Value is Array A)
{
List<AcmeIdentifier> Identifiers = new List<AcmeIdentifier>();
foreach (object Obj2 in A)
{
if (Obj2 is IEnumerable<KeyValuePair<string, object>> Obj3)
Identifiers.Add(new AcmeIdentifier(Client, Obj3));
}
this.identifiers = Identifiers.ToArray();
}
break;
case "authorizations":
if (P.Value is Array A2)
{
List<Uri> Authorizations = new List<Uri>();
foreach (object Obj2 in A2)
{
if (Obj2 is string s)
Authorizations.Add(new Uri(s));
}
this.authorizationUris = Authorizations.ToArray();
}
break;
case "finalize":
this.finalize = new Uri(P.Value as string);
break;
case "certificate":
this.certificate = new Uri(P.Value as string);
break;
case "error":
if (P.Value is IEnumerable<KeyValuePair<string, object>> ErrorObj)
this.error = AcmeClient.CreateException(ErrorObj, Response);
break;
}
}
}
/// <summary>
/// An array of identifier objects that the order pertains to.
/// </summary>
public AcmeIdentifier[] Identifiers => this.identifiers;
/// <summary>
/// For pending orders, the authorizations that the client needs to complete before the
/// requested certificate can be issued.
/// </summary>
public Uri[] AuthorizationUris => this.authorizationUris;
/// <summary>
/// The status of this order.
/// </summary>
public AcmeOrderStatus Status => this.status;
/// <summary>
/// The timestamp after which the server will consider this order invalid
/// </summary>
public DateTime? Expires => this.expires;
/// <summary>
/// The requested value of the notBefore field in the certificate
/// </summary>
public DateTime? NotBefore => this.notBefore;
/// <summary>
/// The requested value of the notAfter field in the certificate
/// </summary>
public DateTime? NotAfter => this.notAfter;
/// <summary>
/// A URL that a CSR must be POSTed to once all of the order's authorizations are satisfied to finalize the order.
/// </summary>
public Uri Finalize => this.finalize;
/// <summary>
/// A URL for the certificate that has been issued in response to this order.
/// </summary>
public Uri Certificate => this.certificate;
/// <summary>
/// Any error, if available.
/// </summary>
public AcmeException Error => this.error;
/// <summary>
/// Gets the current state of the order.
/// </summary>
/// <returns>Current state of the order.</returns>
public Task<AcmeOrder> Poll()
{
return this.Client.GetOrder(this.AccountLocation, this.Location);
}
/// <summary>
/// Gets current authorization objects.
/// </summary>
/// <returns>Arary of authorization objects.</returns>
public async Task<AcmeAuthorization[]> GetAuthorizations()
{
if (this.authorizations is null)
{
int i, c = this.authorizationUris.Length;
AcmeAuthorization[] Result = new AcmeAuthorization[c];
for (i = 0; i < c; i++)
{
Uri Location = this.authorizationUris[i];
Result[i] = new AcmeAuthorization(this.Client, this.AccountLocation, Location,
(await this.Client.POST_as_GET(Location, this.AccountLocation)).Payload);
}
this.authorizations = Result;
}
return this.authorizations;
}
/// <summary>
/// Curernt authorization objects.
/// </summary>
public Task<AcmeAuthorization[]> Authorizations
{
get { return this.GetAuthorizations(); }
}
/// <summary>
/// Finalize order.
/// </summary>
/// <param name="CertificateRequest">Certificate request.</param>
/// <returns>New order object.</returns>
public Task<AcmeOrder> FinalizeOrder(CertificateRequest CertificateRequest)
{
return this.Client.FinalizeOrder(this.AccountLocation, this.finalize, CertificateRequest);
}
/// <summary>
/// Downloads the certificate.
/// </summary>
/// <returns>Certificate chain.</returns>
public Task<X509Certificate2[]> DownloadCertificate()
{
if (this.certificate is null)
throw new Exception("No certificate URI available.");
return this.Client.DownloadCertificate(this.AccountLocation, this.certificate);
}
}
}