Exemplo n.º 1
0
 internal SignerInfo(SignedCms signedCms, SafeLocalAllocHandle pbCmsgSignerInfo)
 {
     this.m_signedCms         = signedCms;
     this.m_parentSignerInfo  = (SignerInfo)null;
     this.m_encodedSignerInfo = (byte[])null;
     this.m_pbCmsgSignerInfo  = pbCmsgSignerInfo;
     this.m_cmsgSignerInfo    = (CAPI.CMSG_SIGNER_INFO)Marshal.PtrToStructure(pbCmsgSignerInfo.DangerousGetHandle(), typeof(CAPI.CMSG_SIGNER_INFO));
 }
Exemplo n.º 2
0
        internal static SafeLocalAllocHandle StringToAnsiPtr(string s)
        {
            byte[] numArray = new byte[s.Length + 1];
            Encoding.ASCII.GetBytes(s, 0, s.Length, numArray, 0);
            SafeLocalAllocHandle localAllocHandle = CAPI.LocalAlloc(0U, new IntPtr(numArray.Length));

            Marshal.Copy(numArray, 0, localAllocHandle.DangerousGetHandle(), numArray.Length);
            return(localAllocHandle);
        }
Exemplo n.º 3
0
        internal static unsafe IntPtr CreateCryptAttributes(CryptographicAttributeObjectCollection attributes)
        {
            if (attributes.Count == 0)
            {
                return(IntPtr.Zero);
            }
            uint num1 = 0U;
            uint num2 = PkcsUtils.AlignedLength((uint)Marshal.SizeOf(typeof(PkcsUtils.I_CRYPT_ATTRIBUTE)));
            uint num3 = PkcsUtils.AlignedLength((uint)Marshal.SizeOf(typeof(CAPI.CRYPTOAPI_BLOB)));

            foreach (CryptographicAttributeObject cryptographicAttributeObject in attributes)
            {
                num1 = num1 + num2 + PkcsUtils.AlignedLength((uint)(cryptographicAttributeObject.Oid.Value.Length + 1));
                foreach (AsnEncodedData asnEncodedData in cryptographicAttributeObject.Values)
                {
                    num1 = num1 + num3 + PkcsUtils.AlignedLength((uint)asnEncodedData.RawData.Length);
                }
            }
            SafeLocalAllocHandle localAllocHandle = CAPI.LocalAlloc(64U, new IntPtr((long)num1));

            PkcsUtils.I_CRYPT_ATTRIBUTE *iCryptAttributePtr = (PkcsUtils.I_CRYPT_ATTRIBUTE *)(void *) localAllocHandle.DangerousGetHandle();
            IntPtr num4 = new IntPtr((long)localAllocHandle.DangerousGetHandle() + (long)num2 * (long)attributes.Count);

            foreach (CryptographicAttributeObject cryptographicAttributeObject in attributes)
            {
                byte * numPtr   = (byte *)(void *)num4;
                byte[] numArray = new byte[cryptographicAttributeObject.Oid.Value.Length + 1];
                CAPI.CRYPTOAPI_BLOB *cryptoapiBlobPtr = (CAPI.CRYPTOAPI_BLOB *)(numPtr + (int)PkcsUtils.AlignedLength((uint)numArray.Length));
                iCryptAttributePtr->pszObjId = (IntPtr)((void *)numPtr);
                iCryptAttributePtr->cValue   = (uint)cryptographicAttributeObject.Values.Count;
                iCryptAttributePtr->rgValue  = (IntPtr)((void *)cryptoapiBlobPtr);
                Encoding.ASCII.GetBytes(cryptographicAttributeObject.Oid.Value, 0, cryptographicAttributeObject.Oid.Value.Length, numArray, 0);
                Marshal.Copy(numArray, 0, iCryptAttributePtr->pszObjId, numArray.Length);
                IntPtr destination = new IntPtr((long)cryptoapiBlobPtr + (long)cryptographicAttributeObject.Values.Count * (long)num3);
                foreach (AsnEncodedData asnEncodedData in cryptographicAttributeObject.Values)
                {
                    byte[] rawData = asnEncodedData.RawData;
                    if (rawData.Length > 0)
                    {
                        cryptoapiBlobPtr->cbData = (uint)rawData.Length;
                        cryptoapiBlobPtr->pbData = destination;
                        Marshal.Copy(rawData, 0, destination, rawData.Length);
                        destination = new IntPtr((long)destination + (long)PkcsUtils.AlignedLength((uint)rawData.Length));
                    }
                    ++cryptoapiBlobPtr;
                }
                ++iCryptAttributePtr;
                num4 = destination;
            }
            GC.SuppressFinalize((object)localAllocHandle);
            return(localAllocHandle.DangerousGetHandle());
        }
Exemplo n.º 4
0
        private unsafe void Sign(CmsSigner signer, bool silent)
        {
            CAPI.CMSG_SIGNED_ENCODE_INFO signedEncodeInfo = new CAPI.CMSG_SIGNED_ENCODE_INFO(Marshal.SizeOf(typeof(CAPI.CMSG_SIGNED_ENCODE_INFO)));
            CAPI.CMSG_SIGNER_ENCODE_INFO signerEncodeInfo = PkcsUtils.CreateSignerEncodeInfo(signer, silent);
            byte[] encodedMessage = (byte[])null;
            try
            {
                SafeLocalAllocHandle localAllocHandle = CAPI.LocalAlloc(0U, new IntPtr(Marshal.SizeOf(typeof(CAPI.CMSG_SIGNER_ENCODE_INFO))));
                try
                {
                    Marshal.StructureToPtr((object)signerEncodeInfo, localAllocHandle.DangerousGetHandle(), false);
                    X509Certificate2Collection bagOfCertificates = PkcsUtils.CreateBagOfCertificates(signer);
                    SafeLocalAllocHandle       encodedCertBlob   = PkcsUtils.CreateEncodedCertBlob(bagOfCertificates);
                    signedEncodeInfo.cSigners     = 1U;
                    signedEncodeInfo.rgSigners    = localAllocHandle.DangerousGetHandle();
                    signedEncodeInfo.cCertEncoded = (uint)bagOfCertificates.Count;
                    if (bagOfCertificates.Count > 0)
                    {
                        signedEncodeInfo.rgCertEncoded = encodedCertBlob.DangerousGetHandle();
                    }
                    SafeCryptMsgHandle safeCryptMsgHandle = string.Compare(this.ContentInfo.ContentType.Value, "1.2.840.113549.1.7.1", StringComparison.OrdinalIgnoreCase) != 0 ? CAPI.CryptMsgOpenToEncode(65537U, this.Detached ? 4U : 0U, 2U, new IntPtr((void *)&signedEncodeInfo), this.ContentInfo.ContentType.Value, IntPtr.Zero) : CAPI.CryptMsgOpenToEncode(65537U, this.Detached ? 4U : 0U, 2U, new IntPtr((void *)&signedEncodeInfo), IntPtr.Zero, IntPtr.Zero);
                    if (safeCryptMsgHandle == null || safeCryptMsgHandle.IsInvalid)
                    {
                        throw new CryptographicException(Marshal.GetLastWin32Error());
                    }
                    if (this.ContentInfo.Content.Length > 0 && !CAPI.CAPISafe.CryptMsgUpdate(safeCryptMsgHandle, this.ContentInfo.pContent, (uint)this.ContentInfo.Content.Length, true))
                    {
                        throw new CryptographicException(Marshal.GetLastWin32Error());
                    }
                    encodedMessage = PkcsUtils.GetContent(safeCryptMsgHandle);
                    safeCryptMsgHandle.Dispose();
                    encodedCertBlob.Dispose();
                }
                finally
                {
                    Marshal.DestroyStructure(localAllocHandle.DangerousGetHandle(), typeof(CAPI.CMSG_SIGNER_ENCODE_INFO));
                    localAllocHandle.Dispose();
                }
            }
            finally
            {
                signerEncodeInfo.Dispose();
            }
            SafeCryptMsgHandle safeCryptMsgHandle1 = SignedCms.OpenToDecode(encodedMessage, this.ContentInfo, this.Detached);

            if (this.m_safeCryptMsgHandle != null && !this.m_safeCryptMsgHandle.IsInvalid)
            {
                this.m_safeCryptMsgHandle.Dispose();
            }
            this.m_safeCryptMsgHandle = safeCryptMsgHandle1;
            GC.KeepAlive((object)signer);
        }
Exemplo n.º 5
0
        private static byte[] Encode(DateTime signingTime)
        {
            long val = signingTime.ToFileTimeUtc();
            SafeLocalAllocHandle localAllocHandle = CAPI.LocalAlloc(64U, new IntPtr(Marshal.SizeOf(typeof(long))));

            Marshal.WriteInt64(localAllocHandle.DangerousGetHandle(), val);
            byte[] encodedData = new byte[0];
            if (!CAPI.EncodeObject("1.2.840.113549.1.9.5", localAllocHandle.DangerousGetHandle(), out encodedData))
            {
                throw new CryptographicException(Marshal.GetLastWin32Error());
            }
            localAllocHandle.Dispose();
            return(encodedData);
        }
Exemplo n.º 6
0
        private void Decode()
        {
            uint cbDecodedValue = 0U;
            SafeLocalAllocHandle decodedValue = (SafeLocalAllocHandle)null;

            if (!CAPI.DecodeObject(new IntPtr(17L), this.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;
        }
Exemplo n.º 7
0
        internal static byte[] DecodeOctetBytes(byte[] encodedOctetString)
        {
            uint cbDecodedValue = 0U;
            SafeLocalAllocHandle decodedValue = (SafeLocalAllocHandle)null;

            if (!CAPI.DecodeObject(new IntPtr(25L), encodedOctetString, out decodedValue, out cbDecodedValue))
            {
                throw new CryptographicException(Marshal.GetLastWin32Error());
            }
            if ((int)cbDecodedValue == 0)
            {
                return(new byte[0]);
            }
            using (decodedValue)
                return(CAPI.BlobToByteArray(decodedValue.DangerousGetHandle()));
        }
Exemplo n.º 8
0
        internal AlgorithmIdentifier(CAPI.CERT_PUBLIC_KEY_INFO keyInfo)
        {
            SafeLocalAllocHandle localAllocHandle = CAPI.LocalAlloc(64U, new IntPtr(Marshal.SizeOf(typeof(CAPI.CERT_PUBLIC_KEY_INFO))));

            Marshal.StructureToPtr((object)keyInfo, localAllocHandle.DangerousGetHandle(), false);
            int keyLength = (int)CAPI.CAPISafe.CertGetPublicKeyLength(65537U, localAllocHandle.DangerousGetHandle());

            byte[] numArray = new byte[(IntPtr)keyInfo.Algorithm.Parameters.cbData];
            if (numArray.Length > 0)
            {
                Marshal.Copy(keyInfo.Algorithm.Parameters.pbData, numArray, 0, numArray.Length);
            }
            Marshal.DestroyStructure(localAllocHandle.DangerousGetHandle(), typeof(CAPI.CERT_PUBLIC_KEY_INFO));
            localAllocHandle.Dispose();
            this.Reset(new Oid(keyInfo.Algorithm.pszObjId), keyLength, numArray);
        }
Exemplo n.º 9
0
        internal KeyTransRecipientInfo(SafeLocalAllocHandle pRecipientInfo, CAPI.CERT_INFO certInfo, uint index)
            : base(RecipientInfoType.KeyTransport, RecipientSubType.Pkcs7KeyTransport, pRecipientInfo, (object)certInfo, index)
        {
            int   version = 2;
            byte *numPtr  = (byte *)(void *)certInfo.SerialNumber.pbData;

            for (int index1 = 0; (long)index1 < (long)certInfo.SerialNumber.cbData; ++index1)
            {
                if ((int)*numPtr++ != 0)
                {
                    version = 0;
                    break;
                }
            }
            this.Reset(version);
        }
Exemplo n.º 10
0
 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;
     }
     this.m_recipentInfoType   = recipientInfoType;
     this.m_recipientSubType   = recipientSubType;
     this.m_pCmsgRecipientInfo = pCmsgRecipientInfo;
     this.m_cmsgRecipientInfo  = cmsgRecipientInfo;
     this.m_index = index;
 }
Exemplo n.º 11
0
        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 SafeCertChainHandle ppChainContext)
        {
            if (pCertContext == null || pCertContext.IsInvalid)
            {
                throw new ArgumentException(SecurityResources.GetResourceString("Cryptography_InvalidContextHandle"), "pCertContext");
            }
            SafeCertStoreHandle hAdditionalStore = SafeCertStoreHandle.InvalidHandle;

            if (extraStore != null && extraStore.Count > 0)
            {
                hAdditionalStore = X509Utils.ExportToMemoryStore(extraStore);
            }
            CAPI.CERT_CHAIN_PARA pChainPara = new CAPI.CERT_CHAIN_PARA();
            pChainPara.cbSize = (uint)Marshal.SizeOf((object)pChainPara);
            SafeLocalAllocHandle localAllocHandle1 = SafeLocalAllocHandle.InvalidHandle;

            if (applicationPolicy != null && applicationPolicy.Count > 0)
            {
                pChainPara.RequestedUsage.dwType = 0U;
                pChainPara.RequestedUsage.Usage.cUsageIdentifier = (uint)applicationPolicy.Count;
                localAllocHandle1 = X509Utils.CopyOidsToUnmanagedMemory(applicationPolicy);
                pChainPara.RequestedUsage.Usage.rgpszUsageIdentifier = localAllocHandle1.DangerousGetHandle();
            }
            SafeLocalAllocHandle localAllocHandle2 = SafeLocalAllocHandle.InvalidHandle;

            if (certificatePolicy != null && certificatePolicy.Count > 0)
            {
                pChainPara.RequestedIssuancePolicy.dwType = 0U;
                pChainPara.RequestedIssuancePolicy.Usage.cUsageIdentifier = (uint)certificatePolicy.Count;
                localAllocHandle2 = X509Utils.CopyOidsToUnmanagedMemory(certificatePolicy);
                pChainPara.RequestedIssuancePolicy.Usage.rgpszUsageIdentifier = localAllocHandle2.DangerousGetHandle();
            }
            pChainPara.dwUrlRetrievalTimeout = (uint)timeout.Milliseconds;
            System.Runtime.InteropServices.ComTypes.FILETIME pTime = new System.Runtime.InteropServices.ComTypes.FILETIME();
            *(long *)&pTime = verificationTime.ToFileTime();
            uint dwFlags = X509Utils.MapRevocationFlags(revocationMode, revocationFlag);

            if (!CAPI.CAPISafe.CertGetCertificateChain(hChainEngine, pCertContext, ref pTime, hAdditionalStore, ref pChainPara, dwFlags, IntPtr.Zero, out ppChainContext))
            {
                return(Marshal.GetHRForLastWin32Error());
            }
            localAllocHandle1.Dispose();
            localAllocHandle2.Dispose();
            return(0);
        }
Exemplo n.º 12
0
        internal static unsafe SafeLocalAllocHandle CreateEncodedCertBlob(X509Certificate2Collection certificates)
        {
            SafeLocalAllocHandle localAllocHandle = SafeLocalAllocHandle.InvalidHandle;

            if (certificates.Count > 0)
            {
                localAllocHandle = CAPI.LocalAlloc(0U, new IntPtr(certificates.Count * Marshal.SizeOf(typeof(CAPI.CRYPTOAPI_BLOB))));
                CAPI.CRYPTOAPI_BLOB *cryptoapiBlobPtr = (CAPI.CRYPTOAPI_BLOB *)(void *) localAllocHandle.DangerousGetHandle();
                foreach (X509Certificate2 certificate in certificates)
                {
                    CAPI.CERT_CONTEXT certContext = *(CAPI.CERT_CONTEXT *)(void *) X509Utils.GetCertContext(certificate).DangerousGetHandle();
                    cryptoapiBlobPtr->cbData = certContext.cbCertEncoded;
                    cryptoapiBlobPtr->pbData = certContext.pbCertEncoded;
                    ++cryptoapiBlobPtr;
                }
            }
            return(localAllocHandle);
        }
Exemplo n.º 13
0
        internal static bool GetPrivateKeyInfo(System.Security.Cryptography.SafeCertContextHandle safeCertContext, ref CspParameters parameters)
        {
            SafeLocalAllocHandle invalidHandle = SafeLocalAllocHandle.InvalidHandle;
            uint pcbData = 0U;

            if (!CAPI.CAPISafe.CertGetCertificateContextProperty(safeCertContext, 2U, invalidHandle, out pcbData))
            {
                if (Marshal.GetLastWin32Error() == -2146885628)
                {
                    return(false);
                }
                else
                {
                    throw new CryptographicException(Marshal.GetLastWin32Error());
                }
            }
            else
            {
                SafeLocalAllocHandle pvData = CAPI.LocalAlloc(0U, new IntPtr((long)pcbData));
                if (!CAPI.CAPISafe.CertGetCertificateContextProperty(safeCertContext, 2U, pvData, out pcbData))
                {
                    if (Marshal.GetLastWin32Error() == -2146885628)
                    {
                        return(false);
                    }
                    else
                    {
                        throw new CryptographicException(Marshal.GetLastWin32Error());
                    }
                }
                else
                {
                    CAPI.CRYPT_KEY_PROV_INFO cryptKeyProvInfo = (CAPI.CRYPT_KEY_PROV_INFO)Marshal.PtrToStructure(pvData.DangerousGetHandle(), typeof(CAPI.CRYPT_KEY_PROV_INFO));
                    parameters.ProviderName     = cryptKeyProvInfo.pwszProvName;
                    parameters.KeyContainerName = cryptKeyProvInfo.pwszContainerName;
                    parameters.ProviderType     = (int)cryptKeyProvInfo.dwProvType;
                    parameters.KeyNumber        = (int)cryptKeyProvInfo.dwKeySpec;
                    parameters.Flags            = ((int)cryptKeyProvInfo.dwFlags & 32) == 32 ? CspProviderFlags.UseMachineKeyStore : CspProviderFlags.NoFlags;
                    pvData.Dispose();
                    return(true);
                }
            }
        }
Exemplo n.º 14
0
        internal SignerInfo(SignedCms signedCms, SignerInfo parentSignerInfo, byte[] encodedSignerInfo)
        {
            uint cbDecodedValue = 0U;
            SafeLocalAllocHandle decodedValue = SafeLocalAllocHandle.InvalidHandle;

            fixed(byte *numPtr = &encodedSignerInfo[0])
            {
                if (!CAPI.DecodeObject(new IntPtr(500L), new IntPtr((void *)numPtr), (uint)encodedSignerInfo.Length, out decodedValue, out cbDecodedValue))
                {
                    throw new CryptographicException(Marshal.GetLastWin32Error());
                }
            }

            this.m_signedCms         = signedCms;
            this.m_parentSignerInfo  = parentSignerInfo;
            this.m_encodedSignerInfo = (byte[])encodedSignerInfo.Clone();
            this.m_pbCmsgSignerInfo  = decodedValue;
            this.m_cmsgSignerInfo    = (CAPI.CMSG_SIGNER_INFO)Marshal.PtrToStructure(decodedValue.DangerousGetHandle(), typeof(CAPI.CMSG_SIGNER_INFO));
        }
Exemplo n.º 15
0
        internal static unsafe int GetMaxKeyLength(SafeCryptProvHandle safeCryptProvHandle, uint algId)
        {
            uint dwFlags = 1U;
            uint num     = (uint)Marshal.SizeOf(typeof(CAPI.PROV_ENUMALGS_EX));
            SafeLocalAllocHandle localAllocHandle = CAPI.LocalAlloc(64U, new IntPtr(Marshal.SizeOf(typeof(CAPI.PROV_ENUMALGS_EX))));

            using (localAllocHandle)
            {
                for (; CAPI.CAPISafe.CryptGetProvParam(safeCryptProvHandle, 22U, localAllocHandle.DangerousGetHandle(), new IntPtr((void *)&num), dwFlags); dwFlags = 0U)
                {
                    CAPI.PROV_ENUMALGS_EX provEnumalgsEx = (CAPI.PROV_ENUMALGS_EX)Marshal.PtrToStructure(localAllocHandle.DangerousGetHandle(), typeof(CAPI.PROV_ENUMALGS_EX));
                    if ((int)provEnumalgsEx.aiAlgid == (int)algId)
                    {
                        return((int)provEnumalgsEx.dwMaxLen);
                    }
                }
            }
            throw new CryptographicException(-2146889726);
        }
Exemplo n.º 16
0
        internal static unsafe AlgorithmIdentifier GetAlgorithmIdentifier(SafeCryptMsgHandle safeCryptMsgHandle)
        {
            AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier();
            uint num = 0U;

            if (!CAPI.CAPISafe.CryptMsgGetParam(safeCryptMsgHandle, 15U, 0U, IntPtr.Zero, new IntPtr((void *)&num)))
            {
                PkcsUtils.checkErr(Marshal.GetLastWin32Error());
            }
            if (num > 0U)
            {
                SafeLocalAllocHandle pvData = CAPI.LocalAlloc(0U, new IntPtr((long)num));
                if (!CAPI.CAPISafe.CryptMsgGetParam(safeCryptMsgHandle, 15U, 0U, pvData, new IntPtr((void *)&num)))
                {
                    PkcsUtils.checkErr(Marshal.GetLastWin32Error());
                }
                algorithmIdentifier = new AlgorithmIdentifier((CAPI.CRYPT_ALGORITHM_IDENTIFIER)Marshal.PtrToStructure(pvData.DangerousGetHandle(), typeof(CAPI.CRYPT_ALGORITHM_IDENTIFIER)));
                pvData.Dispose();
            }
            return(algorithmIdentifier);
        }
Exemplo n.º 17
0
        internal static string DecodeOctetString(byte[] encodedOctetString)
        {
            uint cbDecodedValue = 0U;
            SafeLocalAllocHandle decodedValue = (SafeLocalAllocHandle)null;

            if (!CAPI.DecodeObject(new IntPtr(25L), encodedOctetString, out decodedValue, out cbDecodedValue))
            {
                throw new CryptographicException(Marshal.GetLastWin32Error());
            }
            if ((int)cbDecodedValue == 0)
            {
                return(string.Empty);
            }
            CAPI.CRYPTOAPI_BLOB cryptoapiBlob = (CAPI.CRYPTOAPI_BLOB)Marshal.PtrToStructure(decodedValue.DangerousGetHandle(), typeof(CAPI.CRYPTOAPI_BLOB));
            if ((int)cryptoapiBlob.cbData == 0)
            {
                return(string.Empty);
            }
            string str = Marshal.PtrToStringUni(cryptoapiBlob.pbData);

            decodedValue.Dispose();
            return(str);
        }
Exemplo n.º 18
0
        internal static unsafe CryptographicAttributeObjectCollection GetUnprotectedAttributes(SafeCryptMsgHandle safeCryptMsgHandle)
        {
            uint num = 0U;
            CryptographicAttributeObjectCollection objectCollection = new CryptographicAttributeObjectCollection();
            SafeLocalAllocHandle invalidHandle = SafeLocalAllocHandle.InvalidHandle;

            if (!CAPI.CAPISafe.CryptMsgGetParam(safeCryptMsgHandle, 37U, 0U, invalidHandle, new IntPtr((void *)&num)) && Marshal.GetLastWin32Error() != -2146889713)
            {
                PkcsUtils.checkErr(Marshal.GetLastWin32Error());
            }
            if (num > 0U)
            {
                SafeLocalAllocHandle localAllocHandle;
                using (localAllocHandle = CAPI.LocalAlloc(64U, new IntPtr((long)num)))
                {
                    if (!CAPI.CAPISafe.CryptMsgGetParam(safeCryptMsgHandle, 37U, 0U, localAllocHandle, new IntPtr((void *)&num)))
                    {
                        PkcsUtils.checkErr(Marshal.GetLastWin32Error());
                    }
                    objectCollection = new CryptographicAttributeObjectCollection(localAllocHandle);
                }
            }
            return(objectCollection);
        }
Exemplo n.º 19
0
        internal static unsafe X509IssuerSerial DecodeIssuerSerial(CAPI.CERT_ISSUER_SERIAL_NUMBER pIssuerAndSerial)
        {
            SafeLocalAllocHandle invalidHandle = SafeLocalAllocHandle.InvalidHandle;
            uint csz = CAPI.CAPISafe.CertNameToStrW(65537U, new IntPtr((void *)&pIssuerAndSerial.Issuer), 33554435U, invalidHandle, 0U);

            if (csz <= 1U)
            {
                throw new CryptographicException(Marshal.GetLastWin32Error());
            }
            SafeLocalAllocHandle psz = CAPI.LocalAlloc(0U, new IntPtr((long)(2U * csz)));

            if (CAPI.CAPISafe.CertNameToStrW(65537U, new IntPtr((void *)&pIssuerAndSerial.Issuer), 33554435U, psz, csz) <= 1U)
            {
                throw new CryptographicException(Marshal.GetLastWin32Error());
            }
            X509IssuerSerial x509IssuerSerial = new X509IssuerSerial();

            x509IssuerSerial.IssuerName = Marshal.PtrToStringUni(psz.DangerousGetHandle());
            byte[] numArray = new byte[(IntPtr)pIssuerAndSerial.SerialNumber.cbData];
            Marshal.Copy(pIssuerAndSerial.SerialNumber.pbData, numArray, 0, numArray.Length);
            x509IssuerSerial.SerialNumber = X509Utils.EncodeHexStringFromInt(numArray);
            psz.Dispose();
            return(x509IssuerSerial);
        }
Exemplo n.º 20
0
 internal KeyAgreeRecipientInfo(SafeLocalAllocHandle pRecipientInfo, CAPI.CMSG_KEY_AGREE_PUBLIC_KEY_RECIPIENT_INFO publicKeyRecipient, uint index, uint subIndex)
     : base(RecipientInfoType.KeyAgreement, RecipientSubType.PublicKeyAgreement, pRecipientInfo, (object)publicKeyRecipient, index)
 {
     CAPI.CMSG_RECIPIENT_ENCRYPTED_KEY_INFO encryptedKeyInfo = (CAPI.CMSG_RECIPIENT_ENCRYPTED_KEY_INFO)Marshal.PtrToStructure(Marshal.ReadIntPtr(new IntPtr((long)publicKeyRecipient.rgpRecipientEncryptedKeys + (long)subIndex * (long)Marshal.SizeOf(typeof(IntPtr)))), typeof(CAPI.CMSG_RECIPIENT_ENCRYPTED_KEY_INFO));
     this.Reset(2U, publicKeyRecipient.dwVersion, encryptedKeyInfo, subIndex);
 }
Exemplo n.º 21
0
 internal static unsafe void GetParam(SafeCryptMsgHandle safeCryptMsgHandle, uint paramType, uint index, out SafeLocalAllocHandle pvData, out uint cbData)
 {
     cbData = 0U;
     pvData = SafeLocalAllocHandle.InvalidHandle;
     fixed(uint *numPtr = &cbData)
     {
         if (!CAPI.CAPISafe.CryptMsgGetParam(safeCryptMsgHandle, paramType, index, pvData, new IntPtr((void *)numPtr)))
         {
             PkcsUtils.checkErr(Marshal.GetLastWin32Error());
         }
         if (cbData > 0U)
         {
             pvData = CAPI.LocalAlloc(64U, new IntPtr((long)cbData));
             if (!CAPI.CAPISafe.CryptMsgGetParam(safeCryptMsgHandle, paramType, index, pvData, new IntPtr((void *)numPtr)))
             {
                 PkcsUtils.checkErr(Marshal.GetLastWin32Error());
             }
         }
     }
 }
Exemplo n.º 22
0
 internal KeyTransRecipientInfo(SafeLocalAllocHandle pRecipientInfo, CAPI.CMSG_KEY_TRANS_RECIPIENT_INFO keyTrans, uint index)
     : base(RecipientInfoType.KeyTransport, RecipientSubType.CmsKeyTransport, pRecipientInfo, (object)keyTrans, index)
 {
     this.Reset((int)keyTrans.dwVersion);
 }
Exemplo n.º 23
0
        private unsafe void RemoveCounterSignature(int parentIndex, int childIndex)
        {
            if (parentIndex < 0)
            {
                throw new ArgumentOutOfRangeException("parentIndex");
            }
            if (childIndex < 0)
            {
                throw new ArgumentOutOfRangeException("childIndex");
            }
            uint cbData1 = 0U;
            SafeLocalAllocHandle pvData1 = SafeLocalAllocHandle.InvalidHandle;
            uint cbData2 = 0U;
            SafeLocalAllocHandle pvData2      = SafeLocalAllocHandle.InvalidHandle;
            IntPtr             num1           = IntPtr.Zero;
            SafeCryptMsgHandle cryptMsgHandle = this.m_signedCms.GetCryptMsgHandle();
            uint num2;

            if (PkcsUtils.CmsSupported())
            {
                PkcsUtils.GetParam(cryptMsgHandle, 39U, (uint)parentIndex, out pvData1, out cbData1);
                CAPI.CMSG_CMS_SIGNER_INFO cmsgCmsSignerInfo = (CAPI.CMSG_CMS_SIGNER_INFO)Marshal.PtrToStructure(pvData1.DangerousGetHandle(), typeof(CAPI.CMSG_CMS_SIGNER_INFO));
                num2 = cmsgCmsSignerInfo.UnauthAttrs.cAttr;
                num1 = new IntPtr((long)cmsgCmsSignerInfo.UnauthAttrs.rgAttr);
            }
            else
            {
                PkcsUtils.GetParam(cryptMsgHandle, 6U, (uint)parentIndex, out pvData2, out cbData2);
                CAPI.CMSG_SIGNER_INFO cmsgSignerInfo = (CAPI.CMSG_SIGNER_INFO)Marshal.PtrToStructure(pvData2.DangerousGetHandle(), typeof(CAPI.CMSG_SIGNER_INFO));
                num2 = cmsgSignerInfo.UnauthAttrs.cAttr;
                num1 = new IntPtr((long)cmsgSignerInfo.UnauthAttrs.rgAttr);
            }
            for (uint index = 0U; index < num2; ++index)
            {
                CAPI.CRYPT_ATTRIBUTE cryptAttribute1 = (CAPI.CRYPT_ATTRIBUTE)Marshal.PtrToStructure(num1, typeof(CAPI.CRYPT_ATTRIBUTE));
                if (string.Compare(cryptAttribute1.pszObjId, "1.2.840.113549.1.9.6", StringComparison.OrdinalIgnoreCase) == 0 && cryptAttribute1.cValue > 0U)
                {
                    if (childIndex < (int)cryptAttribute1.cValue)
                    {
                        if (!CAPI.CryptMsgControl(cryptMsgHandle, 0U, 9U, new IntPtr((void *)&new CAPI.CMSG_CTRL_DEL_SIGNER_UNAUTH_ATTR_PARA(Marshal.SizeOf(typeof(CAPI.CMSG_CTRL_DEL_SIGNER_UNAUTH_ATTR_PARA)))
                        {
                            dwSignerIndex = (uint)parentIndex,
                            dwUnauthAttrIndex = index
                        })))
                        {
                            throw new CryptographicException(Marshal.GetLastWin32Error());
                        }
                        if (cryptAttribute1.cValue <= 1U)
                        {
                            return;
                        }
                        try
                        {
                            SafeLocalAllocHandle localAllocHandle1 = CAPI.LocalAlloc(64U, new IntPtr((long)(uint)((ulong)(cryptAttribute1.cValue - 1U) * (ulong)Marshal.SizeOf(typeof(CAPI.CRYPTOAPI_BLOB)))));
                            CAPI.CRYPTOAPI_BLOB *cryptoapiBlobPtr1 = (CAPI.CRYPTOAPI_BLOB *)(void *) cryptAttribute1.rgValue;
                            CAPI.CRYPTOAPI_BLOB *cryptoapiBlobPtr2 = (CAPI.CRYPTOAPI_BLOB *)(void *) localAllocHandle1.DangerousGetHandle();
                            int num3 = 0;
                            while (num3 < (int)cryptAttribute1.cValue)
                            {
                                if (num3 != childIndex)
                                {
                                    *cryptoapiBlobPtr2 = *cryptoapiBlobPtr1;
                                }
                                ++num3;
                                ++cryptoapiBlobPtr1;
                                ++cryptoapiBlobPtr2;
                            }
                            CAPI.CRYPT_ATTRIBUTE cryptAttribute2 = new CAPI.CRYPT_ATTRIBUTE();
                            cryptAttribute2.pszObjId = cryptAttribute1.pszObjId;
                            cryptAttribute2.cValue   = cryptAttribute1.cValue - 1U;
                            cryptAttribute2.rgValue  = localAllocHandle1.DangerousGetHandle();
                            SafeLocalAllocHandle localAllocHandle2 = CAPI.LocalAlloc(64U, new IntPtr(Marshal.SizeOf(typeof(CAPI.CRYPT_ATTRIBUTE))));
                            Marshal.StructureToPtr((object)cryptAttribute2, localAllocHandle2.DangerousGetHandle(), false);
                            byte[] encodedData;
                            try
                            {
                                if (!CAPI.EncodeObject(new IntPtr(22L), localAllocHandle2.DangerousGetHandle(), out encodedData))
                                {
                                    throw new CryptographicException(Marshal.GetLastWin32Error());
                                }
                            }
                            finally
                            {
                                Marshal.DestroyStructure(localAllocHandle2.DangerousGetHandle(), typeof(CAPI.CRYPT_ATTRIBUTE));
                                localAllocHandle2.Dispose();
                            }
                            fixed(byte *numPtr = &encodedData[0])
                            {
                                if (!CAPI.CryptMsgControl(cryptMsgHandle, 0U, 8U, new IntPtr((void *)&new CAPI.CMSG_CTRL_ADD_SIGNER_UNAUTH_ATTR_PARA(Marshal.SizeOf(typeof(CAPI.CMSG_CTRL_ADD_SIGNER_UNAUTH_ATTR_PARA)))
                                {
                                    dwSignerIndex = (uint)parentIndex,
                                    blob =
                                    {
                                        cbData = (uint)encodedData.Length,
                                        pbData = new IntPtr((void *)numPtr)
                                    }
                                })))
                                {
                                    throw new CryptographicException(Marshal.GetLastWin32Error());
                                }
                            }
                            localAllocHandle1.Dispose();
                            return;
                        }
                        catch (CryptographicException ex)
                        {
                            byte[] encodedData;
                            if (CAPI.EncodeObject(new IntPtr(22L), num1, out encodedData))
                            {
                                fixed(byte *numPtr = &encodedData[0])
                                CAPI.CryptMsgControl(cryptMsgHandle, 0U, 8U, new IntPtr((void *)&new CAPI.CMSG_CTRL_ADD_SIGNER_UNAUTH_ATTR_PARA(Marshal.SizeOf(typeof(CAPI.CMSG_CTRL_ADD_SIGNER_UNAUTH_ATTR_PARA)))
                                {
                                    dwSignerIndex = (uint)parentIndex,
                                    blob          =
                                    {
                                        cbData = (uint)encodedData.Length,
                                        pbData = new IntPtr((void *)numPtr)
                                    }
                                }));
                            }
                            throw;
                        }
                    }
                    else
                    {
                        childIndex -= (int)cryptAttribute1.cValue;
                    }
                }
                num1 = new IntPtr((long)num1 + (long)Marshal.SizeOf(typeof(CAPI.CRYPT_ATTRIBUTE)));
            }
            if (pvData1 != null && !pvData1.IsInvalid)
            {
                pvData1.Dispose();
            }
            if (pvData2 != null && !pvData2.IsInvalid)
            {
                pvData2.Dispose();
            }
            throw new CryptographicException(-2146885618);
        }
Exemplo n.º 24
0
        private unsafe void Verify(X509Certificate2Collection extraStore, X509Certificate2 certificate, bool verifySignatureOnly)
        {
            SafeLocalAllocHandle pvData1 = SafeLocalAllocHandle.InvalidHandle;

            CAPI.CERT_CONTEXT certContext = (CAPI.CERT_CONTEXT)Marshal.PtrToStructure(X509Utils.GetCertContext(certificate).DangerousGetHandle(), typeof(CAPI.CERT_CONTEXT));
            IntPtr            ptr1        = new IntPtr((long)new IntPtr((long)certContext.pCertInfo + (long)Marshal.OffsetOf(typeof(CAPI.CERT_INFO), "SubjectPublicKeyInfo")) + (long)Marshal.OffsetOf(typeof(CAPI.CERT_PUBLIC_KEY_INFO), "Algorithm"));
            IntPtr            num1        = new IntPtr((long)ptr1 + (long)Marshal.OffsetOf(typeof(CAPI.CRYPT_ALGORITHM_IDENTIFIER), "Parameters"));

            if ((int)CAPI.CryptFindOIDInfo(1U, Marshal.ReadIntPtr(ptr1), 3U).Algid == 8704)
            {
                bool   flag = false;
                IntPtr ptr2 = new IntPtr((long)num1 + (long)Marshal.OffsetOf(typeof(CAPI.CRYPTOAPI_BLOB), "cbData"));
                IntPtr ptr3 = new IntPtr((long)num1 + (long)Marshal.OffsetOf(typeof(CAPI.CRYPTOAPI_BLOB), "pbData"));
                if (Marshal.ReadInt32(ptr2) == 0)
                {
                    flag = true;
                }
                else if (Marshal.ReadIntPtr(ptr3) == IntPtr.Zero)
                {
                    flag = true;
                }
                else if (Marshal.ReadInt32(Marshal.ReadIntPtr(ptr3)) == 5)
                {
                    flag = true;
                }
                if (flag)
                {
                    SafeCertChainHandle invalidHandle = SafeCertChainHandle.InvalidHandle;
                    X509Utils.BuildChain(new IntPtr(0L), X509Utils.GetCertContext(certificate), (X509Certificate2Collection)null, (OidCollection)null, (OidCollection)null, X509RevocationMode.NoCheck, X509RevocationFlag.ExcludeRoot, DateTime.Now, new TimeSpan(0, 0, 0), ref invalidHandle);
                    invalidHandle.Dispose();
                    uint pcbData = 0U;
                    if (!CAPI.CAPISafe.CertGetCertificateContextProperty(X509Utils.GetCertContext(certificate), 22U, pvData1, out pcbData))
                    {
                        throw new CryptographicException(Marshal.GetLastWin32Error());
                    }
                    if (pcbData > 0U)
                    {
                        pvData1 = CAPI.LocalAlloc(64U, new IntPtr((long)pcbData));
                        if (!CAPI.CAPISafe.CertGetCertificateContextProperty(X509Utils.GetCertContext(certificate), 22U, pvData1, out pcbData))
                        {
                            throw new CryptographicException(Marshal.GetLastWin32Error());
                        }
                        Marshal.WriteInt32(ptr2, (int)pcbData);
                        Marshal.WriteIntPtr(ptr3, pvData1.DangerousGetHandle());
                    }
                }
            }
            if (this.m_parentSignerInfo == null)
            {
                if (!CAPI.CryptMsgControl(this.m_signedCms.GetCryptMsgHandle(), 0U, 1U, certContext.pCertInfo))
                {
                    throw new CryptographicException(Marshal.GetLastWin32Error());
                }
            }
            else
            {
                int num2 = -1;
                int hr   = 0;
                SafeLocalAllocHandle pvData2;
                while (true)
                {
                    try
                    {
                        num2 = PkcsUtils.GetSignerIndex(this.m_signedCms.GetCryptMsgHandle(), this.m_parentSignerInfo, num2 + 1);
                    }
                    catch (CryptographicException ex)
                    {
                        if (hr != 0)
                        {
                            throw new CryptographicException(hr);
                        }
                        throw;
                    }
                    uint cbData = 0U;
                    pvData2 = SafeLocalAllocHandle.InvalidHandle;
                    PkcsUtils.GetParam(this.m_signedCms.GetCryptMsgHandle(), 28U, (uint)num2, out pvData2, out cbData);
                    if ((int)cbData == 0)
                    {
                        hr = -2146885618;
                    }
                    else
                    {
                        fixed(byte *numPtr = this.m_encodedSignerInfo)
                        {
                            if (!CAPI.CAPISafe.CryptMsgVerifyCountersignatureEncoded(IntPtr.Zero, 65537U, pvData2.DangerousGetHandle(), cbData, new IntPtr((void *)numPtr), (uint)this.m_encodedSignerInfo.Length, certContext.pCertInfo))
                            {
                                hr = Marshal.GetLastWin32Error();
                            }
                            else
                            {
                                break;
                            }
                        }
                    }
                }
                // ISSUE: fixed variable is out of scope
                // ISSUE: __unpin statement
                __unpin(numPtr);
                pvData2.Dispose();
            }
            if (!verifySignatureOnly)
            {
                int hr = SignerInfo.VerifyCertificate(certificate, extraStore);
                if (hr != 0)
                {
                    throw new CryptographicException(hr);
                }
            }
            pvData1.Dispose();
        }
Exemplo n.º 25
0
        internal AlgorithmIdentifier(CAPI.CRYPT_ALGORITHM_IDENTIFIER algorithmIdentifier)
        {
            int  keyLength      = 0;
            uint cbDecodedValue = 0U;
            SafeLocalAllocHandle decodedValue = SafeLocalAllocHandle.InvalidHandle;

            byte[] numArray = new byte[0];
            uint   num      = X509Utils.OidToAlgId(algorithmIdentifier.pszObjId);

            switch (num)
            {
            case 26114U:
                if (algorithmIdentifier.Parameters.cbData > 0U)
                {
                    if (!CAPI.DecodeObject(new IntPtr(41L), algorithmIdentifier.Parameters.pbData, algorithmIdentifier.Parameters.cbData, out decodedValue, out cbDecodedValue))
                    {
                        throw new CryptographicException(Marshal.GetLastWin32Error());
                    }
                    CAPI.CRYPT_RC2_CBC_PARAMETERS rc2CbcParameters = (CAPI.CRYPT_RC2_CBC_PARAMETERS)Marshal.PtrToStructure(decodedValue.DangerousGetHandle(), typeof(CAPI.CRYPT_RC2_CBC_PARAMETERS));
                    switch (rc2CbcParameters.dwVersion)
                    {
                    case 52U:
                        keyLength = 56;
                        break;

                    case 58U:
                        keyLength = 128;
                        break;

                    case 160U:
                        keyLength = 40;
                        break;
                    }
                    if (rc2CbcParameters.fIV)
                    {
                        numArray = (byte[])rc2CbcParameters.rgbIV.Clone();
                        break;
                    }
                    else
                    {
                        break;
                    }
                }
                else
                {
                    break;
                }

            case 26625U:
            case 26113U:
            case 26115U:
                if (algorithmIdentifier.Parameters.cbData > 0U)
                {
                    if (!CAPI.DecodeObject(new IntPtr(25L), algorithmIdentifier.Parameters.pbData, algorithmIdentifier.Parameters.cbData, out decodedValue, out cbDecodedValue))
                    {
                        throw new CryptographicException(Marshal.GetLastWin32Error());
                    }
                    if (cbDecodedValue > 0U)
                    {
                        if ((int)num == 26625)
                        {
                            CAPI.CRYPTOAPI_BLOB cryptoapiBlob = (CAPI.CRYPTOAPI_BLOB)Marshal.PtrToStructure(decodedValue.DangerousGetHandle(), typeof(CAPI.CRYPTOAPI_BLOB));
                            if (cryptoapiBlob.cbData > 0U)
                            {
                                numArray = new byte[(IntPtr)cryptoapiBlob.cbData];
                                Marshal.Copy(cryptoapiBlob.pbData, numArray, 0, numArray.Length);
                            }
                        }
                        else
                        {
                            numArray = new byte[(IntPtr)cbDecodedValue];
                            Marshal.Copy(decodedValue.DangerousGetHandle(), numArray, 0, numArray.Length);
                        }
                    }
                }
                keyLength = (int)num != 26625 ? ((int)num != 26113 ? 192 : 64) : 128 - numArray.Length * 8;
                break;

            default:
                if (algorithmIdentifier.Parameters.cbData > 0U)
                {
                    numArray = new byte[(IntPtr)algorithmIdentifier.Parameters.cbData];
                    Marshal.Copy(algorithmIdentifier.Parameters.pbData, numArray, 0, numArray.Length);
                    break;
                }
                else
                {
                    break;
                }
            }
            this.Reset(new Oid(algorithmIdentifier.pszObjId), keyLength, numArray);
            decodedValue.Dispose();
        }
Exemplo n.º 26
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);
            }
        }
Exemplo n.º 27
0
        internal SubjectIdentifier(CAPI.CRYPTOAPI_BLOB issuer, CAPI.CRYPTOAPI_BLOB serialNumber)
        {
            bool  flag   = true;
            byte *numPtr = (byte *)(void *)serialNumber.pbData;

            for (uint index = 0U; index < serialNumber.cbData; ++index)
            {
                if ((int)*numPtr++ != 0)
                {
                    flag = false;
                    break;
                }
            }
            if (flag)
            {
                byte[] numArray = new byte[(IntPtr)issuer.cbData];
                Marshal.Copy(issuer.pbData, numArray, 0, numArray.Length);
                if (string.Compare("CN=Dummy Signer", new X500DistinguishedName(numArray).Name, StringComparison.OrdinalIgnoreCase) == 0)
                {
                    this.Reset(SubjectIdentifierType.NoSignature, (object)null);
                    return;
                }
            }
            if (flag)
            {
                this.m_type  = SubjectIdentifierType.SubjectKeyIdentifier;
                this.m_value = (object)string.Empty;
                uint cbDecodedValue = 0U;
                SafeLocalAllocHandle decodedValue = SafeLocalAllocHandle.InvalidHandle;
                if (!CAPI.DecodeObject(new IntPtr(7L), issuer.pbData, issuer.cbData, out decodedValue, out cbDecodedValue))
                {
                    throw new CryptographicException(Marshal.GetLastWin32Error());
                }
                using (decodedValue)
                {
                    CAPI.CERT_NAME_INFO certNameInfo = (CAPI.CERT_NAME_INFO)Marshal.PtrToStructure(decodedValue.DangerousGetHandle(), typeof(CAPI.CERT_NAME_INFO));
                    for (uint index1 = 0U; index1 < certNameInfo.cRDN; ++index1)
                    {
                        CAPI.CERT_RDN certRdn = (CAPI.CERT_RDN)Marshal.PtrToStructure(new IntPtr((long)certNameInfo.rgRDN + (long)index1 * (long)Marshal.SizeOf(typeof(CAPI.CERT_RDN))), typeof(CAPI.CERT_RDN));
                        for (uint index2 = 0U; index2 < certRdn.cRDNAttr; ++index2)
                        {
                            CAPI.CERT_RDN_ATTR certRdnAttr = (CAPI.CERT_RDN_ATTR)Marshal.PtrToStructure(new IntPtr((long)certRdn.rgRDNAttr + (long)index2 * (long)Marshal.SizeOf(typeof(CAPI.CERT_RDN_ATTR))), typeof(CAPI.CERT_RDN_ATTR));
                            if (string.Compare("1.3.6.1.4.1.311.10.7.1", certRdnAttr.pszObjId, StringComparison.OrdinalIgnoreCase) == 0 && (int)certRdnAttr.dwValueType == 2)
                            {
                                byte[] numArray = new byte[(IntPtr)certRdnAttr.Value.cbData];
                                Marshal.Copy(certRdnAttr.Value.pbData, numArray, 0, numArray.Length);
                                this.Reset(SubjectIdentifierType.SubjectKeyIdentifier, (object)X509Utils.EncodeHexString(numArray));
                                return;
                            }
                        }
                    }
                }
                throw new CryptographicException(-2146889715);
            }
            else
            {
                CAPI.CERT_ISSUER_SERIAL_NUMBER pIssuerAndSerial;
                pIssuerAndSerial.Issuer       = issuer;
                pIssuerAndSerial.SerialNumber = serialNumber;
                this.Reset(SubjectIdentifierType.IssuerAndSerialNumber, (object)PkcsUtils.DecodeIssuerSerial(pIssuerAndSerial));
            }
        }