internal static unsafe IntPtr CreateCryptAttributes(CryptographicAttributeObjectCollection attributes) { if (attributes.Count == 0) { return(IntPtr.Zero); } uint num = 0; uint num2 = AlignedLength((uint)Marshal.SizeOf(typeof(I_CRYPT_ATTRIBUTE))); uint num3 = AlignedLength((uint)Marshal.SizeOf(typeof(System.Security.Cryptography.CAPI.CRYPTOAPI_BLOB))); CryptographicAttributeObjectEnumerator enumerator = attributes.GetEnumerator(); while (enumerator.MoveNext()) { CryptographicAttributeObject current = enumerator.Current; num += num2; num += AlignedLength((uint)(current.Oid.Value.Length + 1)); AsnEncodedDataEnumerator enumerator2 = current.Values.GetEnumerator(); while (enumerator2.MoveNext()) { AsnEncodedData data = enumerator2.Current; num += num3; num += AlignedLength((uint)data.RawData.Length); } } System.Security.Cryptography.SafeLocalAllocHandle handle = System.Security.Cryptography.CAPI.LocalAlloc(0x40, new IntPtr((long)num)); I_CRYPT_ATTRIBUTE *i_crypt_attributePtr = (I_CRYPT_ATTRIBUTE *)handle.DangerousGetHandle(); IntPtr ptr = new IntPtr(((long)handle.DangerousGetHandle()) + (num2 * attributes.Count)); CryptographicAttributeObjectEnumerator enumerator3 = attributes.GetEnumerator(); while (enumerator3.MoveNext()) { CryptographicAttributeObject obj3 = enumerator3.Current; byte * numPtr = (byte *)ptr; byte[] bytes = new byte[obj3.Oid.Value.Length + 1]; System.Security.Cryptography.CAPI.CRYPTOAPI_BLOB *cryptoapi_blobPtr = (System.Security.Cryptography.CAPI.CRYPTOAPI_BLOB *)(numPtr + AlignedLength((uint)bytes.Length)); i_crypt_attributePtr->pszObjId = (IntPtr)numPtr; i_crypt_attributePtr->cValue = (uint)obj3.Values.Count; i_crypt_attributePtr->rgValue = (IntPtr)cryptoapi_blobPtr; Encoding.ASCII.GetBytes(obj3.Oid.Value, 0, obj3.Oid.Value.Length, bytes, 0); Marshal.Copy(bytes, 0, i_crypt_attributePtr->pszObjId, bytes.Length); IntPtr destination = new IntPtr(((long)((ulong)cryptoapi_blobPtr)) + (obj3.Values.Count * num3)); AsnEncodedDataEnumerator enumerator4 = obj3.Values.GetEnumerator(); while (enumerator4.MoveNext()) { byte[] rawData = enumerator4.Current.RawData; if (rawData.Length > 0) { cryptoapi_blobPtr->cbData = (uint)rawData.Length; cryptoapi_blobPtr->pbData = destination; Marshal.Copy(rawData, 0, destination, rawData.Length); destination = new IntPtr(((long)destination) + AlignedLength((uint)rawData.Length)); } cryptoapi_blobPtr++; } i_crypt_attributePtr++; ptr = destination; } GC.SuppressFinalize(handle); return(handle.DangerousGetHandle()); }
private void CounterSign(CmsSigner signer) { CspParameters parameters = new CspParameters(); if (!System.Security.Cryptography.X509Certificates.X509Utils.GetPrivateKeyInfo(System.Security.Cryptography.X509Certificates.X509Utils.GetCertContext(signer.Certificate), ref parameters)) { throw new CryptographicException(Marshal.GetLastWin32Error()); } KeyContainerPermission permission = new KeyContainerPermission(KeyContainerPermissionFlags.NoFlags); KeyContainerPermissionAccessEntry accessEntry = new KeyContainerPermissionAccessEntry(parameters, KeyContainerPermissionFlags.Sign | KeyContainerPermissionFlags.Open); permission.AccessEntries.Add(accessEntry); permission.Demand(); uint dwIndex = (uint)PkcsUtils.GetSignerIndex(this.m_signedCms.GetCryptMsgHandle(), this, 0); System.Security.Cryptography.SafeLocalAllocHandle handle = System.Security.Cryptography.CAPI.LocalAlloc(0x40, new IntPtr(Marshal.SizeOf(typeof(System.Security.Cryptography.CAPI.CMSG_SIGNER_ENCODE_INFO)))); System.Security.Cryptography.CAPI.CMSG_SIGNER_ENCODE_INFO structure = PkcsUtils.CreateSignerEncodeInfo(signer); try { Marshal.StructureToPtr(structure, handle.DangerousGetHandle(), false); if (!System.Security.Cryptography.CAPI.CryptMsgCountersign(this.m_signedCms.GetCryptMsgHandle(), dwIndex, 1, handle.DangerousGetHandle())) { throw new CryptographicException(Marshal.GetLastWin32Error()); } this.m_signedCms.ReopenToDecode(); } finally { Marshal.DestroyStructure(handle.DangerousGetHandle(), typeof(System.Security.Cryptography.CAPI.CMSG_SIGNER_ENCODE_INFO)); handle.Dispose(); structure.Dispose(); } PkcsUtils.AddCertsToMessage(this.m_signedCms.GetCryptMsgHandle(), this.m_signedCms.Certificates, PkcsUtils.CreateBagOfCertificates(signer)); }
internal static unsafe X509Certificate2 CreateDummyCertificate(CspParameters parameters) { System.Security.Cryptography.SafeCertContextHandle invalidHandle = System.Security.Cryptography.SafeCertContextHandle.InvalidHandle; System.Security.Cryptography.SafeCryptProvHandle hCryptProv = System.Security.Cryptography.SafeCryptProvHandle.InvalidHandle; uint dwFlags = 0; if ((parameters.Flags & CspProviderFlags.UseMachineKeyStore) != CspProviderFlags.NoFlags) { dwFlags |= 0x20; } if ((parameters.Flags & CspProviderFlags.UseDefaultKeyContainer) != CspProviderFlags.NoFlags) { dwFlags |= 0xf0000000; } if ((parameters.Flags & CspProviderFlags.NoPrompt) != CspProviderFlags.NoFlags) { dwFlags |= 0x40; } if (!System.Security.Cryptography.CAPI.CryptAcquireContext(ref hCryptProv, parameters.KeyContainerName, parameters.ProviderName, (uint)parameters.ProviderType, dwFlags)) { throw new CryptographicException(Marshal.GetLastWin32Error()); } System.Security.Cryptography.CAPI.CRYPT_KEY_PROV_INFO structure = new System.Security.Cryptography.CAPI.CRYPT_KEY_PROV_INFO { pwszProvName = parameters.ProviderName, pwszContainerName = parameters.KeyContainerName, dwProvType = (uint)parameters.ProviderType, dwKeySpec = (uint)parameters.KeyNumber, dwFlags = ((parameters.Flags & CspProviderFlags.UseMachineKeyStore) == CspProviderFlags.UseMachineKeyStore) ? 0x20 : 0 }; System.Security.Cryptography.SafeLocalAllocHandle handle3 = System.Security.Cryptography.CAPI.LocalAlloc(0x40, new IntPtr(Marshal.SizeOf(typeof(System.Security.Cryptography.CAPI.CRYPT_KEY_PROV_INFO)))); Marshal.StructureToPtr(structure, handle3.DangerousGetHandle(), false); System.Security.Cryptography.CAPI.CRYPT_ALGORITHM_IDENTIFIER crypt_algorithm_identifier = new System.Security.Cryptography.CAPI.CRYPT_ALGORITHM_IDENTIFIER { pszObjId = "1.3.14.3.2.29" }; System.Security.Cryptography.SafeLocalAllocHandle handle4 = System.Security.Cryptography.CAPI.LocalAlloc(0x40, new IntPtr(Marshal.SizeOf(typeof(System.Security.Cryptography.CAPI.CRYPT_ALGORITHM_IDENTIFIER)))); Marshal.StructureToPtr(crypt_algorithm_identifier, handle4.DangerousGetHandle(), false); X500DistinguishedName name = new X500DistinguishedName("cn=CMS Signer Dummy Certificate"); fixed(byte *numRef = name.RawData) { System.Security.Cryptography.CAPI.CRYPTOAPI_BLOB cryptoapi_blob = new System.Security.Cryptography.CAPI.CRYPTOAPI_BLOB { cbData = (uint)name.RawData.Length, pbData = new IntPtr((void *)numRef) }; invalidHandle = System.Security.Cryptography.CAPI.CAPIUnsafe.CertCreateSelfSignCertificate(hCryptProv, new IntPtr((void *)&cryptoapi_blob), 1, handle3.DangerousGetHandle(), handle4.DangerousGetHandle(), IntPtr.Zero, IntPtr.Zero, IntPtr.Zero); } Marshal.DestroyStructure(handle3.DangerousGetHandle(), typeof(System.Security.Cryptography.CAPI.CRYPT_KEY_PROV_INFO)); handle3.Dispose(); Marshal.DestroyStructure(handle4.DangerousGetHandle(), typeof(System.Security.Cryptography.CAPI.CRYPT_ALGORITHM_IDENTIFIER)); handle4.Dispose(); if ((invalidHandle == null) || invalidHandle.IsInvalid) { throw new CryptographicException(Marshal.GetLastWin32Error()); } X509Certificate2 certificate = new X509Certificate2(invalidHandle.DangerousGetHandle()); invalidHandle.Dispose(); return(certificate); }
internal static System.Security.Cryptography.SafeLocalAllocHandle CopyOidsToUnmanagedMemory(OidCollection oids) { System.Security.Cryptography.SafeLocalAllocHandle invalidHandle = System.Security.Cryptography.SafeLocalAllocHandle.InvalidHandle; if ((oids != null) && (oids.Count != 0)) { int num = oids.Count * Marshal.SizeOf(typeof(IntPtr)); int num2 = 0; OidEnumerator enumerator = oids.GetEnumerator(); while (enumerator.MoveNext()) { Oid current = enumerator.Current; num2 += current.Value.Length + 1; } invalidHandle = System.Security.Cryptography.CAPI.LocalAlloc(0x40, new IntPtr((long)((ulong)(num + num2)))); IntPtr val = new IntPtr(((long)invalidHandle.DangerousGetHandle()) + num); for (int i = 0; i < oids.Count; i++) { Marshal.WriteIntPtr(new IntPtr(((long)invalidHandle.DangerousGetHandle()) + (i * Marshal.SizeOf(typeof(IntPtr)))), val); byte[] bytes = Encoding.ASCII.GetBytes(oids[i].Value); Marshal.Copy(bytes, 0, val, bytes.Length); val = new IntPtr((((long)val) + oids[i].Value.Length) + 1L); } } return(invalidHandle); }
private unsafe void Sign(CmsSigner signer, bool silent) { System.Security.Cryptography.SafeCryptMsgHandle hCryptMsg = null; System.Security.Cryptography.CAPI.CMSG_SIGNED_ENCODE_INFO cmsg_signed_encode_info = new System.Security.Cryptography.CAPI.CMSG_SIGNED_ENCODE_INFO(Marshal.SizeOf(typeof(System.Security.Cryptography.CAPI.CMSG_SIGNED_ENCODE_INFO))); System.Security.Cryptography.CAPI.CMSG_SIGNER_ENCODE_INFO structure = PkcsUtils.CreateSignerEncodeInfo(signer, silent); byte[] encodedMessage = null; try { System.Security.Cryptography.SafeLocalAllocHandle handle2 = System.Security.Cryptography.CAPI.LocalAlloc(0, new IntPtr(Marshal.SizeOf(typeof(System.Security.Cryptography.CAPI.CMSG_SIGNER_ENCODE_INFO)))); try { Marshal.StructureToPtr(structure, handle2.DangerousGetHandle(), false); X509Certificate2Collection certificates = PkcsUtils.CreateBagOfCertificates(signer); System.Security.Cryptography.SafeLocalAllocHandle handle3 = PkcsUtils.CreateEncodedCertBlob(certificates); cmsg_signed_encode_info.cSigners = 1; cmsg_signed_encode_info.rgSigners = handle2.DangerousGetHandle(); cmsg_signed_encode_info.cCertEncoded = (uint)certificates.Count; if (certificates.Count > 0) { cmsg_signed_encode_info.rgCertEncoded = handle3.DangerousGetHandle(); } if (string.Compare(this.ContentInfo.ContentType.Value, "1.2.840.113549.1.7.1", StringComparison.OrdinalIgnoreCase) == 0) { hCryptMsg = System.Security.Cryptography.CAPI.CryptMsgOpenToEncode(0x10001, this.Detached ? 4 : 0, 2, new IntPtr((void *)&cmsg_signed_encode_info), IntPtr.Zero, IntPtr.Zero); } else { hCryptMsg = System.Security.Cryptography.CAPI.CryptMsgOpenToEncode(0x10001, this.Detached ? 4 : 0, 2, new IntPtr((void *)&cmsg_signed_encode_info), this.ContentInfo.ContentType.Value, IntPtr.Zero); } if ((hCryptMsg == null) || hCryptMsg.IsInvalid) { throw new CryptographicException(Marshal.GetLastWin32Error()); } if ((this.ContentInfo.Content.Length > 0) && !System.Security.Cryptography.CAPI.CAPISafe.CryptMsgUpdate(hCryptMsg, this.ContentInfo.pContent, (uint)this.ContentInfo.Content.Length, true)) { throw new CryptographicException(Marshal.GetLastWin32Error()); } encodedMessage = PkcsUtils.GetContent(hCryptMsg); hCryptMsg.Dispose(); handle3.Dispose(); } finally { Marshal.DestroyStructure(handle2.DangerousGetHandle(), typeof(System.Security.Cryptography.CAPI.CMSG_SIGNER_ENCODE_INFO)); handle2.Dispose(); } } finally { structure.Dispose(); } hCryptMsg = OpenToDecode(encodedMessage, this.ContentInfo, this.Detached); if ((this.m_safeCryptMsgHandle != null) && !this.m_safeCryptMsgHandle.IsInvalid) { this.m_safeCryptMsgHandle.Dispose(); } this.m_safeCryptMsgHandle = hCryptMsg; GC.KeepAlive(signer); }
internal AlgorithmIdentifier(System.Security.Cryptography.CAPI.CERT_PUBLIC_KEY_INFO keyInfo) { System.Security.Cryptography.SafeLocalAllocHandle handle = System.Security.Cryptography.CAPI.LocalAlloc(0x40, new IntPtr(Marshal.SizeOf(typeof(System.Security.Cryptography.CAPI.CERT_PUBLIC_KEY_INFO)))); Marshal.StructureToPtr(keyInfo, handle.DangerousGetHandle(), false); int keyLength = (int)System.Security.Cryptography.CAPI.CAPISafe.CertGetPublicKeyLength(0x10001, handle.DangerousGetHandle()); byte[] destination = new byte[keyInfo.Algorithm.Parameters.cbData]; if (destination.Length > 0) { Marshal.Copy(keyInfo.Algorithm.Parameters.pbData, destination, 0, destination.Length); } Marshal.DestroyStructure(handle.DangerousGetHandle(), typeof(System.Security.Cryptography.CAPI.CERT_PUBLIC_KEY_INFO)); handle.Dispose(); this.Reset(new System.Security.Cryptography.Oid(keyInfo.Algorithm.pszObjId), keyLength, destination); }
internal static SafeLocalAllocHandle ByteToPtr(byte[] managed) { SafeLocalAllocHandle pb = CAPI.LocalAlloc(CAPI.LMEM_FIXED, new IntPtr(managed.Length)); Marshal.Copy(managed, 0, pb.DangerousGetHandle(), managed.Length); return(pb); }
internal static System.Security.Cryptography.SafeLocalAllocHandle StringToAnsiPtr(string s) { byte[] bytes = new byte[s.Length + 1]; Encoding.ASCII.GetBytes(s, 0, s.Length, bytes, 0); System.Security.Cryptography.SafeLocalAllocHandle handle = System.Security.Cryptography.CAPI.LocalAlloc(0, new IntPtr(bytes.Length)); Marshal.Copy(bytes, 0, handle.DangerousGetHandle(), bytes.Length); return(handle); }
internal static SafeLocalAllocHandle StringToUniPtr(string s) { byte[] arr = new byte[2 * (s.Length + 1)]; Encoding.Unicode.GetBytes(s, 0, s.Length, arr, 0); SafeLocalAllocHandle pb = CAPI.LocalAlloc(CAPI.LMEM_FIXED, new IntPtr(arr.Length)); Marshal.Copy(arr, 0, pb.DangerousGetHandle(), arr.Length); return(pb); }
internal static SafeLocalAllocHandle StringToAnsiPtr(string s) { byte[] arr = new byte[s.Length + 1]; Encoding.ASCII.GetBytes(s, 0, s.Length, arr, 0); SafeLocalAllocHandle pb = CapiNative.LocalAlloc(CapiNative.LMEM_FIXED, new IntPtr(arr.Length)); Marshal.Copy(arr, 0, pb.DangerousGetHandle(), arr.Length); return(pb); }
internal SignerInfo (SignedCms signedCms, SafeLocalAllocHandle pbCmsgSignerInfo) { // Sanity check. Debug.Assert(signedCms != null && pbCmsgSignerInfo != null && !pbCmsgSignerInfo.IsInvalid); m_signedCms = signedCms; m_parentSignerInfo = null; m_encodedSignerInfo = null; m_pbCmsgSignerInfo = pbCmsgSignerInfo; m_cmsgSignerInfo = (CAPI.CMSG_SIGNER_INFO) Marshal.PtrToStructure(pbCmsgSignerInfo.DangerousGetHandle(), typeof(CAPI.CMSG_SIGNER_INFO)); }
internal static SafeLocalAllocHandle CopyOidsToUnmanagedMemory(OidCollection oids) { SafeLocalAllocHandle safeLocalAllocHandle = SafeLocalAllocHandle.InvalidHandle; if (oids == null || oids.Count == 0) { return(safeLocalAllocHandle); } // Copy the oid strings to a local list to prevent a security race condition where // the OidCollection or individual oids can be modified by another thread and // potentially cause a buffer overflow List <string> oidStrs = new List <string>(); foreach (Oid oid in oids) { oidStrs.Add(oid.Value); } IntPtr pOid = IntPtr.Zero; // Needs to be checked to avoid having large sets of oids overflow the sizes and allow // a potential buffer overflow checked { int ptrSize = oidStrs.Count * Marshal.SizeOf(typeof(IntPtr)); int oidSize = 0; foreach (string oidStr in oidStrs) { oidSize += (oidStr.Length + 1); } safeLocalAllocHandle = CAPI.LocalAlloc(CAPI.LPTR, new IntPtr((uint)ptrSize + (uint)oidSize)); pOid = new IntPtr((long)safeLocalAllocHandle.DangerousGetHandle() + ptrSize); } for (int index = 0; index < oidStrs.Count; index++) { Marshal.WriteIntPtr(new IntPtr((long)safeLocalAllocHandle.DangerousGetHandle() + index * Marshal.SizeOf(typeof(IntPtr))), pOid); byte[] ansiOid = Encoding.ASCII.GetBytes(oidStrs[index]); Marshal.Copy(ansiOid, 0, pOid, ansiOid.Length); pOid = new IntPtr((long)pOid + oidStrs[index].Length + 1); } return(safeLocalAllocHandle); }
private void CoSign(CmsSigner signer, bool silent) { using (System.Security.Cryptography.CAPI.CMSG_SIGNER_ENCODE_INFO cmsg_signer_encode_info = PkcsUtils.CreateSignerEncodeInfo(signer, silent)) { System.Security.Cryptography.SafeLocalAllocHandle handle = System.Security.Cryptography.CAPI.LocalAlloc(0x40, new IntPtr(Marshal.SizeOf(typeof(System.Security.Cryptography.CAPI.CMSG_SIGNER_ENCODE_INFO)))); try { Marshal.StructureToPtr(cmsg_signer_encode_info, handle.DangerousGetHandle(), false); if (!System.Security.Cryptography.CAPI.CryptMsgControl(this.m_safeCryptMsgHandle, 0, 6, handle.DangerousGetHandle())) { throw new CryptographicException(Marshal.GetLastWin32Error()); } } finally { Marshal.DestroyStructure(handle.DangerousGetHandle(), typeof(System.Security.Cryptography.CAPI.CMSG_SIGNER_ENCODE_INFO)); handle.Dispose(); } } PkcsUtils.AddCertsToMessage(this.m_safeCryptMsgHandle, this.Certificates, PkcsUtils.CreateBagOfCertificates(signer)); }
private static byte[] Encode(DateTime signingTime) { long val = signingTime.ToFileTimeUtc(); System.Security.Cryptography.SafeLocalAllocHandle handle = System.Security.Cryptography.CAPI.LocalAlloc(0x40, new IntPtr(Marshal.SizeOf(typeof(long)))); Marshal.WriteInt64(handle.DangerousGetHandle(), val); byte[] encodedData = new byte[0]; if (!System.Security.Cryptography.CAPI.EncodeObject("1.2.840.113549.1.9.5", handle.DangerousGetHandle(), out encodedData)) { throw new CryptographicException(Marshal.GetLastWin32Error()); } handle.Dispose(); return(encodedData); }
private void Decode() { uint cbDecodedValue = 0; System.Security.Cryptography.SafeLocalAllocHandle decodedValue = null; if (!System.Security.Cryptography.CAPI.DecodeObject(new IntPtr(0x11L), base.RawData, out decodedValue, out cbDecodedValue)) { throw new CryptographicException(Marshal.GetLastWin32Error()); } long fileTime = Marshal.ReadInt64(decodedValue.DangerousGetHandle()); decodedValue.Dispose(); this.m_signingTime = DateTime.FromFileTimeUtc(fileTime); this.m_decoded = true; }
internal static byte[] DecodeOctetBytes(byte[] encodedOctetString) { uint cbDecodedValue = 0; System.Security.Cryptography.SafeLocalAllocHandle decodedValue = null; if (!System.Security.Cryptography.CAPI.DecodeObject(new IntPtr(0x19L), encodedOctetString, out decodedValue, out cbDecodedValue)) { throw new CryptographicException(Marshal.GetLastWin32Error()); } if (cbDecodedValue == 0) { return(new byte[0]); } using (decodedValue) { return(System.Security.Cryptography.CAPI.BlobToByteArray(decodedValue.DangerousGetHandle())); } }
internal static unsafe int BuildChain(IntPtr hChainEngine, System.Security.Cryptography.SafeCertContextHandle pCertContext, X509Certificate2Collection extraStore, OidCollection applicationPolicy, OidCollection certificatePolicy, X509RevocationMode revocationMode, X509RevocationFlag revocationFlag, DateTime verificationTime, TimeSpan timeout, ref System.Security.Cryptography.SafeCertChainHandle ppChainContext) { System.Security.Cryptography.CAPI.CERT_CHAIN_PARA cert_chain_para; if ((pCertContext == null) || pCertContext.IsInvalid) { throw new ArgumentException(SecurityResources.GetResourceString("Cryptography_InvalidContextHandle"), "pCertContext"); } System.Security.Cryptography.SafeCertStoreHandle invalidHandle = System.Security.Cryptography.SafeCertStoreHandle.InvalidHandle; if ((extraStore != null) && (extraStore.Count > 0)) { invalidHandle = ExportToMemoryStore(extraStore); } cert_chain_para = new System.Security.Cryptography.CAPI.CERT_CHAIN_PARA { cbSize = (uint)Marshal.SizeOf(cert_chain_para) }; System.Security.Cryptography.SafeLocalAllocHandle handle2 = System.Security.Cryptography.SafeLocalAllocHandle.InvalidHandle; if ((applicationPolicy != null) && (applicationPolicy.Count > 0)) { cert_chain_para.RequestedUsage.dwType = 0; cert_chain_para.RequestedUsage.Usage.cUsageIdentifier = (uint)applicationPolicy.Count; handle2 = CopyOidsToUnmanagedMemory(applicationPolicy); cert_chain_para.RequestedUsage.Usage.rgpszUsageIdentifier = handle2.DangerousGetHandle(); } System.Security.Cryptography.SafeLocalAllocHandle handle3 = System.Security.Cryptography.SafeLocalAllocHandle.InvalidHandle; if ((certificatePolicy != null) && (certificatePolicy.Count > 0)) { cert_chain_para.RequestedIssuancePolicy.dwType = 0; cert_chain_para.RequestedIssuancePolicy.Usage.cUsageIdentifier = (uint)certificatePolicy.Count; handle3 = CopyOidsToUnmanagedMemory(certificatePolicy); cert_chain_para.RequestedIssuancePolicy.Usage.rgpszUsageIdentifier = handle3.DangerousGetHandle(); } cert_chain_para.dwUrlRetrievalTimeout = (uint)timeout.Milliseconds; System.Runtime.InteropServices.ComTypes.FILETIME pTime = new System.Runtime.InteropServices.ComTypes.FILETIME(); *((long *)&pTime) = verificationTime.ToFileTime(); uint dwFlags = MapRevocationFlags(revocationMode, revocationFlag); if (!System.Security.Cryptography.CAPI.CAPISafe.CertGetCertificateChain(hChainEngine, pCertContext, ref pTime, invalidHandle, ref cert_chain_para, dwFlags, IntPtr.Zero, ref ppChainContext)) { return(Marshal.GetHRForLastWin32Error()); } handle2.Dispose(); handle3.Dispose(); return(0); }
internal static unsafe bool EncodeObject(string lpszStructType, IntPtr pvStructInfo, out byte[] encodedData) { encodedData = new byte[0]; uint num = 0; SafeLocalAllocHandle invalidHandle = SafeLocalAllocHandle.InvalidHandle; if (!CAPISafe.CryptEncodeObject(0x10001, lpszStructType, pvStructInfo, invalidHandle, new IntPtr((void *)&num))) { return(false); } invalidHandle = LocalAlloc(0, new IntPtr((long)num)); if (!CAPISafe.CryptEncodeObject(0x10001, lpszStructType, pvStructInfo, invalidHandle, new IntPtr((void *)&num))) { return(false); } encodedData = new byte[num]; Marshal.Copy(invalidHandle.DangerousGetHandle(), encodedData, 0, (int)num); invalidHandle.Dispose(); return(true); }
internal static unsafe string CryptFormatObject([In] uint dwCertEncodingType, [In] uint dwFormatStrType, [In] string lpszStructType, [In] byte[] rawData) { if (rawData == null) { throw new ArgumentNullException("rawData"); } uint num = 0; SafeLocalAllocHandle invalidHandle = SafeLocalAllocHandle.InvalidHandle; if (!CAPISafe.CryptFormatObject(dwCertEncodingType, 0, dwFormatStrType, IntPtr.Zero, lpszStructType, rawData, (uint)rawData.Length, invalidHandle, new IntPtr((void *)&num))) { return(System.Security.Cryptography.X509Certificates.X509Utils.EncodeHexString(rawData)); } invalidHandle = LocalAlloc(0, new IntPtr((long)num)); if (!CAPISafe.CryptFormatObject(dwCertEncodingType, 0, dwFormatStrType, IntPtr.Zero, lpszStructType, rawData, (uint)rawData.Length, invalidHandle, new IntPtr((void *)&num))) { return(System.Security.Cryptography.X509Certificates.X509Utils.EncodeHexString(rawData)); } string str = Marshal.PtrToStringUni(invalidHandle.DangerousGetHandle()); invalidHandle.Dispose(); return(str); }
internal static unsafe X509IssuerSerial DecodeIssuerSerial(System.Security.Cryptography.CAPI.CERT_ISSUER_SERIAL_NUMBER pIssuerAndSerial) { System.Security.Cryptography.SafeLocalAllocHandle invalidHandle = System.Security.Cryptography.SafeLocalAllocHandle.InvalidHandle; uint csz = System.Security.Cryptography.CAPI.CAPISafe.CertNameToStrW(0x10001, new IntPtr((void *)&pIssuerAndSerial.Issuer), 0x2000003, invalidHandle, 0); if (csz <= 1) { throw new CryptographicException(Marshal.GetLastWin32Error()); } invalidHandle = System.Security.Cryptography.CAPI.LocalAlloc(0, new IntPtr((long)(2 * csz))); if (System.Security.Cryptography.CAPI.CAPISafe.CertNameToStrW(0x10001, new IntPtr((void *)&pIssuerAndSerial.Issuer), 0x2000003, invalidHandle, csz) <= 1) { throw new CryptographicException(Marshal.GetLastWin32Error()); } X509IssuerSerial serial = new X509IssuerSerial { IssuerName = Marshal.PtrToStringUni(invalidHandle.DangerousGetHandle()) }; byte[] destination = new byte[pIssuerAndSerial.SerialNumber.cbData]; Marshal.Copy(pIssuerAndSerial.SerialNumber.pbData, destination, 0, destination.Length); serial.SerialNumber = System.Security.Cryptography.X509Certificates.X509Utils.EncodeHexStringFromInt(destination); invalidHandle.Dispose(); return(serial); }
internal static unsafe string GetCertNameInfo([In] System.Security.Cryptography.SafeCertContextHandle safeCertContext, [In] uint dwFlags, [In] uint dwDisplayType) { if (safeCertContext == null) { throw new ArgumentNullException("pCertContext"); } if (safeCertContext.IsInvalid) { throw new CryptographicException(SR.GetString("Cryptography_InvalidHandle"), "safeCertContext"); } uint num = 0x2000003; SafeLocalAllocHandle invalidHandle = SafeLocalAllocHandle.InvalidHandle; if (dwDisplayType == 3) { invalidHandle = System.Security.Cryptography.X509Certificates.X509Utils.StringToAnsiPtr("2.5.4.3"); } uint cchNameString = 0; SafeLocalAllocHandle pszNameString = SafeLocalAllocHandle.InvalidHandle; cchNameString = CAPISafe.CertGetNameStringW(safeCertContext, dwDisplayType, dwFlags, (dwDisplayType == 3) ? invalidHandle.DangerousGetHandle() : new IntPtr((void *)&num), pszNameString, 0); if (cchNameString == 0) { throw new CryptographicException(Marshal.GetLastWin32Error()); } pszNameString = LocalAlloc(0, new IntPtr((long)(2 * cchNameString))); if (CAPISafe.CertGetNameStringW(safeCertContext, dwDisplayType, dwFlags, (dwDisplayType == 3) ? invalidHandle.DangerousGetHandle() : new IntPtr((void *)&num), pszNameString, cchNameString) == 0) { throw new CryptographicException(Marshal.GetLastWin32Error()); } string str = Marshal.PtrToStringUni(pszNameString.DangerousGetHandle()); pszNameString.Dispose(); invalidHandle.Dispose(); return(str); }
internal unsafe SignerInfo(SignedCms signedCms, SignerInfo parentSignerInfo, byte[] encodedSignerInfo) { uint cbDecodedValue = 0; System.Security.Cryptography.SafeLocalAllocHandle invalidHandle = System.Security.Cryptography.SafeLocalAllocHandle.InvalidHandle; fixed(byte *numRef = encodedSignerInfo) { if (!System.Security.Cryptography.CAPI.DecodeObject(new IntPtr(500L), new IntPtr((void *)numRef), (uint)encodedSignerInfo.Length, out invalidHandle, out cbDecodedValue)) { throw new CryptographicException(Marshal.GetLastWin32Error()); } } this.m_signedCms = signedCms; this.m_parentSignerInfo = parentSignerInfo; this.m_encodedSignerInfo = (byte[])encodedSignerInfo.Clone(); this.m_pbCmsgSignerInfo = invalidHandle; this.m_cmsgSignerInfo = (System.Security.Cryptography.CAPI.CMSG_SIGNER_INFO)Marshal.PtrToStructure(invalidHandle.DangerousGetHandle(), typeof(System.Security.Cryptography.CAPI.CMSG_SIGNER_INFO)); }
internal CryptographicAttributeObjectCollection (SafeLocalAllocHandle pCryptAttributes) : this(pCryptAttributes.DangerousGetHandle()) {}
internal CryptographicAttributeObjectCollection(SafeLocalAllocHandle pCryptAttributes) : this(pCryptAttributes.DangerousGetHandle()) { }
private static byte[] ConstructDSSPubKeyCspBlob(SafeLocalAllocHandle decodedKeyValue, SafeLocalAllocHandle decodedParameters) { CAPIBase.CRYPTOAPI_BLOB cryptoapi_blob = (CAPIBase.CRYPTOAPI_BLOB) Marshal.PtrToStructure(decodedKeyValue.DangerousGetHandle(), typeof(CAPIBase.CRYPTOAPI_BLOB)); CAPIBase.CERT_DSS_PARAMETERS cert_dss_parameters = (CAPIBase.CERT_DSS_PARAMETERS) Marshal.PtrToStructure(decodedParameters.DangerousGetHandle(), typeof(CAPIBase.CERT_DSS_PARAMETERS)); uint cbData = cert_dss_parameters.p.cbData; if (cbData == 0) { throw new CryptographicException(-2146893803); } uint num2 = ((((0x10 + cbData) + 20) + cbData) + cbData) + 0x18; MemoryStream output = new MemoryStream((int) num2); BinaryWriter writer = new BinaryWriter(output); writer.Write((byte) 6); writer.Write((byte) 2); writer.Write((short) 0); writer.Write((uint) 0x2200); writer.Write((uint) 0x31535344); writer.Write((uint) (cbData * 8)); byte[] destination = new byte[cert_dss_parameters.p.cbData]; Marshal.Copy(cert_dss_parameters.p.pbData, destination, 0, destination.Length); writer.Write(destination); uint num3 = cert_dss_parameters.q.cbData; if ((num3 == 0) || (num3 > 20)) { throw new CryptographicException(-2146893803); } byte[] buffer2 = new byte[cert_dss_parameters.q.cbData]; Marshal.Copy(cert_dss_parameters.q.pbData, buffer2, 0, buffer2.Length); writer.Write(buffer2); if (20 > num3) { writer.Write(new byte[20 - num3]); } num3 = cert_dss_parameters.g.cbData; if ((num3 == 0) || (num3 > cbData)) { throw new CryptographicException(-2146893803); } byte[] buffer3 = new byte[cert_dss_parameters.g.cbData]; Marshal.Copy(cert_dss_parameters.g.pbData, buffer3, 0, buffer3.Length); writer.Write(buffer3); if (cbData > num3) { writer.Write(new byte[cbData - num3]); } num3 = cryptoapi_blob.cbData; if ((num3 == 0) || (num3 > cbData)) { throw new CryptographicException(-2146893803); } byte[] buffer4 = new byte[cryptoapi_blob.cbData]; Marshal.Copy(cryptoapi_blob.pbData, buffer4, 0, buffer4.Length); writer.Write(buffer4); if (cbData > num3) { writer.Write(new byte[cbData - num3]); } writer.Write(uint.MaxValue); writer.Write(new byte[20]); return output.ToArray(); }
internal unsafe SubjectIdentifier(System.Security.Cryptography.CAPI.CRYPTOAPI_BLOB issuer, System.Security.Cryptography.CAPI.CRYPTOAPI_BLOB serialNumber) { System.Security.Cryptography.CAPI.CERT_ISSUER_SERIAL_NUMBER cert_issuer_serial_number; bool flag = true; byte *pbData = (byte *)serialNumber.pbData; for (uint i = 0; i < serialNumber.cbData; i++) { pbData++; if (pbData[0] != 0) { flag = false; break; } } if (flag) { byte[] destination = new byte[issuer.cbData]; Marshal.Copy(issuer.pbData, destination, 0, destination.Length); X500DistinguishedName name = new X500DistinguishedName(destination); if (string.Compare("CN=Dummy Signer", name.Name, StringComparison.OrdinalIgnoreCase) == 0) { this.Reset(SubjectIdentifierType.NoSignature, null); return; } } if (flag) { this.m_type = SubjectIdentifierType.SubjectKeyIdentifier; this.m_value = string.Empty; uint cbDecodedValue = 0; System.Security.Cryptography.SafeLocalAllocHandle invalidHandle = System.Security.Cryptography.SafeLocalAllocHandle.InvalidHandle; if (!System.Security.Cryptography.CAPI.DecodeObject(new IntPtr(7L), issuer.pbData, issuer.cbData, out invalidHandle, out cbDecodedValue)) { throw new CryptographicException(Marshal.GetLastWin32Error()); } using (invalidHandle) { System.Security.Cryptography.CAPI.CERT_NAME_INFO cert_name_info = (System.Security.Cryptography.CAPI.CERT_NAME_INFO)Marshal.PtrToStructure(invalidHandle.DangerousGetHandle(), typeof(System.Security.Cryptography.CAPI.CERT_NAME_INFO)); for (uint j = 0; j < cert_name_info.cRDN; j++) { System.Security.Cryptography.CAPI.CERT_RDN cert_rdn = (System.Security.Cryptography.CAPI.CERT_RDN)Marshal.PtrToStructure(new IntPtr(((long)cert_name_info.rgRDN) + (j * Marshal.SizeOf(typeof(System.Security.Cryptography.CAPI.CERT_RDN)))), typeof(System.Security.Cryptography.CAPI.CERT_RDN)); for (uint k = 0; k < cert_rdn.cRDNAttr; k++) { System.Security.Cryptography.CAPI.CERT_RDN_ATTR cert_rdn_attr = (System.Security.Cryptography.CAPI.CERT_RDN_ATTR)Marshal.PtrToStructure(new IntPtr(((long)cert_rdn.rgRDNAttr) + (k * Marshal.SizeOf(typeof(System.Security.Cryptography.CAPI.CERT_RDN_ATTR)))), typeof(System.Security.Cryptography.CAPI.CERT_RDN_ATTR)); if ((string.Compare("1.3.6.1.4.1.311.10.7.1", cert_rdn_attr.pszObjId, StringComparison.OrdinalIgnoreCase) == 0) && (cert_rdn_attr.dwValueType == 2)) { byte[] buffer2 = new byte[cert_rdn_attr.Value.cbData]; Marshal.Copy(cert_rdn_attr.Value.pbData, buffer2, 0, buffer2.Length); this.Reset(SubjectIdentifierType.SubjectKeyIdentifier, System.Security.Cryptography.X509Certificates.X509Utils.EncodeHexString(buffer2)); return; } } } } throw new CryptographicException(-2146889715); } cert_issuer_serial_number.Issuer = issuer; cert_issuer_serial_number.SerialNumber = serialNumber; X509IssuerSerial serial = PkcsUtils.DecodeIssuerSerial(cert_issuer_serial_number); this.Reset(SubjectIdentifierType.IssuerAndSerialNumber, serial); }
private static byte[] ConstructDSSPubKeyCspBlob (SafeLocalAllocHandle decodedKeyValue, SafeLocalAllocHandle decodedParameters) { // The CAPI DSS public key representation consists of the following sequence: // - PUBLICKEYSTRUC // - DSSPUBKEY // - rgbP[cbKey] // - rgbQ[20] // - rgbG[cbKey] // - rgbY[cbKey] // - DSSSEED CAPI.CRYPTOAPI_BLOB pDssPubKey = (CAPI.CRYPTOAPI_BLOB) Marshal.PtrToStructure(decodedKeyValue.DangerousGetHandle(), typeof(CAPI.CRYPTOAPI_BLOB)); CAPI.CERT_DSS_PARAMETERS pDssParameters = (CAPI.CERT_DSS_PARAMETERS) Marshal.PtrToStructure(decodedParameters.DangerousGetHandle(), typeof(CAPI.CERT_DSS_PARAMETERS)); uint cbKey = pDssParameters.p.cbData; if (cbKey == 0) throw new CryptographicException(CAPI.NTE_BAD_PUBLIC_KEY); const uint DSS_Q_LEN = 20; uint cbKeyBlob = 8 /* sizeof(CAPI.BLOBHEADER) */ + 8 /* sizeof(CAPI.DSSPUBKEY) */ + cbKey + DSS_Q_LEN + cbKey + cbKey + 24 /* sizeof(CAPI.DSSSEED) */; MemoryStream keyBlob = new MemoryStream((int) cbKeyBlob); BinaryWriter bw = new BinaryWriter(keyBlob); // PUBLICKEYSTRUC bw.Write(CAPI.PUBLICKEYBLOB); // pPubKeyStruc->bType = PUBLICKEYBLOB bw.Write(CAPI.CUR_BLOB_VERSION); // pPubKeyStruc->bVersion = CUR_BLOB_VERSION bw.Write((short) 0); // pPubKeyStruc->reserved = 0; bw.Write(CAPI.CALG_DSS_SIGN); // pPubKeyStruc->aiKeyAlg = CALG_DSS_SIGN; // DSSPUBKEY bw.Write(CAPI.DSS_MAGIC); // pCspPubKey->magic = DSS_MAGIC; We are constructing a DSS1 Csp blob. bw.Write(cbKey * 8); // pCspPubKey->bitlen = cbKey * 8; // rgbP[cbKey] byte[] p = new byte[pDssParameters.p.cbData]; Marshal.Copy(pDssParameters.p.pbData, p, 0, p.Length); bw.Write(p); // rgbQ[20] uint cb = pDssParameters.q.cbData; if (cb == 0 || cb > DSS_Q_LEN) throw new CryptographicException(CAPI.NTE_BAD_PUBLIC_KEY); byte[] q = new byte[pDssParameters.q.cbData]; Marshal.Copy(pDssParameters.q.pbData, q, 0, q.Length); bw.Write(q); if (DSS_Q_LEN > cb) bw.Write(new byte[DSS_Q_LEN - cb]); // rgbG[cbKey] cb = pDssParameters.g.cbData; if (cb == 0 || cb > cbKey) throw new CryptographicException(CAPI.NTE_BAD_PUBLIC_KEY); byte[] g = new byte[pDssParameters.g.cbData]; Marshal.Copy(pDssParameters.g.pbData, g, 0, g.Length); bw.Write(g); if (cbKey > cb) bw.Write(new byte[cbKey - cb]); // rgbY[cbKey] cb = pDssPubKey.cbData; if (cb == 0 || cb > cbKey) throw new CryptographicException(CAPI.NTE_BAD_PUBLIC_KEY); byte[] y = new byte[pDssPubKey.cbData]; Marshal.Copy(pDssPubKey.pbData, y, 0, y.Length); bw.Write(y); if (cbKey > cb) bw.Write(new byte[cbKey - cb]); // DSSSEED: set counter to 0xFFFFFFFF to indicate not available bw.Write(0xFFFFFFFF); bw.Write(new byte[20]); return keyBlob.ToArray(); }
internal static unsafe System.Security.Cryptography.SafeLocalAllocHandle CreateEncodedCertBlob(X509Certificate2Collection certificates) { System.Security.Cryptography.SafeLocalAllocHandle invalidHandle = System.Security.Cryptography.SafeLocalAllocHandle.InvalidHandle; if (certificates.Count > 0) { invalidHandle = System.Security.Cryptography.CAPI.LocalAlloc(0, new IntPtr(certificates.Count * Marshal.SizeOf(typeof(System.Security.Cryptography.CAPI.CRYPTOAPI_BLOB)))); System.Security.Cryptography.CAPI.CRYPTOAPI_BLOB *handle = (System.Security.Cryptography.CAPI.CRYPTOAPI_BLOB *)invalidHandle.DangerousGetHandle(); X509Certificate2Enumerator enumerator = certificates.GetEnumerator(); while (enumerator.MoveNext()) { System.Security.Cryptography.CAPI.CERT_CONTEXT cert_context = *((System.Security.Cryptography.CAPI.CERT_CONTEXT *)System.Security.Cryptography.X509Certificates.X509Utils.GetCertContext(enumerator.Current).DangerousGetHandle()); handle->cbData = cert_context.cbCertEncoded; handle->pbData = cert_context.pbCertEncoded; handle++; } } return(invalidHandle); }
internal AlgorithmIdentifier(System.Security.Cryptography.CAPI.CRYPT_ALGORITHM_IDENTIFIER algorithmIdentifier) { int keyLength = 0; uint cbDecodedValue = 0; System.Security.Cryptography.SafeLocalAllocHandle invalidHandle = System.Security.Cryptography.SafeLocalAllocHandle.InvalidHandle; byte[] destination = new byte[0]; uint num3 = System.Security.Cryptography.X509Certificates.X509Utils.OidToAlgId(algorithmIdentifier.pszObjId); if (num3 == 0x6602) { if (algorithmIdentifier.Parameters.cbData > 0) { if (!System.Security.Cryptography.CAPI.DecodeObject(new IntPtr(0x29L), algorithmIdentifier.Parameters.pbData, algorithmIdentifier.Parameters.cbData, out invalidHandle, out cbDecodedValue)) { throw new CryptographicException(Marshal.GetLastWin32Error()); } System.Security.Cryptography.CAPI.CRYPT_RC2_CBC_PARAMETERS crypt_rc_cbc_parameters = (System.Security.Cryptography.CAPI.CRYPT_RC2_CBC_PARAMETERS)Marshal.PtrToStructure(invalidHandle.DangerousGetHandle(), typeof(System.Security.Cryptography.CAPI.CRYPT_RC2_CBC_PARAMETERS)); switch (crypt_rc_cbc_parameters.dwVersion) { case 0x34: keyLength = 0x38; break; case 0x3a: keyLength = 0x80; break; case 160: keyLength = 40; break; } if (crypt_rc_cbc_parameters.fIV) { destination = (byte[])crypt_rc_cbc_parameters.rgbIV.Clone(); } } } else if (((num3 == 0x6801) || (num3 == 0x6601)) || (num3 == 0x6603)) { if (algorithmIdentifier.Parameters.cbData > 0) { if (!System.Security.Cryptography.CAPI.DecodeObject(new IntPtr(0x19L), algorithmIdentifier.Parameters.pbData, algorithmIdentifier.Parameters.cbData, out invalidHandle, out cbDecodedValue)) { throw new CryptographicException(Marshal.GetLastWin32Error()); } if (cbDecodedValue > 0) { if (num3 == 0x6801) { System.Security.Cryptography.CAPI.CRYPTOAPI_BLOB cryptoapi_blob = (System.Security.Cryptography.CAPI.CRYPTOAPI_BLOB)Marshal.PtrToStructure(invalidHandle.DangerousGetHandle(), typeof(System.Security.Cryptography.CAPI.CRYPTOAPI_BLOB)); if (cryptoapi_blob.cbData > 0) { destination = new byte[cryptoapi_blob.cbData]; Marshal.Copy(cryptoapi_blob.pbData, destination, 0, destination.Length); } } else { destination = new byte[cbDecodedValue]; Marshal.Copy(invalidHandle.DangerousGetHandle(), destination, 0, destination.Length); } } } if (num3 == 0x6801) { keyLength = 0x80 - (destination.Length * 8); } else if (num3 == 0x6601) { keyLength = 0x40; } else { keyLength = 0xc0; } } else if (algorithmIdentifier.Parameters.cbData > 0) { destination = new byte[algorithmIdentifier.Parameters.cbData]; Marshal.Copy(algorithmIdentifier.Parameters.pbData, destination, 0, destination.Length); } this.Reset(new System.Security.Cryptography.Oid(algorithmIdentifier.pszObjId), keyLength, destination); invalidHandle.Dispose(); }
internal static unsafe System.Security.Cryptography.CAPI.CMSG_SIGNER_ENCODE_INFO CreateSignerEncodeInfo(CmsSigner signer, bool silent) { System.Security.Cryptography.CAPI.CMSG_SIGNER_ENCODE_INFO cmsg_signer_encode_info = new System.Security.Cryptography.CAPI.CMSG_SIGNER_ENCODE_INFO(Marshal.SizeOf(typeof(System.Security.Cryptography.CAPI.CMSG_SIGNER_ENCODE_INFO))); System.Security.Cryptography.SafeCryptProvHandle invalidHandle = System.Security.Cryptography.SafeCryptProvHandle.InvalidHandle; uint pdwKeySpec = 0; bool pfCallerFreeProv = false; cmsg_signer_encode_info.HashAlgorithm.pszObjId = signer.DigestAlgorithm.Value; if (string.Compare(signer.Certificate.PublicKey.Oid.Value, "1.2.840.10040.4.1", StringComparison.Ordinal) == 0) { cmsg_signer_encode_info.HashEncryptionAlgorithm.pszObjId = "1.2.840.10040.4.3"; } cmsg_signer_encode_info.cAuthAttr = (uint)signer.SignedAttributes.Count; cmsg_signer_encode_info.rgAuthAttr = CreateCryptAttributes(signer.SignedAttributes); cmsg_signer_encode_info.cUnauthAttr = (uint)signer.UnsignedAttributes.Count; cmsg_signer_encode_info.rgUnauthAttr = CreateCryptAttributes(signer.UnsignedAttributes); if (signer.SignerIdentifierType == SubjectIdentifierType.NoSignature) { cmsg_signer_encode_info.HashEncryptionAlgorithm.pszObjId = "1.3.6.1.5.5.7.6.2"; cmsg_signer_encode_info.pCertInfo = IntPtr.Zero; cmsg_signer_encode_info.dwKeySpec = pdwKeySpec; if (!System.Security.Cryptography.CAPI.CryptAcquireContext(ref invalidHandle, (string)null, (string)null, 1, 0xf0000000)) { throw new CryptographicException(Marshal.GetLastWin32Error()); } cmsg_signer_encode_info.hCryptProv = invalidHandle.DangerousGetHandle(); GC.SuppressFinalize(invalidHandle); cmsg_signer_encode_info.SignerId.dwIdChoice = 1; X500DistinguishedName name = new X500DistinguishedName("CN=Dummy Signer") { Oid = new Oid("1.3.6.1.4.1.311.21.9") }; cmsg_signer_encode_info.SignerId.Value.IssuerSerialNumber.Issuer.cbData = (uint)name.RawData.Length; System.Security.Cryptography.SafeLocalAllocHandle handle2 = System.Security.Cryptography.CAPI.LocalAlloc(0x40, new IntPtr((long)cmsg_signer_encode_info.SignerId.Value.IssuerSerialNumber.Issuer.cbData)); Marshal.Copy(name.RawData, 0, handle2.DangerousGetHandle(), name.RawData.Length); cmsg_signer_encode_info.SignerId.Value.IssuerSerialNumber.Issuer.pbData = handle2.DangerousGetHandle(); GC.SuppressFinalize(handle2); cmsg_signer_encode_info.SignerId.Value.IssuerSerialNumber.SerialNumber.cbData = 1; System.Security.Cryptography.SafeLocalAllocHandle handle3 = System.Security.Cryptography.CAPI.LocalAlloc(0x40, new IntPtr((long)cmsg_signer_encode_info.SignerId.Value.IssuerSerialNumber.SerialNumber.cbData)); byte *handle = (byte *)handle3.DangerousGetHandle(); handle[0] = 0; cmsg_signer_encode_info.SignerId.Value.IssuerSerialNumber.SerialNumber.pbData = handle3.DangerousGetHandle(); GC.SuppressFinalize(handle3); return(cmsg_signer_encode_info); } System.Security.Cryptography.SafeCertContextHandle certContext = System.Security.Cryptography.X509Certificates.X509Utils.GetCertContext(signer.Certificate); if (!System.Security.Cryptography.CAPI.CAPISafe.CryptAcquireCertificatePrivateKey(certContext, silent ? 70 : 6, IntPtr.Zero, ref invalidHandle, ref pdwKeySpec, ref pfCallerFreeProv)) { throw new CryptographicException(Marshal.GetLastWin32Error()); } cmsg_signer_encode_info.dwKeySpec = pdwKeySpec; cmsg_signer_encode_info.hCryptProv = invalidHandle.DangerousGetHandle(); GC.SuppressFinalize(invalidHandle); System.Security.Cryptography.CAPI.CERT_CONTEXT cert_context = *((System.Security.Cryptography.CAPI.CERT_CONTEXT *)certContext.DangerousGetHandle()); cmsg_signer_encode_info.pCertInfo = cert_context.pCertInfo; if (signer.SignerIdentifierType == SubjectIdentifierType.SubjectKeyIdentifier) { uint pcbData = 0; System.Security.Cryptography.SafeLocalAllocHandle pvData = System.Security.Cryptography.SafeLocalAllocHandle.InvalidHandle; if (!System.Security.Cryptography.CAPI.CAPISafe.CertGetCertificateContextProperty(certContext, 20, pvData, ref pcbData)) { throw new CryptographicException(Marshal.GetLastWin32Error()); } if (pcbData <= 0) { return(cmsg_signer_encode_info); } pvData = System.Security.Cryptography.CAPI.LocalAlloc(0x40, new IntPtr((long)pcbData)); if (!System.Security.Cryptography.CAPI.CAPISafe.CertGetCertificateContextProperty(certContext, 20, pvData, ref pcbData)) { throw new CryptographicException(Marshal.GetLastWin32Error()); } cmsg_signer_encode_info.SignerId.dwIdChoice = 2; cmsg_signer_encode_info.SignerId.Value.KeyId.cbData = pcbData; cmsg_signer_encode_info.SignerId.Value.KeyId.pbData = pvData.DangerousGetHandle(); GC.SuppressFinalize(pvData); } return(cmsg_signer_encode_info); }
internal static string DecodeOctetString(byte[] encodedOctetString) { uint cbDecodedValue = 0; System.Security.Cryptography.SafeLocalAllocHandle decodedValue = null; if (!System.Security.Cryptography.CAPI.DecodeObject(new IntPtr(0x19L), encodedOctetString, out decodedValue, out cbDecodedValue)) { throw new CryptographicException(Marshal.GetLastWin32Error()); } if (cbDecodedValue == 0) { return(string.Empty); } System.Security.Cryptography.CAPI.CRYPTOAPI_BLOB cryptoapi_blob = (System.Security.Cryptography.CAPI.CRYPTOAPI_BLOB)Marshal.PtrToStructure(decodedValue.DangerousGetHandle(), typeof(System.Security.Cryptography.CAPI.CRYPTOAPI_BLOB)); if (cryptoapi_blob.cbData == 0) { return(string.Empty); } string str = Marshal.PtrToStringUni(cryptoapi_blob.pbData); decodedValue.Dispose(); return(str); }
internal static unsafe AlgorithmIdentifier GetAlgorithmIdentifier(System.Security.Cryptography.SafeCryptMsgHandle safeCryptMsgHandle) { AlgorithmIdentifier identifier = new AlgorithmIdentifier(); uint num = 0; if (!System.Security.Cryptography.CAPI.CAPISafe.CryptMsgGetParam(safeCryptMsgHandle, 15, 0, IntPtr.Zero, new IntPtr((void *)&num))) { checkErr(Marshal.GetLastWin32Error()); } if (num > 0) { System.Security.Cryptography.SafeLocalAllocHandle pvData = System.Security.Cryptography.CAPI.LocalAlloc(0, new IntPtr((long)num)); if (!System.Security.Cryptography.CAPI.CAPISafe.CryptMsgGetParam(safeCryptMsgHandle, 15, 0, pvData, new IntPtr((void *)&num))) { checkErr(Marshal.GetLastWin32Error()); } System.Security.Cryptography.CAPI.CRYPT_ALGORITHM_IDENTIFIER algorithmIdentifier = (System.Security.Cryptography.CAPI.CRYPT_ALGORITHM_IDENTIFIER)Marshal.PtrToStructure(pvData.DangerousGetHandle(), typeof(System.Security.Cryptography.CAPI.CRYPT_ALGORITHM_IDENTIFIER)); identifier = new AlgorithmIdentifier(algorithmIdentifier); pvData.Dispose(); } return(identifier); }
internal static unsafe int GetSignerIndex(System.Security.Cryptography.SafeCryptMsgHandle safeCrytpMsgHandle, SignerInfo signerInfo, int startIndex) { uint num = 0; uint num2 = (uint)Marshal.SizeOf(typeof(uint)); if (!System.Security.Cryptography.CAPI.CAPISafe.CryptMsgGetParam(safeCrytpMsgHandle, 5, 0, new IntPtr((void *)&num), new IntPtr((void *)&num2))) { checkErr(Marshal.GetLastWin32Error()); } for (int i = startIndex; i < num; i++) { uint num4 = 0; if (!System.Security.Cryptography.CAPI.CAPISafe.CryptMsgGetParam(safeCrytpMsgHandle, 6, (uint)i, IntPtr.Zero, new IntPtr((void *)&num4))) { checkErr(Marshal.GetLastWin32Error()); } if (num4 > 0) { System.Security.Cryptography.SafeLocalAllocHandle pvData = System.Security.Cryptography.CAPI.LocalAlloc(0, new IntPtr((long)num4)); if (!System.Security.Cryptography.CAPI.CAPISafe.CryptMsgGetParam(safeCrytpMsgHandle, 6, (uint)i, pvData, new IntPtr((void *)&num4))) { checkErr(Marshal.GetLastWin32Error()); } System.Security.Cryptography.CAPI.CMSG_SIGNER_INFO cmsgSignerInfo = signerInfo.GetCmsgSignerInfo(); System.Security.Cryptography.CAPI.CMSG_SIGNER_INFO cmsg_signer_info2 = (System.Security.Cryptography.CAPI.CMSG_SIGNER_INFO)Marshal.PtrToStructure(pvData.DangerousGetHandle(), typeof(System.Security.Cryptography.CAPI.CMSG_SIGNER_INFO)); if (System.Security.Cryptography.X509Certificates.X509Utils.MemEqual((byte *)cmsgSignerInfo.Issuer.pbData, cmsgSignerInfo.Issuer.cbData, (byte *)cmsg_signer_info2.Issuer.pbData, cmsg_signer_info2.Issuer.cbData) && System.Security.Cryptography.X509Certificates.X509Utils.MemEqual((byte *)cmsgSignerInfo.SerialNumber.pbData, cmsgSignerInfo.SerialNumber.cbData, (byte *)cmsg_signer_info2.SerialNumber.pbData, cmsg_signer_info2.SerialNumber.cbData)) { return(i); } pvData.Dispose(); } } throw new CryptographicException(-2146889714); }
internal static unsafe int GetMaxKeyLength(System.Security.Cryptography.SafeCryptProvHandle safeCryptProvHandle, uint algId) { uint dwFlags = 1; uint num2 = (uint)Marshal.SizeOf(typeof(System.Security.Cryptography.CAPI.PROV_ENUMALGS_EX)); System.Security.Cryptography.SafeLocalAllocHandle handle = System.Security.Cryptography.CAPI.LocalAlloc(0x40, new IntPtr(Marshal.SizeOf(typeof(System.Security.Cryptography.CAPI.PROV_ENUMALGS_EX)))); using (handle) { while (System.Security.Cryptography.CAPI.CAPISafe.CryptGetProvParam(safeCryptProvHandle, 0x16, handle.DangerousGetHandle(), new IntPtr((void *)&num2), dwFlags)) { System.Security.Cryptography.CAPI.PROV_ENUMALGS_EX prov_enumalgs_ex = (System.Security.Cryptography.CAPI.PROV_ENUMALGS_EX)Marshal.PtrToStructure(handle.DangerousGetHandle(), typeof(System.Security.Cryptography.CAPI.PROV_ENUMALGS_EX)); if (prov_enumalgs_ex.aiAlgid == algId) { return((int)prov_enumalgs_ex.dwMaxLen); } dwFlags = 0; } } throw new CryptographicException(-2146889726); }