24 ushort num = (ushort)keyUsages;
26 cRYPT_BIT_BLOB.cbData = 2;
27 cRYPT_BIT_BLOB.pbData = (
byte*)(&num);
28 cRYPT_BIT_BLOB.cUnusedBits = 0;
45 return *(ushort*)pbData;
56 cERT_BASIC_CONSTRAINTS2_INFO.fCA = (certificateAuthority ? 1 : 0);
57 cERT_BASIC_CONSTRAINTS2_INFO.fPathLenConstraint = (hasPathLengthConstraint ? 1 : 0);
58 cERT_BASIC_CONSTRAINTS2_INFO.dwPathLenConstraint = pathLengthConstraint;
60 return global::Interop.crypt32.EncodeObject(
"2.5.29.19", &cERT_BASIC_CONSTRAINTS2_INFO2);
76 using SafeHandle safeHandle = usages.ToLpstrArray(out numOids);
78 cERT_ENHKEY_USAGE.cUsageIdentifier = numOids;
79 cERT_ENHKEY_USAGE.rgpszUsageIdentifier = (
IntPtr*)(
void*)safeHandle.DangerousGetHandle();
81 return global::Interop.crypt32.EncodeObject(
"2.5.29.37", &cERT_ENHKEY_USAGE2);
90 for (
int i = 0; i < cUsageIdentifier; i++)
95 oidCollection.
Add(oid2);
103 fixed (
byte* pbData = subjectKeyIdentifier)
106 return global::Interop.crypt32.EncodeObject(
"2.5.29.14", &cRYPTOAPI_BLOB);
112 subjectKeyIdentifier = encoded.DecodeObject(
"2.5.29.14", (
void* pvDecoded,
int cbDecoded) => ((
CRYPTOAPI_BLOB*)pvDecoded)->ToByteArray());
117 fixed (
byte*
value =
key.Oid.ValueAsAscii())
119 byte[] rawData =
key.EncodedParameters.RawData;
120 fixed (
byte* pbData = rawData)
122 byte[] rawData2 =
key.EncodedKeyValue.RawData;
123 fixed (
byte* pbData2 = rawData2)
133 cbData = rawData2.Length,
138 int pcbComputedHash = 20;
139 byte[]
array =
new byte[pcbComputedHash];
144 if (pcbComputedHash <
array.Length)
146 byte[] array2 =
new byte[pcbComputedHash];
159 fixed (
byte* pbData = rawData)
162 if (!global::Interop.crypt32.CryptQueryObject(
CertQueryObjectType.CERT_QUERY_OBJECT_BLOB, &cRYPTOAPI_BLOB,
ExpectedContentTypeFlags.CERT_QUERY_CONTENT_FLAG_ALL,
ExpectedFormatTypeFlags.CERT_QUERY_FORMAT_FLAG_ALL, 0,
IntPtr.
Zero, out pdwContentType,
IntPtr.
Zero,
IntPtr.
Zero,
IntPtr.
Zero,
IntPtr.
Zero))
173 fixed (
char* pvObject = fileName)
175 if (!global::Interop.crypt32.CryptQueryObject(
CertQueryObjectType.CERT_QUERY_OBJECT_FILE, pvObject,
ExpectedContentTypeFlags.CERT_QUERY_CONTENT_FLAG_ALL,
ExpectedFormatTypeFlags.CERT_QUERY_FORMAT_FLAG_ALL, 0,
IntPtr.
Zero, out pdwContentType,
IntPtr.
Zero,
IntPtr.
Zero,
IntPtr.
Zero,
IntPtr.
Zero))
189 case ContentType.CERT_QUERY_CONTENT_SERIALIZED_STORE:
191 case ContentType.CERT_QUERY_CONTENT_SERIALIZED_CERT:
194 case ContentType.CERT_QUERY_CONTENT_PKCS7_UNSIGNED:
196 case ContentType.CERT_QUERY_CONTENT_PKCS7_SIGNED_EMBED:
231 switch (global::Interop.Crypt32.FindOidInfo(global::Interop.Crypt32.CryptOidInfoKeyType.CRYPT_OID_INFO_OID_KEY, oid.
Value,
OidGroup.PublicKeyAlgorithm, fallBackToAllGroups:
true).AlgId)
245 return dSACryptoServiceProvider;
258 if (curveName ==
null)
261 byte[] keyBlob =
ExportKeyBlob(safeBCryptKeyHandle, cngKeyBlobFormat);
268 byte[] keyBlob =
ExportKeyBlob(safeBCryptKeyHandle, cngKeyBlobFormat);
272 val =
new TAlgorithm();
273 import(val, ecParams);
281 bool success =
false;
282 certContext.DangerousAddRef(ref success);
295 certContext.DangerousRelease();
304 global::Interop.BCrypt.NTSTATUS nTSTATUS = global::Interop.BCrypt.BCryptExportKey(bCryptKeyHandle,
IntPtr.
Zero,
format,
null, 0, out pcbResult, 0);
309 byte[]
array =
new byte[pcbResult];
310 nTSTATUS = global::Interop.BCrypt.BCryptExportKey(bCryptKeyHandle,
IntPtr.
Zero,
format,
array,
array.Length, out pcbResult, 0);
321 fixed (
byte* ptr = &ecBlob[0])
323 global::Interop.BCrypt.BCRYPT_ECCKEY_BLOB* ptr2 = (global::Interop.BCrypt.BCRYPT_ECCKEY_BLOB*)ptr;
324 int offset =
sizeof(global::Interop.BCrypt.BCRYPT_ECCKEY_BLOB);
327 X = global::Interop.BCrypt.Consume(ecBlob, ref
offset, ptr2->cbKey),
328 Y = global::Interop.BCrypt.Consume(ecBlob, ref
offset, ptr2->cbKey)
330 if (includePrivateParameters)
332 ecParams.D = global::Interop.BCrypt.Consume(ecBlob, ref
offset, ptr2->cbKey);
339 int pcbStructInfo = 0;
344 byte[]
array =
new byte[pcbStructInfo];
359 throw (-2146893803).ToCryptographicException();
361 int capacity = 16 + num + 20 + num + num + 24;
364 binaryWriter.
Write((
byte)6);
365 binaryWriter.
Write((
byte)2);
366 binaryWriter.
Write((
short)0);
367 binaryWriter.
Write(8704u);
368 binaryWriter.
Write(827544388);
369 binaryWriter.
Write(num * 8);
370 binaryWriter.
Write(p);
372 if (num2 == 0 || num2 > 20)
374 throw (-2146893803).ToCryptographicException();
376 binaryWriter.
Write(q);
379 binaryWriter.
Write(
new byte[20 - num2]);
382 if (num2 == 0 || num2 > num)
384 throw (-2146893803).ToCryptographicException();
386 binaryWriter.
Write(g);
389 binaryWriter.
Write(
new byte[num - num2]);
392 if (num2 == 0 || num2 > num)
394 throw (-2146893803).ToCryptographicException();
399 binaryWriter.
Write(
new byte[num - num2]);
401 binaryWriter.
Write(uint.MaxValue);
402 binaryWriter.
Write(
new byte[20]);
411 private unsafe
static void DecodeDssParameters(
byte[] encodedParameters, out
byte[] p, out
byte[] q, out
byte[] g)
418 byte[]
property =
GetProperty(bcryptHandle,
"ECCParameters");
419 if (property !=
null)
421 return property.Length != 0;
433 byte[]
property =
GetProperty(cryptHandle, propertyName);
434 if (property ==
null || property.Length == 0)
438 fixed (
byte* ptr = &property[0])
446 if (global::Interop.BCrypt.BCryptGetProperty(cryptHandle, propertyName,
null, 0, out var pcbResult, 0) != 0)
450 byte[]
array =
new byte[pcbResult];
451 global::Interop.BCrypt.NTSTATUS nTSTATUS;
452 fixed (
byte* pbOutput =
array)
454 nTSTATUS = global::Interop.BCrypt.BCryptGetProperty(cryptHandle, propertyName, pbOutput,
array.Length, out pcbResult, 0);
467 fixed (
byte* pbData = encodedDistinguishedName)
470 cRYPTOAPI_BLOB.cbData = encodedDistinguishedName.Length;
471 cRYPTOAPI_BLOB.pbData = pbData;
472 int num = global::Interop.Crypt32.CertNameToStr(65537, &cRYPTOAPI_BLOB, dwStrType,
null, 0);
475 throw (-2146762476).ToCryptographicException();
479 fixed (
char* psz = span2)
481 if (global::Interop.Crypt32.CertNameToStr(65537, &cRYPTOAPI_BLOB, dwStrType, psz, num) == 0)
483 throw (-2146762476).ToCryptographicException();
498 byte[]
array =
new byte[pcbEncoded];
508 if (encodedDistinguishedName ==
null || encodedDistinguishedName.Length == 0)
512 int dwFormatStrType = (multiLine ? 1 : 0);
514 if (!global::Interop.Crypt32.CryptFormatObject(1, 0, dwFormatStrType,
IntPtr.
Zero, (
byte*)7, encodedDistinguishedName, encodedDistinguishedName.Length,
null, ref pcbFormat))
516 return encodedDistinguishedName.ToHexStringUpper();
518 int num = (pcbFormat + 1) / 2;
521 fixed (
char* pbFormat = span2)
523 if (!global::Interop.Crypt32.CryptFormatObject(1, 0, dwFormatStrType,
IntPtr.
Zero, (
byte*)7, encodedDistinguishedName, encodedDistinguishedName.Length, pbFormat, ref pcbFormat))
525 return encodedDistinguishedName.ToHexStringUpper();
528 return new string(span2.
Slice(0, pcbFormat / 2 - 1));
574 return certNameStrTypeAndFlags;
SafeCertContextHandle CertContext
unsafe CERT_CONTEXT * CertContext
static byte[] ConstructDSSPublicKeyCspBlob(byte[] encodedKeyValue, byte[] encodedParameters)
static unsafe void ExportNamedCurveParameters(ref ECParameters ecParams, byte[] ecBlob, bool includePrivateParameters)
ECDiffieHellman DecodeECDiffieHellmanPublicKey(ICertificatePal certificatePal)
unsafe byte[] EncodeX509SubjectKeyIdentifierExtension(ReadOnlySpan< byte > subjectKeyIdentifier)
unsafe byte[] ComputeCapiSha1OfPublicKey(PublicKey key)
static unsafe void DecodeDssParameters(byte[] encodedParameters, out byte[] p, out byte[] q, out byte[] g)
ECDsa DecodeECDsaPublicKey(ICertificatePal certificatePal)
unsafe string X500DistinguishedNameDecode(byte[] encodedDistinguishedName, X500DistinguishedNameFlags flag)
unsafe void DecodeX509KeyUsageExtension(byte[] encoded, out X509KeyUsageFlags keyUsages)
unsafe void DecodeX509EnhancedKeyUsageExtension(byte[] encoded, out OidCollection usages)
static CertNameStrTypeAndFlags MapNameToStrFlag(X500DistinguishedNameFlags flag)
unsafe byte[] EncodeX509BasicConstraints2Extension(bool certificateAuthority, bool hasPathLengthConstraint, int pathLengthConstraint)
byte[] X500DistinguishedNameEncode(string distinguishedName, X500DistinguishedNameFlags flag)
unsafe void DecodeX509BasicConstraintsExtension(byte[] encoded, out bool certificateAuthority, out bool hasPathLengthConstraint, out int pathLengthConstraint)
bool SupportsLegacyBasicConstraintsExtension
static byte[] ExportKeyBlob(Microsoft.Win32.SafeHandles.SafeBCryptKeyHandle bCryptKeyHandle, CngKeyBlobFormat blobFormat)
unsafe byte[] EncodeX509KeyUsageExtension(X509KeyUsageFlags keyUsages)
static unsafe string GetPropertyAsString(Microsoft.Win32.SafeHandles.SafeBCryptKeyHandle cryptHandle, string propertyName)
static string GetCurveName(Microsoft.Win32.SafeHandles.SafeBCryptKeyHandle bcryptHandle)
static bool HasExplicitParameters(Microsoft.Win32.SafeHandles.SafeBCryptKeyHandle bcryptHandle)
unsafe byte[] EncodeX509EnhancedKeyUsageExtension(OidCollection usages)
AsymmetricAlgorithm DecodePublicKey(Oid oid, byte[] encodedKeyValue, byte[] encodedParameters, ICertificatePal certificatePal)
static TAlgorithm DecodeECPublicKey< TAlgorithm >(CertificatePal certificatePal, Func< CngKey, TAlgorithm > factory, Action< TAlgorithm, ECParameters > import, CryptImportPublicKeyInfoFlags importFlags=CryptImportPublicKeyInfoFlags.NONE)
unsafe void DecodeX509BasicConstraints2Extension(byte[] encoded, out bool certificateAuthority, out bool hasPathLengthConstraint, out int pathLengthConstraint)
unsafe void DecodeX509SubjectKeyIdentifierExtension(byte[] encoded, out byte[] subjectKeyIdentifier)
unsafe X509ContentType GetCertContentType(string fileName)
static unsafe byte[] DecodeDssKeyValue(byte[] encodedKeyValue)
static unsafe Microsoft.Win32.SafeHandles.SafeBCryptKeyHandle ImportPublicKeyInfo(SafeCertContextHandle certContext, CryptImportPublicKeyInfoFlags importFlags)
unsafe X509ContentType GetCertContentType(ReadOnlySpan< byte > rawData)
static byte[] DecodeKeyBlob(CryptDecodeObjectStructType lpszStructType, byte[] encodedKeyValue)
static X509ContentType MapContentType(ContentType contentType)
static unsafe byte[] GetProperty(Microsoft.Win32.SafeHandles.SafeBCryptKeyHandle cryptHandle, string propertyName)
unsafe string X500DistinguishedNameFormat(byte[] encodedDistinguishedName, bool multiLine)
static void BlockCopy(Array src, int srcOffset, Array dst, int dstOffset, int count)
virtual void Write(bool value)
static int GetHRForLastWin32Error()
static unsafe? string PtrToStringUni(IntPtr ptr)
static unsafe? string PtrToStringAnsi(IntPtr ptr)
static int GetLastWin32Error()
static string NotSupported_KeyAlgorithm
static CngKey Import(ReadOnlySpan< byte > keyBlob, CngKeyBlobFormat format)
void ImportCspBlob(byte[] keyBlob)
override void ImportParameters(ECParameters parameters)
override void ImportParameters(ECParameters parameters)
CryptImportPublicKeyInfoFlags
CryptDecodeObjectStructType
X500DistinguishedNameFlags
unsafe CERT_INFO * pCertInfo
CERT_PUBLIC_KEY_INFO SubjectPublicKeyInfo
static readonly IntPtr Zero
static ECCurve CreateFromFriendlyName(string oidFriendlyName)
Span< T > Slice(int start)