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 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 X509Certificate2Collection GetCertificates(System.Security.Cryptography.SafeCryptMsgHandle safeCryptMsgHandle) { uint num = 0; uint num2 = (uint)Marshal.SizeOf(typeof(uint)); X509Certificate2Collection certificates = new X509Certificate2Collection(); if (!System.Security.Cryptography.CAPI.CAPISafe.CryptMsgGetParam(safeCryptMsgHandle, 11, 0, new IntPtr((void *)&num), new IntPtr((void *)&num2))) { checkErr(Marshal.GetLastWin32Error()); } for (uint i = 0; i < num; i++) { uint cbData = 0; System.Security.Cryptography.SafeLocalAllocHandle invalidHandle = System.Security.Cryptography.SafeLocalAllocHandle.InvalidHandle; GetParam(safeCryptMsgHandle, 12, i, out invalidHandle, out cbData); if (cbData > 0) { System.Security.Cryptography.SafeCertContextHandle handle2 = System.Security.Cryptography.CAPI.CAPISafe.CertCreateCertificateContext(0x10001, invalidHandle, cbData); if ((handle2 == null) || handle2.IsInvalid) { throw new CryptographicException(Marshal.GetLastWin32Error()); } certificates.Add(new X509Certificate2(handle2.DangerousGetHandle())); handle2.Dispose(); } } return(certificates); }
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); }
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 uint OidToAlgId(string value) { SafeLocalAllocHandle pszOid = StringToAnsiPtr(value); CAPI.CRYPT_OID_INFO pOIDInfo = CAPI.CryptFindOIDInfo(CAPI.CRYPT_OID_INFO_OID_KEY, pszOid, 0); return(pOIDInfo.Algid); }
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 bool GetPrivateKeyInfo(System.Security.Cryptography.SafeCertContextHandle safeCertContext, ref CspParameters parameters) { System.Security.Cryptography.SafeLocalAllocHandle invalidHandle = System.Security.Cryptography.SafeLocalAllocHandle.InvalidHandle; uint pcbData = 0; if (!System.Security.Cryptography.CAPI.CAPISafe.CertGetCertificateContextProperty(safeCertContext, 2, invalidHandle, ref pcbData)) { if (Marshal.GetLastWin32Error() != -2146885628) { throw new CryptographicException(Marshal.GetLastWin32Error()); } return(false); } invalidHandle = System.Security.Cryptography.CAPI.LocalAlloc(0, new IntPtr((long)pcbData)); if (!System.Security.Cryptography.CAPI.CAPISafe.CertGetCertificateContextProperty(safeCertContext, 2, invalidHandle, ref pcbData)) { if (Marshal.GetLastWin32Error() != -2146885628) { throw new CryptographicException(Marshal.GetLastWin32Error()); } return(false); } System.Security.Cryptography.CAPI.CRYPT_KEY_PROV_INFO crypt_key_prov_info = (System.Security.Cryptography.CAPI.CRYPT_KEY_PROV_INFO)Marshal.PtrToStructure(invalidHandle.DangerousGetHandle(), typeof(System.Security.Cryptography.CAPI.CRYPT_KEY_PROV_INFO)); parameters.ProviderName = crypt_key_prov_info.pwszProvName; parameters.KeyContainerName = crypt_key_prov_info.pwszContainerName; parameters.ProviderType = (int)crypt_key_prov_info.dwProvType; parameters.KeyNumber = (int)crypt_key_prov_info.dwKeySpec; parameters.Flags = ((crypt_key_prov_info.dwFlags & 0x20) == 0x20) ? CspProviderFlags.UseMachineKeyStore : CspProviderFlags.NoFlags; invalidHandle.Dispose(); return(true); }
internal unsafe SignerInfoCollection(SignedCms signedCms) { uint num = 0; uint num2 = (uint)Marshal.SizeOf(typeof(uint)); System.Security.Cryptography.SafeCryptMsgHandle cryptMsgHandle = signedCms.GetCryptMsgHandle(); if (!System.Security.Cryptography.CAPI.CAPISafe.CryptMsgGetParam(cryptMsgHandle, 5, 0, new IntPtr((void *)&num), new IntPtr((void *)&num2))) { throw new CryptographicException(Marshal.GetLastWin32Error()); } SignerInfo[] infoArray = new SignerInfo[num]; for (int i = 0; i < num; i++) { uint num4 = 0; if (!System.Security.Cryptography.CAPI.CAPISafe.CryptMsgGetParam(cryptMsgHandle, 6, (uint)i, IntPtr.Zero, new IntPtr((void *)&num4))) { throw new CryptographicException(Marshal.GetLastWin32Error()); } System.Security.Cryptography.SafeLocalAllocHandle pvData = System.Security.Cryptography.CAPI.LocalAlloc(0, new IntPtr((long)num4)); if (!System.Security.Cryptography.CAPI.CAPISafe.CryptMsgGetParam(cryptMsgHandle, 6, (uint)i, pvData, new IntPtr((void *)&num4))) { throw new CryptographicException(Marshal.GetLastWin32Error()); } infoArray[i] = new SignerInfo(signedCms, pvData); } this.m_signerInfos = infoArray; }
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 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 SignerInfo(SignedCms signedCms, System.Security.Cryptography.SafeLocalAllocHandle pbCmsgSignerInfo) { this.m_signedCms = signedCms; this.m_parentSignerInfo = null; this.m_encodedSignerInfo = null; this.m_pbCmsgSignerInfo = pbCmsgSignerInfo; this.m_cmsgSignerInfo = (System.Security.Cryptography.CAPI.CMSG_SIGNER_INFO)Marshal.PtrToStructure(pbCmsgSignerInfo.DangerousGetHandle(), typeof(System.Security.Cryptography.CAPI.CMSG_SIGNER_INFO)); }
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 SignerInfo(SignedCms signedCms, System.Security.Cryptography.SafeLocalAllocHandle pbCmsgSignerInfo) { this.m_signedCms = signedCms; this.m_parentSignerInfo = null; this.m_encodedSignerInfo = null; this.m_pbCmsgSignerInfo = pbCmsgSignerInfo; this.m_cmsgSignerInfo = (System.Security.Cryptography.CAPI.CMSG_SIGNER_INFO) Marshal.PtrToStructure(pbCmsgSignerInfo.DangerousGetHandle(), typeof(System.Security.Cryptography.CAPI.CMSG_SIGNER_INFO)); }
bool CryptDecodeObject( [In] uint dwCertEncodingType, [In] IntPtr lpszStructType, [In] IntPtr pbEncoded, [In] uint cbEncoded, [In] uint dwFlags, [In, Out] SafeLocalAllocHandle pvStructInfo, [In, Out] IntPtr pcbStructInfo);
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 static SafeLocalAllocHandle LocalAlloc(uint uFlags, IntPtr sizetdwBytes) { SafeLocalAllocHandle handle = CAPISafe.LocalAlloc(uFlags, sizetdwBytes); if ((handle == null) || handle.IsInvalid) { throw new OutOfMemoryException(); } return(handle); }
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 LocalAlloc(uint uFlags, IntPtr sizetdwBytes) { SafeLocalAllocHandle safeLocalAllocHandle = UnsafeNativeMethods.LocalAlloc(uFlags, sizetdwBytes); if (safeLocalAllocHandle == null || safeLocalAllocHandle.IsInvalid) { throw new OutOfMemoryException(); } return(safeLocalAllocHandle); }
internal RecipientInfo (RecipientInfoType recipientInfoType, RecipientSubType recipientSubType, SafeLocalAllocHandle pCmsgRecipientInfo, Object cmsgRecipientInfo, uint index) { if (recipientInfoType < RecipientInfoType.Unknown || recipientInfoType > RecipientInfoType.KeyAgreement) recipientInfoType = RecipientInfoType.Unknown; if (recipientSubType < RecipientSubType.Unknown || recipientSubType > RecipientSubType.PublicKeyAgreement) recipientSubType = RecipientSubType.Unknown; m_recipentInfoType = recipientInfoType; m_recipientSubType = recipientSubType; m_pCmsgRecipientInfo = pCmsgRecipientInfo; m_cmsgRecipientInfo = cmsgRecipientInfo; m_index = index; }
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); }
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); }
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 RecipientInfo(RecipientInfoType recipientInfoType, RecipientSubType recipientSubType, System.Security.Cryptography.SafeLocalAllocHandle pCmsgRecipientInfo, object cmsgRecipientInfo, uint index) { if ((recipientInfoType < RecipientInfoType.Unknown) || (recipientInfoType > RecipientInfoType.KeyAgreement)) { recipientInfoType = RecipientInfoType.Unknown; } if ((recipientSubType < RecipientSubType.Unknown) || (recipientSubType > RecipientSubType.PublicKeyAgreement)) { recipientSubType = RecipientSubType.Unknown; } this.m_recipentInfoType = recipientInfoType; this.m_recipientSubType = recipientSubType; this.m_pCmsgRecipientInfo = pCmsgRecipientInfo; this.m_cmsgRecipientInfo = cmsgRecipientInfo; this.m_index = index; }
internal unsafe KeyTransRecipientInfo(System.Security.Cryptography.SafeLocalAllocHandle pRecipientInfo, System.Security.Cryptography.CAPI.CERT_INFO certInfo, uint index) : base(RecipientInfoType.KeyTransport, RecipientSubType.Pkcs7KeyTransport, pRecipientInfo, certInfo, index) { int version = 2; byte *pbData = (byte *)certInfo.SerialNumber.pbData; for (int i = 0; i < certInfo.SerialNumber.cbData; i++) { pbData++; if (pbData[0] != 0) { version = 0; break; } } this.Reset(version); }
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 static string FindOidInfo(uint keyType, string keyValue, Cryptography.OidGroup oidGroup) { if (keyValue == null) { throw new ArgumentNullException("keyValue"); } if (keyValue.Length == 0) { return(null); } SafeLocalAllocHandle pvKey = SafeLocalAllocHandle.InvalidHandle; try { switch (keyType) { case CAPI.CRYPT_OID_INFO_OID_KEY: pvKey = StringToAnsiPtr(keyValue); break; case CAPI.CRYPT_OID_INFO_NAME_KEY: pvKey = StringToUniPtr(keyValue); break; default: Debug.Assert(false); break; } CAPI.CRYPT_OID_INFO pOidInfo = CAPI.CryptFindOIDInfo(keyType, pvKey, oidGroup); if (keyType == CAPI.CRYPT_OID_INFO_OID_KEY) { return(pOidInfo.pwszName); } else { return(pOidInfo.pszOID); } } finally { pvKey.Dispose(); } }
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 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 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 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 static unsafe bool DecodeObject(IntPtr pszStructType, IntPtr pbEncoded, uint cbEncoded, out SafeLocalAllocHandle decodedValue, out uint cbDecodedValue) { // Initialize out parameters decodedValue = SafeLocalAllocHandle.InvalidHandle; cbDecodedValue = 0; // Decode uint cbDecoded = 0; SafeLocalAllocHandle ptr = SafeLocalAllocHandle.InvalidHandle; if (!UnsafeNativeMethods.CryptDecodeObject(X509_ASN_ENCODING | PKCS_7_ASN_ENCODING, pszStructType, pbEncoded, cbEncoded, 0, ptr, new IntPtr(&cbDecoded))) { return(false); } ptr = LocalAlloc(LMEM_FIXED, new IntPtr(cbDecoded)); if (!UnsafeNativeMethods.CryptDecodeObject(X509_ASN_ENCODING | PKCS_7_ASN_ENCODING, pszStructType, pbEncoded, cbEncoded, 0, ptr, new IntPtr(&cbDecoded))) { return(false); } // Return decoded values decodedValue = ptr; cbDecodedValue = cbDecoded; return(true); }
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); }
internal unsafe SignerInfo (SignedCms signedCms, SignerInfo parentSignerInfo, byte[] encodedSignerInfo) { // Sanity check. Debug.Assert(signedCms != null && encodedSignerInfo != null && encodedSignerInfo.Length > 0); uint cbCmsgSignerInfo = 0; SafeLocalAllocHandle pbCmsgSignerInfo = SafeLocalAllocHandle.InvalidHandle; fixed (byte * pEncodedSignerInfo = &encodedSignerInfo[0]) { if (!CAPI.DecodeObject(new IntPtr(CAPI.PKCS7_SIGNER_INFO), new IntPtr(pEncodedSignerInfo), (uint) encodedSignerInfo.Length, out pbCmsgSignerInfo, out cbCmsgSignerInfo)) throw new CryptographicException(Marshal.GetLastWin32Error()); } m_signedCms = signedCms; m_parentSignerInfo = parentSignerInfo; m_encodedSignerInfo = (byte[]) encodedSignerInfo.Clone(); m_pbCmsgSignerInfo = pbCmsgSignerInfo; m_cmsgSignerInfo = (CAPI.CMSG_SIGNER_INFO) Marshal.PtrToStructure(pbCmsgSignerInfo.DangerousGetHandle(), typeof(CAPI.CMSG_SIGNER_INFO)); }
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 unsafe KeyTransRecipientInfo (SafeLocalAllocHandle pRecipientInfo, CAPI.CERT_INFO certInfo, uint index) : base(RecipientInfoType.KeyTransport, RecipientSubType.Pkcs7KeyTransport, pRecipientInfo, certInfo, index) { // If serial number is 0, then it is the special SKI encoding. int version = 2; byte * pb = (byte *) certInfo.SerialNumber.pbData; for (int i = 0; i < certInfo.SerialNumber.cbData; i++) { if (*pb++ != (byte) 0) { version = 0; break; } } Reset(version); }
internal KeyTransRecipientInfo (SafeLocalAllocHandle pRecipientInfo, CAPI.CMSG_KEY_TRANS_RECIPIENT_INFO keyTrans, uint index) : base(RecipientInfoType.KeyTransport, RecipientSubType.CmsKeyTransport, pRecipientInfo, keyTrans, index) { Reset((int) keyTrans.dwVersion); }
internal KeyAgreeRecipientInfo (SafeLocalAllocHandle pRecipientInfo, CAPI.CMSG_KEY_AGREE_PUBLIC_KEY_RECIPIENT_INFO publicKeyRecipient, uint index, uint subIndex) : base(RecipientInfoType.KeyAgreement, RecipientSubType.PublicKeyAgreement, pRecipientInfo, publicKeyRecipient, index) { checked { IntPtr pEncryptedKeyInfo = Marshal.ReadIntPtr(new IntPtr((long) publicKeyRecipient.rgpRecipientEncryptedKeys + (long) (subIndex * Marshal.SizeOf(typeof(IntPtr))))); CAPI.CMSG_RECIPIENT_ENCRYPTED_KEY_INFO encryptedKeyInfo = (CAPI.CMSG_RECIPIENT_ENCRYPTED_KEY_INFO) Marshal.PtrToStructure(pEncryptedKeyInfo, typeof(CAPI.CMSG_RECIPIENT_ENCRYPTED_KEY_INFO)); Reset(CAPI.CMSG_KEY_AGREE_ORIGINATOR_PUBLIC_KEY, publicKeyRecipient.dwVersion, encryptedKeyInfo, subIndex); } }
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 static unsafe void GetParam (SafeCryptMsgHandle safeCryptMsgHandle, uint paramType, uint index, out SafeLocalAllocHandle pvData, out uint cbData) { cbData = 0; pvData = SafeLocalAllocHandle.InvalidHandle; fixed (uint * pcbData = &cbData) { if (!CAPI.CAPISafe.CryptMsgGetParam(safeCryptMsgHandle, paramType, index, pvData, new IntPtr(pcbData))) checkErr(Marshal.GetLastWin32Error()); if (cbData > 0) { pvData = CAPI.LocalAlloc(CAPI.LPTR, new IntPtr(cbData)); if (!CAPI.CAPISafe.CryptMsgGetParam(safeCryptMsgHandle, paramType, index, pvData, new IntPtr(pcbData))) checkErr(Marshal.GetLastWin32Error()); } } }
internal CryptographicAttributeObjectCollection (SafeLocalAllocHandle pCryptAttributes) : this(pCryptAttributes.DangerousGetHandle()) {}
internal static unsafe bool DecodeObject(IntPtr pszStructType, IntPtr pbEncoded, uint cbEncoded, out SafeLocalAllocHandle decodedValue, out uint cbDecodedValue) { decodedValue = SafeLocalAllocHandle.InvalidHandle; cbDecodedValue = 0; uint num = 0; SafeLocalAllocHandle invalidHandle = SafeLocalAllocHandle.InvalidHandle; if (!CAPISafe.CryptDecodeObject(0x10001, pszStructType, pbEncoded, cbEncoded, 0, invalidHandle, new IntPtr((void*) &num))) { return false; } invalidHandle = LocalAlloc(0, new IntPtr((long) num)); if (!CAPISafe.CryptDecodeObject(0x10001, pszStructType, pbEncoded, cbEncoded, 0, invalidHandle, new IntPtr((void*) &num))) { return false; } decodedValue = invalidHandle; cbDecodedValue = num; return true; }