Ejemplo n.º 1
0
        internal unsafe void AddCountersignature(CmsSigner cmsSigner, CngKey privateKey)
        {
            using (var hb = new HeapBlockRetainer())
            {
                var signerInfo = NativeUtilities.CreateSignerInfo(cmsSigner, privateKey, hb);

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

                AddCertificates(CertificateUtility.GetRawDataForCollection(cmsSigner.Certificates));
            }
        }
Ejemplo n.º 2
0
        private byte[] GetByteArrayAttribute(CMSG_GETPARAM_TYPE param, uint index)
        {
            uint valueLength = 0;

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

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

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

            return(data);
        }
Ejemplo n.º 3
0
 public void Dispose()
 {
     NativeUtilities.SafeFree(pbData);
 }
Ejemplo n.º 4
0
        private Signature CreateSignature(CmsSigner cmsSigner, SignatureContent signatureContent, CngKey privateKey)
        {
            var cms = NativeUtilities.NativeSign(cmsSigner, signatureContent.GetBytes(), privateKey);

            return(Signature.Load(cms));
        }
Ejemplo n.º 5
0
        private static PrimarySignature CreatePrimarySignature(CmsSigner cmsSigner, byte[] signingData, CngKey privateKey)
        {
            var cms = NativeUtilities.NativeSign(cmsSigner, signingData, privateKey);

            return(PrimarySignature.Load(cms));
        }
Ejemplo n.º 6
0
        private unsafe CMSG_SIGNER_INFO?GetRepositoryCountersignature()
        {
            const uint primarySignerInfoIndex = 0;
            uint       unsignedAttributeCount = 0;
            var        pointer = IntPtr.Zero;

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

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

            using (var retainer = new HeapBlockRetainer())
            {
                pointer = retainer.Alloc((int)unsignedAttributeCount);

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

                var unsignedAttributes = Marshal.PtrToStructure <CRYPT_ATTRIBUTES>(pointer);

                for (var i = 0; i < unsignedAttributes.cAttr; ++i)
                {
                    var attributePointer = new IntPtr(
                        (long)unsignedAttributes.rgAttr + (i * Marshal.SizeOf <CRYPT_ATTRIBUTE_STRING>()));
                    var attribute = Marshal.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 * Marshal.SizeOf <CRYPT_INTEGER_BLOB>()));
                        var  attributeValue = Marshal.PtrToStructure <CRYPT_INTEGER_BLOB>(attributeValuePointer);
                        uint cbSignerInfo   = 0;

                        NativeUtilities.ThrowIfFailed(NativeMethods.CryptDecodeObject(
                                                          NativeMethods.X509_ASN_ENCODING | NativeMethods.PKCS_7_ASN_ENCODING,
                                                          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);

                        NativeUtilities.ThrowIfFailed(NativeMethods.CryptDecodeObject(
                                                          NativeMethods.X509_ASN_ENCODING | NativeMethods.PKCS_7_ASN_ENCODING,
                                                          new IntPtr(NativeMethods.PKCS7_SIGNER_INFO),
                                                          attributeValue.pbData,
                                                          attributeValue.cbData,
                                                          dwFlags: 0,
                                                          pvStructInfo: counterSignerInfoPointer,
                                                          pcbStructInfo: new IntPtr(&cbSignerInfo)));

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

                        if (IsRepositoryCounterSignerInfo(counterSignerInfo))
                        {
                            return(counterSignerInfo);
                        }
                    }
                }
            }

            return(null);
        }