EasySign BETA
Digital Signing Tool
Loading...
Searching...
No Matches
CommandProviderConfiguration.cs
Go to the documentation of this file.
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Security.Cryptography.X509Certificates;
5using System.Text;
6using System.Text.Json.Serialization;
7using System.Threading.Tasks;
8
10{
15 {
19 public Dictionary<string, bool> Settings { get; set; } = new Dictionary<string, bool>
20 {
21 ["trust.enable"] = true,
22 ["selfsign.enable"] = true,
23 };
24
28 protected string[] ProtectedPrefixes { get; set; } = [];
29
33 public Dictionary<string, byte[]> TrustedRootCA { get; set; } = [];
34
38 public Dictionary<string, byte[]> IntermediateCA { get; set; } = [];
39
43 public Dictionary<string, byte[]> IssuedCertificates { get; set; } = [];
44
48 public byte[]? SelfSignedRootCA { get; set; } = null;
49
54 {
55
56 }
57
67 public CommandProviderConfiguration(string[] protectedPrefixes)
68 {
69 AddProtectedPrefix(protectedPrefixes);
70 }
71
79 protected void CheckProtectedPrefix(string id)
80 {
81 if (IsProtected(id))
82 {
83 throw new InvalidOperationException($"The ID '{id}' is protected and cannot be modified.");
84 }
85 }
86
96 public bool IsProtected(string id)
97 {
98 return ProtectedPrefixes.Any(id.StartsWith);
99 }
100
108 public void AddProtectedPrefix(params string[] prefixes)
109 {
110 if (prefixes == null || prefixes.Length == 0)
111 {
112 throw new ArgumentNullException(nameof(prefixes), "Prefixes cannot be null or empty.");
113 }
114
115 ProtectedPrefixes = ProtectedPrefixes.Union(prefixes).ToArray();
116 }
117
122 {
123 AddProtectedPrefix("sapteam:");
124
125 TrustedRootCA["sapteam:rootca"] = SAPTeamCertificates.RootCA;
126 IntermediateCA["sapteam:packages"] = SAPTeamCertificates.PackagesIntermediateCA;
127 }
128
145 public string AddCertificate(CertificateStore certificateStore, X509Certificate2 certificate, string? id = null)
146 {
147 id = !string.IsNullOrEmpty(id) ? id : certificate.Thumbprint.ToLowerInvariant()[^6..];
148 if (certificateStore != CertificateStore.IssuedCertificates)
149 {
151 }
152
153 byte[] data = certificateStore == CertificateStore.IssuedCertificates ? certificate.Export(X509ContentType.Pfx) : certificate.Export(X509ContentType.Cert);
154
155 switch (certificateStore)
156 {
157 case CertificateStore.TrustedRootCA:
158 TrustedRootCA[id] = data;
159 break;
160 case CertificateStore.IntermediateCA:
161 IntermediateCA[id] = data;
162 break;
163 case CertificateStore.IssuedCertificates:
164 IssuedCertificates[id] = data;
165 break;
166 default:
167 throw new ArgumentOutOfRangeException(nameof(certificateStore), certificateStore, null);
168 }
169
170 return id;
171 }
172
183 public X509Certificate2Collection LoadCertificates(CertificateStore certificateStore)
184 {
185 X509Certificate2Collection certificates = new X509Certificate2Collection();
186
187 switch (certificateStore)
188 {
189 case CertificateStore.TrustedRootCA:
190 foreach (var id in TrustedRootCA.Keys)
191 {
192 certificates.Add(LoadCertificate(certificateStore, id));
193 }
194 break;
195 case CertificateStore.IntermediateCA:
196 foreach (var id in IntermediateCA.Keys)
197 {
198 certificates.Add(LoadCertificate(certificateStore, id));
199 }
200 break;
201 case CertificateStore.IssuedCertificates:
202 foreach (var id in IssuedCertificates.Keys)
203 {
204 certificates.Add(LoadCertificate(certificateStore, id));
205 }
206 break;
207 default:
208 throw new ArgumentOutOfRangeException(nameof(certificateStore), certificateStore, null);
209 }
210
211 return certificates;
212 }
213
225 public X509Certificate2 LoadCertificate(CertificateStore certificateStore, string id)
226 {
227 byte[] data;
228
229 switch (certificateStore)
230 {
231 case CertificateStore.TrustedRootCA:
232 data = TrustedRootCA[id];
233 break;
234 case CertificateStore.IntermediateCA:
235 data = IntermediateCA[id];
236 break;
237 case CertificateStore.IssuedCertificates:
238 data = IssuedCertificates[id];
239 break;
240 default:
241 throw new ArgumentOutOfRangeException(nameof(certificateStore), certificateStore, null);
242 }
243
244 return certificateStore == CertificateStore.IssuedCertificates
245 ? CertificateUtilities.ImportPFX(data).Single()
246 : CertificateUtilities.Import(data);
247 }
248
262 public bool RemoveCertificate(CertificateStore certificateStore, string id)
263 {
264 if (certificateStore != CertificateStore.IssuedCertificates)
265 {
267 }
268
269 bool result;
270
271 switch (certificateStore)
272 {
273 case CertificateStore.TrustedRootCA:
274 result = TrustedRootCA.Remove(id);
275 break;
276 case CertificateStore.IntermediateCA:
277 result = IntermediateCA.Remove(id);
278 break;
279 case CertificateStore.IssuedCertificates:
280 result = IssuedCertificates.Remove(id);
281 break;
282 default:
283 throw new ArgumentOutOfRangeException(nameof(certificateStore), certificateStore, null);
284 }
285
286 return result;
287 }
288 }
289}
Represents the configuration for the EasySign command provider.
byte?[] SelfSignedRootCA
Gets or sets the self-signed root CA certificate.
CommandProviderConfiguration()
Initializes a new instance of the CommandProviderConfiguration class.
void AddSAPTeamCertificates()
Adds the SAP Team certificates to the trusted root CA and intermediate CA stores and Locks the saptea...
void AddProtectedPrefix(params string[] prefixes)
Adds given prefixes to the list of Protected ID Prefixes.
Dictionary< string, byte[]> TrustedRootCA
Gets or sets the list of trusted root CA certificates.
bool IsProtected(string id)
Checks if the given ID starts with any of the protected prefixes.
string AddCertificate(CertificateStore certificateStore, X509Certificate2 certificate, string? id=null)
Adds a certificate to the specified certificate store.
X509Certificate2 LoadCertificate(CertificateStore certificateStore, string id)
Loads a certificate from the specified certificate store using the given ID.
string[] ProtectedPrefixes
Gets or sets the list of prefixes that should be protected from modification.
Dictionary< string, bool > Settings
Gets or sets the settings for the command provider.
CommandProviderConfiguration(string[] protectedPrefixes)
Initializes a new instance of the CommandProviderConfiguration class with the specified protected pre...
bool RemoveCertificate(CertificateStore certificateStore, string id)
Removes a certificate from the specified certificate store using the given ID.
void CheckProtectedPrefix(string id)
Checks if the given ID starts with any of the protected prefixes.
Dictionary< string, byte[]> IntermediateCA
Gets or sets the list of intermediate CA certificates.
X509Certificate2Collection LoadCertificates(CertificateStore certificateStore)
Loads all certificates from the specified certificate store.
Dictionary< string, byte[]> IssuedCertificates
Gets or sets the list of issued certificates by the self signing root CA.
CertificateStore
Enumeration of certificate stores in the CommandProviderConfiguration.