コード例 #1
0
        internal static X509Certificate2Collection CreateBagOfCertificates(CmsSigner signer)
        {
            X509Certificate2Collection certificate2Collection = new X509Certificate2Collection();

            certificate2Collection.AddRange(signer.Certificates);
            if (signer.IncludeOption != X509IncludeOption.None)
            {
                if (signer.IncludeOption == X509IncludeOption.EndCertOnly)
                {
                    certificate2Collection.Add(signer.Certificate);
                }
                else
                {
                    int       num       = 1;
                    X509Chain x509Chain = new X509Chain();
                    x509Chain.Build(signer.Certificate);
                    if (x509Chain.ChainStatus.Length > 0 && (x509Chain.ChainStatus[0].Status & X509ChainStatusFlags.PartialChain) == X509ChainStatusFlags.PartialChain)
                    {
                        throw new CryptographicException(-2146762486);
                    }
                    if (signer.IncludeOption == X509IncludeOption.WholeChain)
                    {
                        num = x509Chain.ChainElements.Count;
                    }
                    else if (x509Chain.ChainElements.Count > 1)
                    {
                        num = x509Chain.ChainElements.Count - 1;
                    }
                    for (int index = 0; index < num; ++index)
                    {
                        certificate2Collection.Add(x509Chain.ChainElements[index].Certificate);
                    }
                }
            }
            return(certificate2Collection);
        }
コード例 #2
0
 public void ComputeSignature(CmsSigner signer)
 {
     this.ComputeSignature(signer, true);
 }
コード例 #3
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);
            }
        }
コード例 #4
0
 internal static CAPI.CMSG_SIGNER_ENCODE_INFO CreateSignerEncodeInfo(CmsSigner signer)
 {
     return(PkcsUtils.CreateSignerEncodeInfo(signer, false));
 }