Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
CertificateRequest.cs
Go to the documentation of this file.
7
9
10public sealed class CertificateRequest
11{
12 private readonly AsymmetricAlgorithm _key;
13
15
17
19
21
22
23 public PublicKey PublicKey { get; }
24
26
28 {
29 if (subjectName == null)
30 {
31 throw new ArgumentNullException("subjectName");
32 }
33 if (key == null)
34 {
35 throw new ArgumentNullException("key");
36 }
37 if (string.IsNullOrEmpty(hashAlgorithm.Name))
38 {
40 }
42 _key = key;
45 HashAlgorithm = hashAlgorithm;
46 }
47
49 {
50 if (subjectName == null)
51 {
52 throw new ArgumentNullException("subjectName");
53 }
54 if (key == null)
55 {
56 throw new ArgumentNullException("key");
57 }
58 if (string.IsNullOrEmpty(hashAlgorithm.Name))
59 {
61 }
63 _key = key;
66 HashAlgorithm = hashAlgorithm;
67 }
68
70 {
71 if (subjectName == null)
72 {
73 throw new ArgumentNullException("subjectName");
74 }
75 if (key == null)
76 {
77 throw new ArgumentNullException("key");
78 }
79 if (string.IsNullOrEmpty(hashAlgorithm.Name))
80 {
82 }
83 if (padding == null)
84 {
85 throw new ArgumentNullException("padding");
86 }
88 _key = key;
90 _rsaPadding = padding;
92 HashAlgorithm = hashAlgorithm;
93 }
94
96 {
97 if (subjectName == null)
98 {
99 throw new ArgumentNullException("subjectName");
100 }
101 if (key == null)
102 {
103 throw new ArgumentNullException("key");
104 }
105 if (string.IsNullOrEmpty(hashAlgorithm.Name))
106 {
108 }
109 if (padding == null)
110 {
111 throw new ArgumentNullException("padding");
112 }
114 _key = key;
116 _rsaPadding = padding;
118 HashAlgorithm = hashAlgorithm;
119 }
120
122 {
123 if (subjectName == null)
124 {
125 throw new ArgumentNullException("subjectName");
126 }
127 if (publicKey == null)
128 {
129 throw new ArgumentNullException("publicKey");
130 }
131 if (string.IsNullOrEmpty(hashAlgorithm.Name))
132 {
134 }
137 HashAlgorithm = hashAlgorithm;
138 }
139
140 public byte[] CreateSigningRequest()
141 {
142 if (_generator == null)
143 {
145 }
147 }
148
150 {
151 if (signatureGenerator == null)
152 {
153 throw new ArgumentNullException("signatureGenerator");
154 }
155 X501Attribute[] attributes = Array.Empty<X501Attribute>();
157 {
158 attributes = new X501Attribute[1]
159 {
161 };
162 }
165 }
166
168 {
169 if (notAfter < notBefore)
170 {
172 }
173 if (_key == null)
174 {
176 }
177 Span<byte> span = stackalloc byte[8];
180 {
181 if (_key is RSA privateKey)
182 {
183 return certificate.CopyWithPrivateKey(privateKey);
184 }
186 {
187 return certificate.CopyWithPrivateKey(privateKey2);
188 }
189 }
190 throw new CryptographicException();
191 }
192
197
199 {
200 if (issuerCertificate == null)
201 {
202 throw new ArgumentNullException("issuerCertificate");
203 }
204 if (!issuerCertificate.HasPrivateKey)
205 {
207 }
208 if (notAfter < notBefore)
209 {
211 }
212 if (serialNumber.IsEmpty)
213 {
214 throw new ArgumentException(System.SR.Arg_EmptyOrNullArray, "serialNumber");
215 }
216 if (issuerCertificate.PublicKey.Oid.Value != PublicKey.Oid.Value)
217 {
219 }
220 DateTime localDateTime = notBefore.LocalDateTime;
221 if (localDateTime < issuerCertificate.NotBefore)
222 {
224 }
225 DateTime localDateTime2 = notAfter.LocalDateTime;
226 long ticks = localDateTime2.Ticks;
227 long num = ticks % 10000000;
228 ticks -= num;
229 localDateTime2 = new DateTime(ticks, localDateTime2.Kind);
230 if (localDateTime2 > issuerCertificate.NotAfter)
231 {
233 }
237 {
239 }
240 if (!x509BasicConstraintsExtension.CertificateAuthority)
241 {
243 }
244 if (x509KeyUsageExtension != null && (x509KeyUsageExtension.KeyUsages & X509KeyUsageFlags.KeyCertSign) == 0)
245 {
247 }
249 string keyAlgorithm = issuerCertificate.GetKeyAlgorithm();
250 try
251 {
253 if (!(keyAlgorithm == "1.2.840.113549.1.1.1"))
254 {
255 if (!(keyAlgorithm == "1.2.840.10045.2.1"))
256 {
258 }
259 ECDsa eCDsaPrivateKey = issuerCertificate.GetECDsaPrivateKey();
262 }
263 else
264 {
265 if (_rsaPadding == null)
266 {
268 }
269 RSA rSAPrivateKey = issuerCertificate.GetRSAPrivateKey();
272 }
274 }
275 finally
276 {
277 asymmetricAlgorithm?.Dispose();
278 }
279 }
280
285
287 {
288 if (issuerName == null)
289 {
290 throw new ArgumentNullException("issuerName");
291 }
292 if (generator == null)
293 {
294 throw new ArgumentNullException("generator");
295 }
296 if (notAfter < notBefore)
297 {
299 }
300 if (serialNumber == null || serialNumber.Length < 1)
301 {
302 throw new ArgumentException(System.SR.Arg_EmptyOrNullArray, "serialNumber");
303 }
304 byte[] signatureAlgorithmIdentifier = generator.GetSignatureAlgorithmIdentifier(HashAlgorithm);
306 if (signatureAlgorithm.Parameters.HasValue)
307 {
309 }
312 tbsCertificateAsn.Version = 2;
313 tbsCertificateAsn.SerialNumber = arraySegment;
314 tbsCertificateAsn.SignatureAlgorithm = signatureAlgorithm;
315 tbsCertificateAsn.Issuer = issuerName.RawData;
316 tbsCertificateAsn.SubjectPublicKeyInfo = new System.Security.Cryptography.Asn1.SubjectPublicKeyInfoAsn
317 {
319 {
320 Algorithm = PublicKey.Oid.Value,
321 Parameters = PublicKey.EncodedParameters.RawData
322 },
323 SubjectPublicKey = PublicKey.EncodedKeyValue.RawData
324 };
325 tbsCertificateAsn.Validity = new ValidityAsn(notBefore, notAfter);
326 tbsCertificateAsn.Subject = SubjectName.RawData;
329 {
333 {
334 if (certificateExtension != null)
335 {
336 if (!hashSet.Add(certificateExtension.Oid.Value))
337 {
339 }
341 }
342 }
343 if (list.Count > 0)
344 {
345 tbsCertificate.Extensions = list.ToArray();
346 }
347 }
350 byte[] data = asnWriter.Encode();
351 asnWriter.Reset();
353 certificateAsn.TbsCertificate = tbsCertificate;
354 certificateAsn.SignatureAlgorithm = signatureAlgorithm;
355 certificateAsn.SignatureValue = generator.SignData(data, HashAlgorithm);
358 X509Certificate2 result = new X509Certificate2(asnWriter.Encode());
360 return result;
361 }
362
364 {
365 byte[] array;
366 if (serialNumber[0] >= 128)
367 {
369 array[0] = 0;
370 serialNumber.CopyTo(array.AsSpan(1));
371 return new ArraySegment<byte>(array, 0, serialNumber.Length + 1);
372 }
373 int i;
374 for (i = 0; i < serialNumber.Length - 1 && serialNumber[i] == 0 && serialNumber[i + 1] < 128; i++)
375 {
376 }
377 int num = serialNumber.Length - i;
379 serialNumber.Slice(i).CopyTo(array);
380 return new ArraySegment<byte>(array, 0, num);
381 }
382}
static void ValidateDer(ReadOnlyMemory< byte > encodedValue)
Definition Helpers.cs:151
void CopyTo(KeyValuePair< TKey, TValue >[] array, int index)
void Add(TKey key, TValue value)
static string Cryptography_CertReq_BasicConstraintsRequired
Definition SR.cs:44
static string Cryptography_CertReq_NotBeforeNotNested
Definition SR.cs:58
static string Format(string resourceFormat, object p1)
Definition SR.cs:118
static string Cryptography_CertReq_NoKeyProvided
Definition SR.cs:60
static string Cryptography_CertReq_AlgorithmMustMatch
Definition SR.cs:42
static string Cryptography_CertReq_IssuerBasicConstraintsInvalid
Definition SR.cs:50
static string Cryptography_CertReq_DuplicateExtension
Definition SR.cs:48
static string Cryptography_CertReq_IssuerKeyUsageInvalid
Definition SR.cs:52
static string Cryptography_UnknownKeyAlgorithm
Definition SR.cs:90
static string Cryptography_CertReq_RSAPaddingRequired
Definition SR.cs:62
static string Cryptography_CertReq_DatesReversed
Definition SR.cs:46
static string Cryptography_CertReq_NotAfterNotNested
Definition SR.cs:56
static string Cryptography_CertReq_IssuerRequiresPrivateKey
Definition SR.cs:54
static string Arg_EmptyOrNullArray
Definition SR.cs:14
static string Cryptography_HashAlgorithmNameNullOrEmpty
Definition SR.cs:60
Definition SR.cs:7
static void Return(byte[] array, int clearSize=-1)
Definition CryptoPool.cs:12
static byte[] Rent(int minimumLength)
Definition CryptoPool.cs:7
CertificateRequest(string subjectName, RSA key, HashAlgorithmName hashAlgorithm, RSASignaturePadding padding)
CertificateRequest(X500DistinguishedName subjectName, ECDsa key, HashAlgorithmName hashAlgorithm)
CertificateRequest(X500DistinguishedName subjectName, RSA key, HashAlgorithmName hashAlgorithm, RSASignaturePadding padding)
X509Certificate2 Create(X500DistinguishedName issuerName, X509SignatureGenerator generator, DateTimeOffset notBefore, DateTimeOffset notAfter, ReadOnlySpan< byte > serialNumber)
CertificateRequest(X500DistinguishedName subjectName, PublicKey publicKey, HashAlgorithmName hashAlgorithm)
CertificateRequest(string subjectName, ECDsa key, HashAlgorithmName hashAlgorithm)
byte[] CreateSigningRequest(X509SignatureGenerator signatureGenerator)
X509Certificate2 Create(X509Certificate2 issuerCertificate, DateTimeOffset notBefore, DateTimeOffset notAfter, ReadOnlySpan< byte > serialNumber)
ArraySegment< byte > NormalizeSerialNumber(ReadOnlySpan< byte > serialNumber)
X509Certificate2 CreateSelfSigned(DateTimeOffset notBefore, DateTimeOffset notAfter)
X509Certificate2 Create(X500DistinguishedName issuerName, X509SignatureGenerator generator, DateTimeOffset notBefore, DateTimeOffset notAfter, byte[] serialNumber)
X509Certificate2 Create(X509Certificate2 issuerCertificate, DateTimeOffset notBefore, DateTimeOffset notAfter, byte[] serialNumber)
static X509SignatureGenerator CreateForRSA(RSA key, RSASignaturePadding signaturePadding)
static void Decode(ref AsnValueReader reader, ReadOnlyMemory< byte > rebind, out AlgorithmIdentifierAsn decoded)