Beispiel #1
0
        internal static unsafe CAPI.CMSG_SIGNER_ENCODE_INFO CreateSignerEncodeInfo(CmsSigner signer, bool silent)
        {
            CAPI.CMSG_SIGNER_ENCODE_INFO signerEncodeInfo = new CAPI.CMSG_SIGNER_ENCODE_INFO(Marshal.SizeOf(typeof(CAPI.CMSG_SIGNER_ENCODE_INFO)));
            SafeCryptProvHandle          invalidHandle1   = SafeCryptProvHandle.InvalidHandle;
            uint pdwKeySpec       = 0U;
            bool pfCallerFreeProv = false;

            signerEncodeInfo.HashAlgorithm.pszObjId = signer.DigestAlgorithm.Value;
            if (string.Compare(signer.Certificate.PublicKey.Oid.Value, "1.2.840.10040.4.1", StringComparison.Ordinal) == 0)
            {
                signerEncodeInfo.HashEncryptionAlgorithm.pszObjId = "1.2.840.10040.4.3";
            }
            signerEncodeInfo.cAuthAttr    = (uint)signer.SignedAttributes.Count;
            signerEncodeInfo.rgAuthAttr   = PkcsUtils.CreateCryptAttributes(signer.SignedAttributes);
            signerEncodeInfo.cUnauthAttr  = (uint)signer.UnsignedAttributes.Count;
            signerEncodeInfo.rgUnauthAttr = PkcsUtils.CreateCryptAttributes(signer.UnsignedAttributes);
            if (signer.SignerIdentifierType == SubjectIdentifierType.NoSignature)
            {
                signerEncodeInfo.HashEncryptionAlgorithm.pszObjId = "1.3.6.1.5.5.7.6.2";
                signerEncodeInfo.pCertInfo = IntPtr.Zero;
                signerEncodeInfo.dwKeySpec = pdwKeySpec;
                if (!CAPI.CryptAcquireContext(out invalidHandle1, (string)null, (string)null, 1U, 4026531840U))
                {
                    throw new CryptographicException(Marshal.GetLastWin32Error());
                }
                signerEncodeInfo.hCryptProv = invalidHandle1.DangerousGetHandle();
                GC.SuppressFinalize((object)invalidHandle1);
                signerEncodeInfo.SignerId.dwIdChoice = 1U;
                X500DistinguishedName distinguishedName = new X500DistinguishedName("CN=Dummy Signer");
                distinguishedName.Oid = new Oid("1.3.6.1.4.1.311.21.9");
                signerEncodeInfo.SignerId.Value.IssuerSerialNumber.Issuer.cbData = (uint)distinguishedName.RawData.Length;
                SafeLocalAllocHandle localAllocHandle1 = CAPI.LocalAlloc(64U, new IntPtr((long)signerEncodeInfo.SignerId.Value.IssuerSerialNumber.Issuer.cbData));
                Marshal.Copy(distinguishedName.RawData, 0, localAllocHandle1.DangerousGetHandle(), distinguishedName.RawData.Length);
                signerEncodeInfo.SignerId.Value.IssuerSerialNumber.Issuer.pbData = localAllocHandle1.DangerousGetHandle();
                GC.SuppressFinalize((object)localAllocHandle1);
                signerEncodeInfo.SignerId.Value.IssuerSerialNumber.SerialNumber.cbData = 1U;
                SafeLocalAllocHandle localAllocHandle2 = CAPI.LocalAlloc(64U, new IntPtr((long)signerEncodeInfo.SignerId.Value.IssuerSerialNumber.SerialNumber.cbData));
                *(sbyte *)(void *)localAllocHandle2.DangerousGetHandle() = (sbyte)0;
                signerEncodeInfo.SignerId.Value.IssuerSerialNumber.SerialNumber.pbData = localAllocHandle2.DangerousGetHandle();
                GC.SuppressFinalize((object)localAllocHandle2);
                return(signerEncodeInfo);
            }
            else
            {
                System.Security.Cryptography.SafeCertContextHandle certContext1 = X509Utils.GetCertContext(signer.Certificate);
                if (!CAPI.CAPISafe.CryptAcquireCertificatePrivateKey(certContext1, silent ? 70U : 6U, IntPtr.Zero, out invalidHandle1, out pdwKeySpec, out pfCallerFreeProv))
                {
                    throw new CryptographicException(Marshal.GetLastWin32Error());
                }
                signerEncodeInfo.dwKeySpec  = pdwKeySpec;
                signerEncodeInfo.hCryptProv = invalidHandle1.DangerousGetHandle();
                GC.SuppressFinalize((object)invalidHandle1);
                CAPI.CERT_CONTEXT certContext2 = *(CAPI.CERT_CONTEXT *)(void *) certContext1.DangerousGetHandle();
                signerEncodeInfo.pCertInfo = certContext2.pCertInfo;
                if (signer.SignerIdentifierType == SubjectIdentifierType.SubjectKeyIdentifier)
                {
                    uint pcbData = 0U;
                    SafeLocalAllocHandle invalidHandle2 = SafeLocalAllocHandle.InvalidHandle;
                    if (!CAPI.CAPISafe.CertGetCertificateContextProperty(certContext1, 20U, invalidHandle2, out pcbData))
                    {
                        throw new CryptographicException(Marshal.GetLastWin32Error());
                    }
                    if (pcbData > 0U)
                    {
                        SafeLocalAllocHandle pvData = CAPI.LocalAlloc(64U, new IntPtr((long)pcbData));
                        if (!CAPI.CAPISafe.CertGetCertificateContextProperty(certContext1, 20U, pvData, out pcbData))
                        {
                            throw new CryptographicException(Marshal.GetLastWin32Error());
                        }
                        signerEncodeInfo.SignerId.dwIdChoice         = 2U;
                        signerEncodeInfo.SignerId.Value.KeyId.cbData = pcbData;
                        signerEncodeInfo.SignerId.Value.KeyId.pbData = pvData.DangerousGetHandle();
                        GC.SuppressFinalize((object)pvData);
                    }
                }
                return(signerEncodeInfo);
            }
        }