Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
FindPal.cs
Go to the documentation of this file.
1using System;
8
10
11internal sealed class FindPal : IFindPal, IDisposable
12{
14 {
15 {
16 "DigitalSignature",
17 X509KeyUsageFlags.DigitalSignature
18 },
19 {
20 "NonRepudiation",
21 X509KeyUsageFlags.NonRepudiation
22 },
23 {
24 "KeyEncipherment",
25 X509KeyUsageFlags.KeyEncipherment
26 },
27 {
28 "DataEncipherment",
29 X509KeyUsageFlags.DataEncipherment
30 },
31 {
32 "KeyAgreement",
33 X509KeyUsageFlags.KeyAgreement
34 },
35 {
36 "KeyCertSign",
37 X509KeyUsageFlags.KeyCertSign
38 },
39 {
40 "CrlSign",
41 X509KeyUsageFlags.CrlSign
42 },
43 {
44 "EncipherOnly",
45 X509KeyUsageFlags.EncipherOnly
46 },
47 {
48 "DecipherOnly",
49 X509KeyUsageFlags.DecipherOnly
50 }
51 };
52
53 private readonly StorePal _storePal;
54
56
57 private readonly bool _validOnly;
58
60 {
63 {
64 switch (findType)
65 {
66 case X509FindType.FindByThumbprint:
67 {
68 byte[] thumbprint = ConfirmedCast<string>(findValue).DecodeHexString();
69 findPal.FindByThumbprint(thumbprint);
70 break;
71 }
72 case X509FindType.FindBySubjectName:
73 {
75 findPal.FindBySubjectName(subjectName);
76 break;
77 }
78 case X509FindType.FindBySubjectDistinguishedName:
79 {
81 findPal.FindBySubjectDistinguishedName(subjectDistinguishedName);
82 break;
83 }
84 case X509FindType.FindByIssuerName:
85 {
87 findPal.FindByIssuerName(issuerName);
88 break;
89 }
90 case X509FindType.FindByIssuerDistinguishedName:
91 {
93 findPal.FindByIssuerDistinguishedName(issuerDistinguishedName);
94 break;
95 }
96 case X509FindType.FindBySerialNumber:
97 {
99 byte[] array = text.DecodeHexString();
103 findPal.FindBySerialNumber(hexValue, decimalValue);
104 break;
105 }
106 case X509FindType.FindByTimeValid:
107 {
109 findPal.FindByTimeValid(dateTime3);
110 break;
111 }
112 case X509FindType.FindByTimeNotYetValid:
113 {
115 findPal.FindByTimeNotYetValid(dateTime2);
116 break;
117 }
118 case X509FindType.FindByTimeExpired:
119 {
121 findPal.FindByTimeExpired(dateTime);
122 break;
123 }
124 case X509FindType.FindByTemplateName:
125 {
127 findPal.FindByTemplateName(templateName);
128 break;
129 }
130 case X509FindType.FindByApplicationPolicy:
131 {
133 findPal.FindByApplicationPolicy(oidValue3);
134 break;
135 }
136 case X509FindType.FindByCertificatePolicy:
137 {
139 findPal.FindByCertificatePolicy(oidValue2);
140 break;
141 }
142 case X509FindType.FindByExtension:
143 {
144 string oidValue = ConfirmedOidValue(findPal, findValue, OidGroup.ExtensionOrAttribute);
145 findPal.FindByExtension(oidValue);
146 break;
147 }
148 case X509FindType.FindByKeyUsage:
149 {
151 findPal.FindByKeyUsage(keyUsage);
152 break;
153 }
154 case X509FindType.FindBySubjectKeyIdentifier:
155 {
156 byte[] keyIdentifier = ConfirmedCast<string>(findValue).DecodeHexString();
157 findPal.FindBySubjectKeyIdentifier(keyIdentifier);
158 break;
159 }
160 default:
162 }
163 }
165 }
166
167 private static T ConfirmedCast<T>(object findValue)
168 {
169 if (findValue.GetType() != typeof(T))
170 {
172 }
173 return (T)findValue;
174 }
175
177 {
179 if (text.Length == 0)
180 {
182 }
183 return findPal.NormalizeOid(text, oidGroup);
184 }
185
187 {
189 {
191 }
192 if (findValue is int)
193 {
194 return (X509KeyUsageFlags)(int)findValue;
195 }
196 if (findValue is uint)
197 {
198 return (X509KeyUsageFlags)(uint)findValue;
199 }
200 if (findValue is string key && s_keyUsages.TryGetValue(key, out var value))
201 {
202 return value;
203 }
205 }
206
207 internal static void ValidateOidValue(string keyValue)
208 {
209 if (keyValue == null)
210 {
211 throw new ArgumentNullException("keyValue");
212 }
213 int length = keyValue.Length;
214 if (length < 2)
215 {
217 }
218 char c = keyValue[0];
219 if (c != '0' && c != '1' && c != '2')
220 {
222 }
223 if (keyValue[1] != '.' || keyValue[length - 1] == '.')
224 {
226 }
227 for (int i = 1; i < length; i++)
228 {
229 if (!char.IsDigit(keyValue[i]) && (keyValue[i] != '.' || keyValue[i + 1] == '.'))
230 {
232 }
233 }
234 }
235
237 {
238 if (bytes.Length == 0 || bytes[^1] < 128)
239 {
240 return new BigInteger(bytes);
241 }
242 byte[] array = new byte[bytes.Length + 1];
243 Buffer.BlockCopy(bytes, 0, array, 0, bytes.Length);
244 return new BigInteger(array);
245 }
246
248 {
249 BigInteger right = new BigInteger(10);
251 foreach (char c in decimalString)
252 {
253 if (c >= '0' && c <= '9')
254 {
257 }
258 }
259 return bigInteger;
260 }
261
268
273
275 {
276 string text = global::Interop.Crypt32.FindOidInfo(global::Interop.Crypt32.CryptOidInfoKeyType.CRYPT_OID_INFO_NAME_KEY, maybeOid, expectedGroup, fallBackToAllGroups: true).OID;
277 if (text == null)
278 {
279 text = maybeOid;
281 }
282 return text;
283 }
284
285 public unsafe void FindByThumbprint(byte[] thumbPrint)
286 {
287 fixed (byte* pbData = thumbPrint)
288 {
291 }
292 }
293
294 public unsafe void FindBySubjectName(string subjectName)
295 {
297 {
298 FindCore<object>(CertFindType.CERT_FIND_SUBJECT_STR, pvFindPara);
299 }
300 }
301
303 {
305 {
306 string certNameInfo = GetCertNameInfo(pCertContext, CertNameType.CERT_NAME_RDN_TYPE, CertNameFlags.None);
307 return subjectDistinguishedName.Equals(certNameInfo, StringComparison.OrdinalIgnoreCase);
308 });
309 }
310
311 public unsafe void FindByIssuerName(string issuerName)
312 {
313 fixed (char* pvFindPara = issuerName)
314 {
315 FindCore<object>(CertFindType.CERT_FIND_ISSUER_STR, pvFindPara);
316 }
317 }
318
320 {
322 {
323 string certNameInfo = GetCertNameInfo(pCertContext, CertNameType.CERT_NAME_RDN_TYPE, CertNameFlags.CERT_NAME_ISSUER_FLAG);
324 return issuerDistinguishedName.Equals(certNameInfo, StringComparison.OrdinalIgnoreCase);
325 });
326 }
327
329 {
331 {
332 byte[] bytes = pCertContext.CertContext->pCertInfo->SerialNumber.ToByteArray();
333 GC.KeepAlive(pCertContext);
335 return state.hexValue.Equals(other) || state.decimalValue.Equals(other);
336 });
337 }
338
340 {
342 }
343
345 {
346 FindByTime(dateTime, -1);
347 }
348
350 {
352 }
353
354 private unsafe void FindByTime(DateTime dateTime, int compareResult)
355 {
358 {
359 int num = global::Interop.crypt32.CertVerifyTimeValidity(ref state.fileTime, pCertContext.CertContext->pCertInfo);
360 GC.KeepAlive(pCertContext);
361 return num == state.compareResult;
362 });
363 }
364
365 public unsafe void FindByTemplateName(string templateName)
366 {
367 FindCore(templateName, delegate(string templateName, SafeCertContextHandle pCertContext)
368 {
369 bool foundMatch = false;
370 CERT_INFO* pCertInfo = pCertContext.CertContext->pCertInfo;
371 CERT_EXTENSION* ptr = global::Interop.crypt32.CertFindExtension("1.3.6.1.4.1.311.20.2", pCertInfo->cExtension, pCertInfo->rgExtension);
372 if (ptr != null)
373 {
374 byte[] encoded = ptr->Value.ToByteArray();
375 if (!encoded.DecodeObjectNoThrow(CryptDecodeObjectStructType.X509_UNICODE_ANY_STRING, delegate(void* pvDecoded, int cbDecoded)
376 {
377 string value2 = Marshal.PtrToStringUni(new IntPtr(((CERT_NAME_VALUE*)pvDecoded)->Value.pbData));
378 if (templateName.Equals(value2, StringComparison.OrdinalIgnoreCase))
379 {
380 foundMatch = true;
381 }
382 }))
383 {
384 return false;
385 }
386 }
387 if (!foundMatch)
388 {
389 CERT_EXTENSION* ptr2 = global::Interop.crypt32.CertFindExtension("1.3.6.1.4.1.311.21.7", pCertInfo->cExtension, pCertInfo->rgExtension);
390 if (ptr2 != null)
391 {
392 byte[] encoded2 = ptr2->Value.ToByteArray();
393 if (!encoded2.DecodeObjectNoThrow(CryptDecodeObjectStructType.X509_CERTIFICATE_TEMPLATE, delegate(void* pvDecoded, int cbDecoded)
394 {
396 string text = global::Interop.Crypt32.FindOidInfo(global::Interop.Crypt32.CryptOidInfoKeyType.CRYPT_OID_INFO_NAME_KEY, templateName, OidGroup.Template, fallBackToAllGroups: true).OID;
397 if (text == null)
398 {
400 }
401 if (text.Equals(value, StringComparison.OrdinalIgnoreCase))
402 {
403 foundMatch = true;
404 }
405 }))
406 {
407 return false;
408 }
409 }
410 }
411 GC.KeepAlive(pCertContext);
412 return foundMatch;
413 });
414 }
415
416 public unsafe void FindByApplicationPolicy(string oidValue)
417 {
418 FindCore(oidValue, delegate(string oidValue, SafeCertContextHandle pCertContext)
419 {
420 int pcbOIDs = 0;
421 if (!global::Interop.crypt32.CertGetValidUsages(1, ref pCertContext, out var cNumOIDs, null, ref pcbOIDs))
422 {
423 return false;
424 }
425 if (cNumOIDs == -1)
426 {
427 return true;
428 }
429 fixed (byte* ptr = new byte[pcbOIDs])
430 {
431 if (!global::Interop.crypt32.CertGetValidUsages(1, ref pCertContext, out cNumOIDs, ptr, ref pcbOIDs))
432 {
433 return false;
434 }
435 IntPtr* ptr2 = (IntPtr*)ptr;
436 for (int i = 0; i < cNumOIDs; i++)
437 {
438 string value = Marshal.PtrToStringAnsi(ptr2[i]);
439 if (oidValue.Equals(value, StringComparison.OrdinalIgnoreCase))
440 {
441 return true;
442 }
443 }
444 return false;
445 }
446 });
447 }
448
449 public unsafe void FindByCertificatePolicy(string oidValue)
450 {
451 FindCore(oidValue, delegate(string oidValue, SafeCertContextHandle pCertContext)
452 {
453 CERT_INFO* pCertInfo = pCertContext.CertContext->pCertInfo;
454 CERT_EXTENSION* ptr = global::Interop.crypt32.CertFindExtension("2.5.29.32", pCertInfo->cExtension, pCertInfo->rgExtension);
455 if (ptr == null)
456 {
457 return false;
458 }
459 bool foundMatch = false;
460 byte[] encoded = ptr->Value.ToByteArray();
461 if (!encoded.DecodeObjectNoThrow(CryptDecodeObjectStructType.X509_CERT_POLICIES, delegate(void* pvDecoded, int cbDecoded)
462 {
463 for (int i = 0; i < ((CERT_POLICIES_INFO*)pvDecoded)->cPolicyInfo; i++)
464 {
465 CERT_POLICY_INFO* ptr2 = ((CERT_POLICIES_INFO*)pvDecoded)->rgPolicyInfo + i;
466 string value = Marshal.PtrToStringAnsi(ptr2->pszPolicyIdentifier);
467 if (oidValue.Equals(value, StringComparison.OrdinalIgnoreCase))
468 {
469 foundMatch = true;
470 break;
471 }
472 }
473 }))
474 {
475 return false;
476 }
477 GC.KeepAlive(pCertContext);
478 return foundMatch;
479 });
480 }
481
482 public unsafe void FindByExtension(string oidValue)
483 {
484 FindCore(oidValue, delegate(string oidValue, SafeCertContextHandle pCertContext)
485 {
486 CERT_INFO* pCertInfo = pCertContext.CertContext->pCertInfo;
487 CERT_EXTENSION* ptr = global::Interop.crypt32.CertFindExtension(oidValue, pCertInfo->cExtension, pCertInfo->rgExtension);
488 GC.KeepAlive(pCertContext);
489 return ptr != null;
490 });
491 }
492
494 {
496 {
497 CERT_INFO* pCertInfo = pCertContext.CertContext->pCertInfo;
498 if (!global::Interop.crypt32.CertGetIntendedKeyUsage(CertEncodingType.All, pCertInfo, out var pbKeyUsage, 4))
499 {
500 return true;
501 }
502 GC.KeepAlive(pCertContext);
503 return (pbKeyUsage & keyUsage) == keyUsage;
504 });
505 }
506
508 {
509 FindCore(keyIdentifier, delegate(byte[] keyIdentifier, SafeCertContextHandle pCertContext)
510 {
511 int pcbData = 0;
512 if (!global::Interop.crypt32.CertGetCertificateContextProperty(pCertContext, CertContextPropId.CERT_KEY_IDENTIFIER_PROP_ID, null, ref pcbData))
513 {
514 return false;
515 }
516 byte[] array = new byte[pcbData];
517 return global::Interop.crypt32.CertGetCertificateContextProperty(pCertContext, CertContextPropId.CERT_KEY_IDENTIFIER_PROP_ID, array, ref pcbData) && keyIdentifier.ContentsEqual(array);
518 });
519 }
520
521 public void Dispose()
522 {
524 }
525
527 {
528 FindCore(CertFindType.CERT_FIND_ANY, null, state, filter);
529 }
530
532 {
533 SafeCertStoreHandle safeCertStoreHandle = global::Interop.crypt32.CertOpenStore(CertStoreProvider.CERT_STORE_PROV_MEMORY, CertEncodingType.All, IntPtr.Zero, CertStoreFlags.CERT_STORE_ENUM_ARCHIVED_FLAG | CertStoreFlags.CERT_STORE_CREATE_NEW_FLAG, null);
534 if (safeCertStoreHandle.IsInvalid)
535 {
536 throw Marshal.GetHRForLastWin32Error().ToCryptographicException();
537 }
538 SafeCertContextHandle pCertContext = null;
539 while (global::Interop.crypt32.CertFindCertificateInStore(_storePal.SafeCertStoreHandle, dwFindType, pvFindPara, ref pCertContext))
540 {
541 if ((filter == null || filter(state, pCertContext)) && (!_validOnly || VerifyCertificateIgnoringErrors(pCertContext)) && !global::Interop.crypt32.CertAddCertificateLinkToStore(safeCertStoreHandle, pCertContext, CertStoreAddDisposition.CERT_STORE_ADD_ALWAYS, IntPtr.Zero))
542 {
543 throw Marshal.GetLastWin32Error().ToCryptographicException();
544 }
545 }
548 }
549
551 {
552 ChainPal chainPal = ChainPal.BuildChain(useMachineContext: false, CertificatePal.FromHandle(pCertContext.DangerousGetHandle()), null, null, null, X509RevocationMode.NoCheck, X509RevocationFlag.ExcludeRoot, null, X509ChainTrustMode.System, DateTime.Now, new TimeSpan(0, 0, 0), disableAia: false);
553 if (chainPal == null)
554 {
555 return false;
556 }
557 using (chainPal)
558 {
559 if (!chainPal.Verify(X509VerificationFlags.NoFlag, out var _).GetValueOrDefault())
560 {
561 return false;
562 }
563 }
564 return true;
565 }
566
568 {
569 return global::Interop.crypt32.CertGetNameString(pCertContext, dwNameType, dwNameFlags, (CertNameStringType)33554435);
570 }
571}
static ICertificatePal FromHandle(IntPtr handle)
static unsafe ChainPal BuildChain(bool useMachineContext, ICertificatePal cert, X509Certificate2Collection extraStore, OidCollection applicationPolicy, OidCollection certificatePolicy, X509RevocationMode revocationMode, X509RevocationFlag revocationFlag, X509Certificate2Collection customTrustStore, X509ChainTrustMode trustMode, DateTime verificationTime, TimeSpan timeout, bool disableAia)
Definition ChainPal.cs:94
void FindByTimeExpired(DateTime dateTime)
Definition FindPal.cs:349
string NormalizeOid(string maybeOid, OidGroup expectedGroup)
Definition FindPal.cs:274
unsafe void FindByExtension(string oidValue)
Definition FindPal.cs:482
static string ConfirmedOidValue(IFindPal findPal, object findValue, OidGroup oidGroup)
Definition FindPal.cs:176
unsafe void FindByCertificatePolicy(string oidValue)
Definition FindPal.cs:449
unsafe void FindByThumbprint(byte[] thumbPrint)
Definition FindPal.cs:285
void FindByTimeNotYetValid(DateTime dateTime)
Definition FindPal.cs:344
static string GetCertNameInfo(SafeCertContextHandle pCertContext, CertNameType dwNameType, CertNameFlags dwNameFlags)
Definition FindPal.cs:567
static IFindPal OpenPal(X509Certificate2Collection findFrom, X509Certificate2Collection copyTo, bool validOnly)
Definition FindPal.cs:269
FindPal(X509Certificate2Collection findFrom, X509Certificate2Collection copyTo, bool validOnly)
Definition FindPal.cs:262
unsafe void FindCore< TState >(TState state, Func< TState, SafeCertContextHandle, bool > filter)
Definition FindPal.cs:526
unsafe void FindByApplicationPolicy(string oidValue)
Definition FindPal.cs:416
static X509Certificate2Collection FindFromCollection(X509Certificate2Collection coll, X509FindType findType, object findValue, bool validOnly)
Definition FindPal.cs:59
unsafe void FindByTime(DateTime dateTime, int compareResult)
Definition FindPal.cs:354
static X509KeyUsageFlags ConfirmedX509KeyUsage(object findValue)
Definition FindPal.cs:186
void FindBySubjectKeyIdentifier(byte[] keyIdentifier)
Definition FindPal.cs:507
static T ConfirmedCast< T >(object findValue)
Definition FindPal.cs:167
static bool VerifyCertificateIgnoringErrors(SafeCertContextHandle pCertContext)
Definition FindPal.cs:550
unsafe void FindByTemplateName(string templateName)
Definition FindPal.cs:365
readonly X509Certificate2Collection _copyTo
Definition FindPal.cs:55
static void ValidateOidValue(string keyValue)
Definition FindPal.cs:207
static BigInteger PositiveBigIntegerFromByteArray(byte[] bytes)
Definition FindPal.cs:236
unsafe void FindBySubjectName(string subjectName)
Definition FindPal.cs:294
unsafe void FindBySerialNumber(BigInteger hexValue, BigInteger decimalValue)
Definition FindPal.cs:328
void FindByTimeValid(DateTime dateTime)
Definition FindPal.cs:339
static BigInteger LaxParseDecimalBigInteger(string decimalString)
Definition FindPal.cs:247
unsafe void FindByIssuerName(string issuerName)
Definition FindPal.cs:311
void FindBySubjectDistinguishedName(string subjectDistinguishedName)
Definition FindPal.cs:302
static readonly Dictionary< string, X509KeyUsageFlags > s_keyUsages
Definition FindPal.cs:13
readonly StorePal _storePal
Definition FindPal.cs:53
void FindByIssuerDistinguishedName(string issuerDistinguishedName)
Definition FindPal.cs:319
unsafe void FindByKeyUsage(X509KeyUsageFlags keyUsage)
Definition FindPal.cs:493
SafeCertStoreHandle SafeCertStoreHandle
Definition StorePal.cs:15
static IExportPal LinkFromCertificateCollection(X509Certificate2Collection certificates)
Definition StorePal.cs:269
static void Reverse(Array array)
Definition Array.cs:2207
static void BlockCopy(Array src, int srcOffset, Array dst, int dstOffset, int count)
Definition Buffer.cs:102
void CopyTo(KeyValuePair< TKey, TValue >[] array, int index)
static void KeepAlive(object? obj)
Definition GC.cs:180
Definition GC.cs:8
static unsafe? string PtrToStringUni(IntPtr ptr)
Definition Marshal.cs:652
static unsafe? string PtrToStringAnsi(IntPtr ptr)
Definition Marshal.cs:630
static string Cryptography_X509_InvalidFindValue
Definition SR.cs:98
static string Argument_InvalidOidValue
Definition SR.cs:20
static string Cryptography_X509_InvalidFindType
Definition SR.cs:96
Definition SR.cs:7
static StringComparer OrdinalIgnoreCase
unsafe CERT_EXTENSION * rgExtension
Definition CERT_INFO.cs:27
static FILETIME FromDateTime(DateTime dt)
Definition FILETIME.cs:17
static DateTime Now
Definition DateTime.cs:103
static readonly IntPtr Zero
Definition IntPtr.cs:18
static BigInteger Multiply(BigInteger left, BigInteger right)
static BigInteger Add(BigInteger left, BigInteger right)
static BigInteger Zero
Definition BigInteger.cs:32