Beispiel #1
0
        internal unsafe void AddCountersignature(CmsSigner cmsSigner, CngKey privateKey)
        {
            using (var hb = new HeapBlockRetainer())
            {
                var signerInfo = NativeUtility.CreateSignerInfo(cmsSigner, privateKey, hb);

                NativeUtility.ThrowIfFailed(NativeMethods.CryptMsgCountersign(
                                                _handle,
                                                dwIndex: 0,
                                                cCountersigners: 1,
                                                rgCountersigners: signerInfo));

                AddCertificates(cmsSigner.Certificates.OfType <X509Certificate2>());
            }
        }
Beispiel #2
0
        private byte[] GetByteArrayAttribute(CMSG_GETPARAM_TYPE param, uint index)
        {
            uint valueLength = 0;

            NativeUtility.ThrowIfFailed(NativeMethods.CryptMsgGetParam(
                                            _handle,
                                            param,
                                            index,
                                            null,
                                            ref valueLength));

            var data = new byte[(int)valueLength];

            NativeUtility.ThrowIfFailed(NativeMethods.CryptMsgGetParam(
                                            _handle,
                                            param,
                                            index,
                                            data,
                                            ref valueLength));

            return(data);
        }
Beispiel #3
0
        private static PrimarySignature CreatePrimarySignature(CmsSigner cmsSigner, byte[] signingData, CngKey privateKey)
        {
            var cms = NativeUtility.NativeSign(cmsSigner, signingData, privateKey);

            return(PrimarySignature.Load(cms));
        }
Beispiel #4
0
        private unsafe RepositoryCounterSignerInfo?GetRepositoryCountersignature(HeapBlockRetainer retainer)
        {
            const uint primarySignerInfoIndex = 0;
            uint       unsignedAttributeCount = 0;
            var        pointer = IntPtr.Zero;

            NativeUtility.ThrowIfFailed(NativeMethods.CryptMsgGetParam(
                                            _handle,
                                            CMSG_GETPARAM_TYPE.CMSG_SIGNER_UNAUTH_ATTR_PARAM,
                                            primarySignerInfoIndex,
                                            pointer,
                                            ref unsignedAttributeCount));

            if (unsignedAttributeCount == 0)
            {
                return(null);
            }

            pointer = retainer.Alloc((int)unsignedAttributeCount);

            NativeUtility.ThrowIfFailed(NativeMethods.CryptMsgGetParam(
                                            _handle,
                                            CMSG_GETPARAM_TYPE.CMSG_SIGNER_UNAUTH_ATTR_PARAM,
                                            primarySignerInfoIndex,
                                            pointer,
                                            ref unsignedAttributeCount));

            var unsignedAttributes         = MarshalUtility.PtrToStructure <CRYPT_ATTRIBUTES>(pointer);
            int sizeOfCryptAttributeString = MarshalUtility.SizeOf <CRYPT_ATTRIBUTE_STRING>();
            int sizeOfCryptIntegerBlob     = MarshalUtility.SizeOf <CRYPT_INTEGER_BLOB>();

            for (uint i = 0; i < unsignedAttributes.cAttr; ++i)
            {
                var attributePointer = new IntPtr(
                    (long)unsignedAttributes.rgAttr + (i * sizeOfCryptAttributeString));
                var attribute = MarshalUtility.PtrToStructure <CRYPT_ATTRIBUTE_STRING>(attributePointer);

                if (!string.Equals(attribute.pszObjId, Oids.Countersignature, StringComparison.Ordinal))
                {
                    continue;
                }

                for (var j = 0; j < attribute.cValue; ++j)
                {
                    var attributeValuePointer = new IntPtr(
                        (long)attribute.rgValue + (j * sizeOfCryptIntegerBlob));
                    var  attributeValue = MarshalUtility.PtrToStructure <CRYPT_INTEGER_BLOB>(attributeValuePointer);
                    uint cbSignerInfo   = 0;

                    NativeUtility.ThrowIfFailed(NativeMethods.CryptDecodeObject(
                                                    CMSG_ENCODING.Any,
                                                    new IntPtr(NativeMethods.PKCS7_SIGNER_INFO),
                                                    attributeValue.pbData,
                                                    attributeValue.cbData,
                                                    dwFlags: 0,
                                                    pvStructInfo: IntPtr.Zero,
                                                    pcbStructInfo: new IntPtr(&cbSignerInfo)));

                    var counterSignerInfoPointer = retainer.Alloc((int)cbSignerInfo);

                    NativeUtility.ThrowIfFailed(NativeMethods.CryptDecodeObject(
                                                    CMSG_ENCODING.Any,
                                                    new IntPtr(NativeMethods.PKCS7_SIGNER_INFO),
                                                    attributeValue.pbData,
                                                    attributeValue.cbData,
                                                    dwFlags: 0,
                                                    pvStructInfo: counterSignerInfoPointer,
                                                    pcbStructInfo: new IntPtr(&cbSignerInfo)));

                    var counterSignerInfo = MarshalUtility.PtrToStructure <CMSG_SIGNER_INFO>(counterSignerInfoPointer);

                    if (IsRepositoryCounterSignerInfo(counterSignerInfo))
                    {
                        return(new RepositoryCounterSignerInfo()
                        {
                            dwUnauthAttrIndex = i,
                            UnauthAttr = attribute,
                            SignerInfo = counterSignerInfo
                        });
                    }
                }
            }

            return(null);
        }
Beispiel #5
0
 public void Dispose()
 {
     NativeUtility.SafeFree(pbData);
 }