EasySign BETA
Digital Signing Tool
Loading...
Searching...
No Matches
CertificateSubject.cs
Go to the documentation of this file.
1using System;
2using System.Collections.Generic;
3using System.Diagnostics.Metrics;
4using System.Linq;
5using System.Security.AccessControl;
6using System.Security.Cryptography.X509Certificates;
7using System.Text;
8using System.Threading.Tasks;
9
10using EnsureThat;
11
13{
17 public class CertificateSubject
18 {
22 public string CommonName { get; set; }
23
27 public string? Email { get; set; }
28
32 public string? Organization { get; set; }
33
37 public string? OrganizationalUnit { get; set; }
38
42 public string? Locality { get; set; }
43
47 public string? State { get; set; }
48
52 public string? Country { get; set; }
53
57 public Dictionary<string, string> Unknown { get; } = [];
58
65 public CertificateSubject(X509Certificate2 certificate) : this(certificate.Subject)
66 {
67 var commonName = certificate.GetNameInfo(X509NameType.SimpleName, false);
68 if (string.IsNullOrEmpty(commonName))
69 {
70 commonName = certificate.GetNameInfo(X509NameType.DnsName, false);
71 }
72
73 if (!string.IsNullOrEmpty(commonName))
74 {
75 CommonName = commonName;
76 }
77
78 var email = certificate.GetNameInfo(X509NameType.EmailName, false);
79 if (!string.IsNullOrEmpty(email))
80 {
81 Email = email;
82 }
83 }
84
91 public CertificateSubject(string subject)
92 {
93 Ensure.String.IsNotNullOrEmpty(subject, nameof(subject));
94
95 var parts = subject.Split([","], StringSplitOptions.RemoveEmptyEntries);
96
97 foreach (var part in parts)
98 {
99 int index = part.IndexOf('=');
100 if (index <= 0 || index >= part.Length - 1)
101 continue; // Ignore malformed parts.
102
103 string key = part.Substring(0, index).Trim();
104 string value = part.Substring(index + 1).Trim();
105
106 // Map each key abbreviation to a property of the object
107 switch (key.ToUpperInvariant())
108 {
109 case "CN":
110 CommonName = value;
111 break;
112 case "E":
113 Email = value;
114 break;
115 case "O":
116 Organization = value;
117 break;
118 case "OU":
119 OrganizationalUnit = value;
120 break;
121 case "L":
122 Locality = value;
123 break;
124 case "ST":
125 case "S":
126 State = value;
127 break;
128 case "C":
129 Country = value;
130 break;
131 default:
132 Unknown[key.ToUpperInvariant()] = value; // Store unknown keys in the dictionary
133 break;
134 }
135 }
136
137 if (string.IsNullOrEmpty(CommonName))
138 {
139 CommonName = string.Empty;
140 }
141 }
142
143
154 public CertificateSubject(string commonName, string? email, string? organization, string? organizationalUnit, string? locality, string? state, string? country)
155 {
156 Ensure.String.IsNotNullOrEmpty(commonName, nameof(commonName));
157
158 CommonName = commonName;
159 Email = email;
160 Organization = organization;
161 OrganizationalUnit = organizationalUnit;
162 Locality = locality;
163 State = state;
164 Country = country;
165 }
166
167
174 public override string ToString()
175 {
176 var components = new List<string>
177 {
178 $"CN={CommonName}"
179 };
180
181 if (!string.IsNullOrEmpty(Email))
182 {
183 components.Add($"E={Email}");
184 }
185
186 if (!string.IsNullOrEmpty(Organization))
187 {
188 components.Add($"O={Organization}");
189 }
190
191 if (!string.IsNullOrEmpty(OrganizationalUnit))
192 {
193 components.Add($"OU={OrganizationalUnit}");
194 }
195
196 if (!string.IsNullOrEmpty(Locality))
197 {
198 components.Add($"L={Locality}");
199 }
200
201 if (!string.IsNullOrEmpty(State))
202 {
203 components.Add($"ST={State}");
204 }
205
206 if (!string.IsNullOrEmpty(Country))
207 {
208 components.Add($"C={Country}");
209 }
210
211 return string.Join(", ", components);
212 }
213 }
214
215}
Represents the subject of a certificate.
string? Email
Gets or sets the email address (E) of the certificate subject.
Dictionary< string, string > Unknown
Gets a dictionary of unknown keys and their values from the parsed subject string.
CertificateSubject(string subject)
Initializes a new instance of the CertificateSubject class with the specified subject string.
string? Organization
Gets or sets the organization (O) of the certificate subject.
string CommonName
Gets or sets the common name (CN) of the certificate subject.
string? Locality
Gets or sets the locality (L) of the certificate subject.
string? OrganizationalUnit
Gets or sets the organizational unit (OU) of the certificate subject.
string? Country
Gets or sets the country (C) of the certificate subject.
string? State
Gets or sets the state or province (ST) of the certificate subject.
CertificateSubject(X509Certificate2 certificate)
Initializes a new instance of the CertificateSubject class with the specified X509Certificate2 certif...
CertificateSubject(string commonName, string? email, string? organization, string? organizationalUnit, string? locality, string? state, string? country)
Initializes a new instance of the CertificateSubject class with the specified properties.
override string ToString()
Generates a comma-delimited string representation of the certificate subject.