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 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);
        }
Exemplo n.º 3
0
        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 CMSG_SIGNER_ENCODE_INFO(int size)
 {
     this.cbSize = (uint) size;
     this.pCertInfo = IntPtr.Zero;
     this.hCryptProv = IntPtr.Zero;
     this.dwKeySpec = 0;
     this.HashAlgorithm = new System.Security.Cryptography.CAPI.CRYPT_ALGORITHM_IDENTIFIER();
     this.pvHashAuxInfo = IntPtr.Zero;
     this.cAuthAttr = 0;
     this.rgAuthAttr = IntPtr.Zero;
     this.cUnauthAttr = 0;
     this.rgUnauthAttr = IntPtr.Zero;
     this.SignerId = new System.Security.Cryptography.CAPI.CERT_ID();
     this.HashEncryptionAlgorithm = new System.Security.Cryptography.CAPI.CRYPT_ALGORITHM_IDENTIFIER();
     this.pvHashEncryptionAuxInfo = IntPtr.Zero;
 }
 internal CMSG_ENVELOPED_ENCODE_INFO(int size)
 {
     this.cbSize = (uint) size;
     this.hCryptProv = IntPtr.Zero;
     this.ContentEncryptionAlgorithm = new System.Security.Cryptography.CAPI.CRYPT_ALGORITHM_IDENTIFIER();
     this.pvEncryptionAuxInfo = IntPtr.Zero;
     this.cRecipients = 0;
     this.rgpRecipients = IntPtr.Zero;
     this.rgCmsRecipients = IntPtr.Zero;
     this.cCertEncoded = 0;
     this.rgCertEncoded = IntPtr.Zero;
     this.cCrlEncoded = 0;
     this.rgCrlEncoded = IntPtr.Zero;
     this.cAttrCertEncoded = 0;
     this.rgAttrCertEncoded = IntPtr.Zero;
     this.cUnprotectedAttr = 0;
     this.rgUnprotectedAttr = IntPtr.Zero;
 }
 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;
 }