コード例 #1
0
        /// <summary>
        /// Marshal array of extensions into one big buffer
        /// </summary>
        /// <param name="extensions">Array of extensions</param>
        /// <returns>An allocated buffer (from Marshal.AllocHGlobal) containing the data</returns>
        private IntPtr MarshalExtensions(X509Extension[] extensions)
        {
            int stringSize = extensions.Sum(x => Encoding.ASCII.GetByteCount(x.Oid.Value) + 1);
            int dataSize   = extensions.Sum(x => x.RawData.Length);
            int structSize = Marshal.SizeOf(typeof(CryptoApiMethods.CERT_EXTENSION));

            IntPtr ret           = IntPtr.Zero;
            IntPtr currStructPtr = IntPtr.Zero;
            IntPtr currDataPtr   = IntPtr.Zero;
            IntPtr currStringPtr = IntPtr.Zero;

            ret = Marshal.AllocHGlobal((extensions.Length * structSize) + dataSize + stringSize);

            currStructPtr = ret;
            currDataPtr   = ret + (extensions.Length * structSize);
            currStringPtr = currDataPtr + dataSize;

            foreach (var x in extensions)
            {
                byte[] oidString = Encoding.ASCII.GetBytes(x.Oid.Value + "\0");
                var    certExt   = new CryptoApiMethods.CERT_EXTENSION();
                certExt.pszObjId     = currStringPtr;
                certExt.Value        = new CryptoApiMethods.CRYPTOAPI_BLOB();
                certExt.Value.pbData = currDataPtr;
                certExt.Value.cbData = (uint)x.RawData.Length;
                certExt.fCritical    = x.Critical;

                Marshal.StructureToPtr(certExt, currStructPtr, false);
                Marshal.Copy(oidString, 0, currStringPtr, oidString.Length);
                Marshal.Copy(x.RawData, 0, currDataPtr, x.RawData.Length);

                currStructPtr += structSize;
                currStringPtr += oidString.Length;
                currDataPtr   += x.RawData.Length;
            }

            return(ret);
        }
コード例 #2
0
        /// <summary>
        /// Marshal array of extensions into one big buffer
        /// </summary>
        /// <param name="extensions">Array of extensions</param>
        /// <returns>An allocated buffer (from Marshal.AllocHGlobal) containing the data</returns>
        private IntPtr MarshalExtensions(X509Extension[] extensions)
        {
            int stringSize = extensions.Sum(x => Encoding.ASCII.GetByteCount(x.Oid.Value) + 1);
            int dataSize = extensions.Sum(x => x.RawData.Length);
            int structSize = Marshal.SizeOf(typeof(CryptoApiMethods.CERT_EXTENSION));

            IntPtr ret = IntPtr.Zero;
            IntPtr currStructPtr = IntPtr.Zero;
            IntPtr currDataPtr = IntPtr.Zero;
            IntPtr currStringPtr = IntPtr.Zero;

            ret = Marshal.AllocHGlobal((extensions.Length * structSize) + dataSize + stringSize);

            currStructPtr = ret;
            currDataPtr = ret + (extensions.Length * structSize);
            currStringPtr = currDataPtr + dataSize;

            foreach (var x in extensions)
            {
                byte[] oidString = Encoding.ASCII.GetBytes(x.Oid.Value + "\0");
                var certExt = new CryptoApiMethods.CERT_EXTENSION();
                certExt.pszObjId = currStringPtr;
                certExt.Value = new CryptoApiMethods.CRYPTOAPI_BLOB();
                certExt.Value.pbData = currDataPtr;
                certExt.Value.cbData = (uint)x.RawData.Length;
                certExt.fCritical = x.Critical;

                Marshal.StructureToPtr(certExt, currStructPtr, false);
                Marshal.Copy(oidString, 0, currStringPtr, oidString.Length);
                Marshal.Copy(x.RawData, 0, currDataPtr, x.RawData.Length);

                currStructPtr += structSize;
                currStringPtr += oidString.Length;
                currDataPtr += x.RawData.Length;
            }

            return ret;
        }