Example #1
0
		private static extern ulong HttpQueryServiceConfiguration(
			IntPtr ServiceIntPtr,
			HTTP_SERVICE_CONFIG_ID ConfigId,
			IntPtr pInputConfigInfo,
			int InputConfigInfoLength,
			IntPtr pOutputConfigInfo,
			int OutputConfigInfoLength,
			[Optional] out int pReturnLength,
			IntPtr pOverlapped);
 public static extern uint HttpQueryServiceConfiguration(
     IntPtr ServiceHandle,
     HTTP_SERVICE_CONFIG_ID ConfigId,
     [Optional] IntPtr pInputConfigInfo,
     [Optional] int InputConfigInfoLength,
     [In, Out, Optional] IntPtr pOutputConfigInfo,
     [Optional] int OutputConfigInfoLength,
     [Out, Optional] out int pReturnLength,
     IntPtr pOverlapped);
 internal static extern HttpApi.Error HttpQueryServiceConfiguration(
     IntPtr                  ServiceHandle,
     HTTP_SERVICE_CONFIG_ID  ConfigId,
     IntPtr                  pInputConfigInfo,
     int                     InputConfigInfoLength,
     IntPtr                  pOutputConfigInfo,
     int                     OutputConfigInfoLength,
     out int                 pReturnLength,
     IntPtr                  pOverlapped);
Example #4
0
 public static extern HttpError HttpQueryServiceConfiguration(
     IntPtr ServiceHandle,
     HTTP_SERVICE_CONFIG_ID ConfigId,
     IntPtr pInputConfigInfo,
     int InputConfigInfoLength,
     IntPtr pOutputConfigInfo,
     int OutputConfigInfoLength,
     out int pReturnLength,
     IntPtr pOverlapped);
Example #5
0
 public static unsafe extern uint HttpQueryServiceConfiguration(
         IntPtr ServiceIntPtr,
         HTTP_SERVICE_CONFIG_ID ConfigId,
         ref HTTP_SERVICE_CONFIG_URLACL_QUERY pInputConfigInfo,
         int InputConfigInfoLength,
         byte* pOutputConfigInfo,
         int OutputConfigInfoLength,
         out int pReturnLength,
         IntPtr pOverlapped);
Example #6
0
 public static unsafe extern uint HttpQueryServiceConfiguration(
     IntPtr ServiceIntPtr,
     HTTP_SERVICE_CONFIG_ID ConfigId,
     ref HTTP_SERVICE_CONFIG_URLACL_QUERY pInputConfigInfo,
     int InputConfigInfoLength,
     byte *pOutputConfigInfo,
     int OutputConfigInfoLength,
     out int pReturnLength,
     IntPtr pOverlapped);
 private static extern uint HttpQueryServiceConfiguration(
         IntPtr serviceIntPtr,
         HTTP_SERVICE_CONFIG_ID configId,
         IntPtr pInputConfigInfo,
         int inputConfigInfoLength,
         IntPtr pOutputConfigInfo,
         int outputConfigInfoLength,
         [Optional]
                 out int pReturnLength,
         IntPtr pOverlapped);
 public static extern uint HttpQueryServiceConfiguration(
     IntPtr serviceIntPtr,
     HTTP_SERVICE_CONFIG_ID configId,
     IntPtr pInputConfigInfo,
     int inputConfigInfoLength,
     IntPtr pOutputConfigInfo,
     int outputConfigInfoLength,
     [Optional]
     out int pReturnLength,
     IntPtr pOverlapped);
Example #9
0
 public static extern uint HttpQueryServiceConfiguration(
      IntPtr ServiceIntPtr,
      HTTP_SERVICE_CONFIG_ID ConfigId,
      IntPtr pInputConfigInfo,
      int InputConfigInfoLength,
      IntPtr pOutputConfigInfo,
      int OutputConfigInfoLength,
      [Optional()]
      out int pReturnLength,
      IntPtr pOverlapped);
Example #10
0
 static extern uint HttpDeleteServiceConfiguration(
     IntPtr ServiceIntPtr,
     HTTP_SERVICE_CONFIG_ID ConfigId,
     IntPtr pConfigInformation,
     int ConfigInformationLength,
     IntPtr pOverlapped);
Example #11
0
 internal static extern Win32Error HttpSetServiceConfiguration(IntPtr serviceHandle, HTTP_SERVICE_CONFIG_ID configId, HTTP_SERVICE_CONFIG_URLACL_SET pConfigInformation, int configInformationLength, IntPtr pOverlapped);
 public static extern HttpError HttpDeleteServiceConfiguration(
     IntPtr ServiceHandle,
     HTTP_SERVICE_CONFIG_ID ConfigId,
     IntPtr pConfigInformation,
     int ConfigInformationLength,
     IntPtr pOverlapped);
Example #13
0
        public static SslCertificateInfo QuerySslCertificateInfo(IPEndPoint ipPort)
        {
            SslCertificateInfo result = null;

            uint retVal;

            CallHttpApi(delegate
            {
                GCHandle sockAddrHandle            = CreateSockaddrStructure(ipPort);
                IntPtr pIpPort                     = sockAddrHandle.AddrOfPinnedObject();
                HTTP_SERVICE_CONFIG_SSL_KEY sslKey = new HTTP_SERVICE_CONFIG_SSL_KEY(pIpPort);

                HTTP_SERVICE_CONFIG_SSL_QUERY inputConfigInfoQuery =
                    new HTTP_SERVICE_CONFIG_SSL_QUERY
                {
                    QueryDesc = HTTP_SERVICE_CONFIG_QUERY_TYPE.HttpServiceConfigQueryExact,
                    KeyDesc   = sslKey
                };

                IntPtr pInputConfigInfo =
                    Marshal.AllocCoTaskMem(Marshal.SizeOf(typeof(HTTP_SERVICE_CONFIG_SSL_QUERY)));
                Marshal.StructureToPtr(inputConfigInfoQuery, pInputConfigInfo, false);

                IntPtr pOutputConfigInfo = IntPtr.Zero;
                int returnLength         = 0;

                try
                {
                    HTTP_SERVICE_CONFIG_ID queryType = HTTP_SERVICE_CONFIG_ID.HttpServiceConfigSSLCertInfo;
                    int inputConfigInfoSize          = Marshal.SizeOf(inputConfigInfoQuery);
                    retVal = HttpQueryServiceConfiguration(IntPtr.Zero,
                                                           queryType,
                                                           pInputConfigInfo,
                                                           inputConfigInfoSize,
                                                           pOutputConfigInfo,
                                                           returnLength,
                                                           out returnLength,
                                                           IntPtr.Zero);
                    if (retVal == ERROR_FILE_NOT_FOUND)
                    {
                        return;
                    }

                    if (ERROR_INSUFFICIENT_BUFFER == retVal) // ERROR_INSUFFICIENT_BUFFER = 122
                    {
                        pOutputConfigInfo = Marshal.AllocCoTaskMem(returnLength);

                        try
                        {
                            retVal = HttpQueryServiceConfiguration(IntPtr.Zero,
                                                                   queryType,
                                                                   pInputConfigInfo,
                                                                   inputConfigInfoSize,
                                                                   pOutputConfigInfo,
                                                                   returnLength,
                                                                   out returnLength,
                                                                   IntPtr.Zero);
                            ThrowWin32ExceptionIfError(retVal);

                            var outputConfigInfo =
                                (HTTP_SERVICE_CONFIG_SSL_SET)
                                Marshal.PtrToStructure(pOutputConfigInfo, typeof(HTTP_SERVICE_CONFIG_SSL_SET));

                            byte[] hash = new byte[outputConfigInfo.ParamDesc.SslHashLength];
                            Marshal.Copy(outputConfigInfo.ParamDesc.pSslHash, hash, 0, hash.Length);

                            Guid appId       = outputConfigInfo.ParamDesc.AppId;
                            string storeName = outputConfigInfo.ParamDesc.pSslCertStoreName;

                            result = new SslCertificateInfo {
                                AppId = appId, Hash = hash, StoreName = storeName, IpPort = ipPort
                            };
                        }
                        finally
                        {
                            Marshal.FreeCoTaskMem(pOutputConfigInfo);
                        }
                    }
                    else
                    {
                        ThrowWin32ExceptionIfError(retVal);
                    }
                }
                finally
                {
                    Marshal.FreeCoTaskMem(pInputConfigInfo);
                    if (sockAddrHandle.IsAllocated)
                    {
                        sockAddrHandle.Free();
                    }
                }
            });

            return(result);
        }
Example #14
0
 static extern ErrorCode HttpQueryServiceConfiguration(IntPtr nullHandle, HTTP_SERVICE_CONFIG_ID ConfigId, ref HTTP_SERVICE_CONFIG_SSL_QUERY pInputConfigInfo, int InputConfigInfoLength, byte *pOutputConfigInfo, int OutputConfigInfoLength, out int pReturnLength, IntPtr pOverlapped);
Example #15
0
 internal static extern uint HttpQueryServiceConfiguration(IntPtr ServiceHandle, HTTP_SERVICE_CONFIG_ID ConfigId, IntPtr pInputConfigInfo, uint InputConfigLength, IntPtr pOutputConfigInfo, uint OutputConfigInfoLength, ref uint pReturnLength, IntPtr pOverlapped);
 public static extern uint HttpDeleteServiceConfiguration(
     IntPtr serviceIntPtr,
     HTTP_SERVICE_CONFIG_ID configId,
     HTTP_SERVICE_CONFIG_URLACL_SET pConfigInformation,
     int configInformationLength,
     IntPtr pOverlapped);
Example #17
0
 internal static extern uint HttpQueryServiceConfiguration(IntPtr ServiceHandle, HTTP_SERVICE_CONFIG_ID ConfigId, IntPtr pInputConfigInfo, uint InputConfigLength, IntPtr pOutputConfigInfo, uint OutputConfigInfoLength, ref uint pReturnLength, IntPtr pOverlapped);
Example #18
0
        public static SslSniInfo[] QuerySslSniInfo()
        {
            var result = new List <SslSniInfo>();

            if (Environment.OSVersion.Version < new Version(6, 2))
            {
                return(result.ToArray());
            }

            CallHttpApi(
                delegate
            {
                uint token = 0;

                uint retVal;
                do
                {
                    HTTP_SERVICE_CONFIG_SSL_SNI_QUERY inputConfigInfoQuery =
                        new HTTP_SERVICE_CONFIG_SSL_SNI_QUERY
                    {
                        QueryDesc = HTTP_SERVICE_CONFIG_QUERY_TYPE.HttpServiceConfigQueryNext,
                        dwToken   = token
                    };

                    IntPtr pInputConfigInfo =
                        Marshal.AllocCoTaskMem(Marshal.SizeOf(typeof(HTTP_SERVICE_CONFIG_SSL_SNI_QUERY)));
                    Marshal.StructureToPtr(inputConfigInfoQuery, pInputConfigInfo, false);

                    IntPtr pOutputConfigInfo = IntPtr.Zero;
                    int returnLength         = 0;

                    const HTTP_SERVICE_CONFIG_ID queryType = HTTP_SERVICE_CONFIG_ID.HttpServiceConfigSslSniCertInfo;

                    try
                    {
                        int inputConfigInfoSize = Marshal.SizeOf(inputConfigInfoQuery);
                        retVal = HttpQueryServiceConfiguration(IntPtr.Zero,
                                                               queryType,
                                                               pInputConfigInfo,
                                                               inputConfigInfoSize,
                                                               pOutputConfigInfo,
                                                               returnLength,
                                                               out returnLength,
                                                               IntPtr.Zero);
                        if (ERROR_NO_MORE_ITEMS == retVal)
                        {
                            break;
                        }
                        if (ERROR_INSUFFICIENT_BUFFER == retVal)     // ERROR_INSUFFICIENT_BUFFER = 122
                        {
                            pOutputConfigInfo = Marshal.AllocCoTaskMem(returnLength);

                            try
                            {
                                retVal = HttpQueryServiceConfiguration(
                                    IntPtr.Zero,
                                    queryType,
                                    pInputConfigInfo,
                                    inputConfigInfoSize,
                                    pOutputConfigInfo,
                                    returnLength,
                                    out returnLength,
                                    IntPtr.Zero);
                                ThrowWin32ExceptionIfError(retVal);

                                var outputConfigInfo =
                                    (HTTP_SERVICE_CONFIG_SSL_SNI_SET)
                                    Marshal.PtrToStructure(pOutputConfigInfo, typeof(HTTP_SERVICE_CONFIG_SSL_SNI_SET));

                                byte[] hash = new byte[outputConfigInfo.ParamDesc.SslHashLength];
                                Marshal.Copy(outputConfigInfo.ParamDesc.pSslHash, hash, 0, hash.Length);

                                Guid appId       = outputConfigInfo.ParamDesc.AppId;
                                string storeName = outputConfigInfo.ParamDesc.pSslCertStoreName;
                                var ipPort       = ReadSockAddrStorageStructure(outputConfigInfo.KeyDesc.IpPort);

                                var host       = outputConfigInfo.KeyDesc.Host;
                                var resultItem = new SslSniInfo
                                {
                                    AppId     = appId,
                                    Hash      = hash,
                                    StoreName = storeName,
                                    Host      = host,
                                    Port      = ipPort.Port
                                };
                                result.Add(resultItem);
                                token++;
                            }
                            finally
                            {
                                Marshal.FreeCoTaskMem(pOutputConfigInfo);
                            }
                        }
                        else
                        {
                            ThrowWin32ExceptionIfError(retVal);
                        }
                    }
                    finally
                    {
                        Marshal.FreeCoTaskMem(pInputConfigInfo);
                    }
                } while (NOERROR == retVal);
            });

            return(result.ToArray());
        }
 internal static extern uint HttpSetServiceConfiguration(
     IntPtr ServiceHandle,
     HTTP_SERVICE_CONFIG_ID ConfigId,
     IntPtr pConfigInformation,
     int ConfigInformationLength,
     IntPtr pOverlapped);
Example #20
0
        public static HttpNamespaceAcl[] QueryHttpNamespaceAcls()
        {
            var result = new List <HttpNamespaceAcl>();

            CallHttpApi(
                delegate
            {
                uint token = 0;

                uint retVal;
                do
                {
                    HTTP_SERVICE_CONFIG_URLACL_QUERY inputConfigInfoQuery =
                        new HTTP_SERVICE_CONFIG_URLACL_QUERY
                    {
                        QueryDesc = HTTP_SERVICE_CONFIG_QUERY_TYPE.HttpServiceConfigQueryNext,
                        dwToken   = token
                    };

                    IntPtr pInputConfigInfo =
                        Marshal.AllocCoTaskMem(Marshal.SizeOf(typeof(HTTP_SERVICE_CONFIG_URLACL_QUERY)));
                    Marshal.StructureToPtr(inputConfigInfoQuery, pInputConfigInfo, false);

                    IntPtr pOutputConfigInfo = IntPtr.Zero;
                    int returnLength         = 0;

                    const HTTP_SERVICE_CONFIG_ID queryType = HTTP_SERVICE_CONFIG_ID.HttpServiceConfigUrlAclInfo;

                    try
                    {
                        int inputConfigInfoSize = Marshal.SizeOf(inputConfigInfoQuery);
                        retVal = HttpQueryServiceConfiguration(IntPtr.Zero,
                                                               queryType,
                                                               pInputConfigInfo,
                                                               inputConfigInfoSize,
                                                               pOutputConfigInfo,
                                                               returnLength,
                                                               out returnLength,
                                                               IntPtr.Zero);
                        if (ERROR_NO_MORE_ITEMS == retVal)
                        {
                            break;
                        }
                        if (ERROR_INSUFFICIENT_BUFFER == retVal)     // ERROR_INSUFFICIENT_BUFFER = 122
                        {
                            pOutputConfigInfo = Marshal.AllocCoTaskMem(returnLength);

                            try
                            {
                                retVal = HttpQueryServiceConfiguration(
                                    IntPtr.Zero,
                                    queryType,
                                    pInputConfigInfo,
                                    inputConfigInfoSize,
                                    pOutputConfigInfo,
                                    returnLength,
                                    out returnLength,
                                    IntPtr.Zero);
                                ThrowWin32ExceptionIfError(retVal);

                                var outputConfigInfo =
                                    (HTTP_SERVICE_CONFIG_URLACL_SET)
                                    Marshal.PtrToStructure(pOutputConfigInfo, typeof(HTTP_SERVICE_CONFIG_URLACL_SET));

                                var resultItem = new HttpNamespaceAcl
                                {
                                    UrlPrefix          = outputConfigInfo.KeyDesc.pUrlPrefix,
                                    SecurityDescriptor = outputConfigInfo.ParamDesc.pStringSecurityDescriptor
                                };
                                result.Add(resultItem);
                                token++;
                            }
                            finally
                            {
                                Marshal.FreeCoTaskMem(pOutputConfigInfo);
                            }
                        }
                        else
                        {
                            ThrowWin32ExceptionIfError(retVal);
                        }
                    }
                    finally
                    {
                        Marshal.FreeCoTaskMem(pInputConfigInfo);
                    }
                } while (NOERROR == retVal);
            });

            return(result.ToArray());
        }
Example #21
0
 public static extern uint HttpSetServiceConfiguration(
         IntPtr ServiceIntPtr,
         HTTP_SERVICE_CONFIG_ID ConfigId,
         ref HTTP_SERVICE_CONFIG_URLACL_SET pConfigInformation,
         int ConfigInformationLength,
         IntPtr pOverlapped);
 public static extern uint HttpQueryServiceConfiguration(
     IntPtr ServiceHandle,
     HTTP_SERVICE_CONFIG_ID ConfigId,
     [Optional] IntPtr pInputConfigInfo,
     [Optional] int InputConfigInfoLength,
     [In, Out, Optional] IntPtr pOutputConfigInfo,
     [Optional] int OutputConfigInfoLength,
     [Out, Optional] out int pReturnLength,
     IntPtr pOverlapped);
Example #23
0
        public static SslSniInfo QuerySslSniInfo(Tuple <string, int> binding)
        {
            if (string.IsNullOrWhiteSpace(binding.Item1))
            {
                return(null);
            }

            if (Environment.OSVersion.Version < new Version(6, 2))
            {
                return(null);
            }

            SslSniInfo result = null;

            uint retVal;

            CallHttpApi(delegate
            {
                HTTP_SERVICE_CONFIG_SSL_SNI_KEY sslKey = new HTTP_SERVICE_CONFIG_SSL_SNI_KEY();
                sslKey.Host   = binding.Item1;
                sslKey.IpPort = CreateSockAddrStorageStructure(binding.Item2);

                HTTP_SERVICE_CONFIG_SSL_SNI_QUERY inputConfigInfoQuery =
                    new HTTP_SERVICE_CONFIG_SSL_SNI_QUERY
                {
                    QueryDesc = HTTP_SERVICE_CONFIG_QUERY_TYPE.HttpServiceConfigQueryExact,
                    KeyDesc   = sslKey
                };

                IntPtr pInputConfigInfo =
                    Marshal.AllocCoTaskMem(Marshal.SizeOf(typeof(HTTP_SERVICE_CONFIG_SSL_SNI_QUERY)));
                Marshal.StructureToPtr(inputConfigInfoQuery, pInputConfigInfo, false);

                IntPtr pOutputConfigInfo = IntPtr.Zero;
                int returnLength         = 0;

                try
                {
                    HTTP_SERVICE_CONFIG_ID queryType = HTTP_SERVICE_CONFIG_ID.HttpServiceConfigSslSniCertInfo;
                    int inputConfigInfoSize          = Marshal.SizeOf(inputConfigInfoQuery);
                    retVal = HttpQueryServiceConfiguration(IntPtr.Zero,
                                                           queryType,
                                                           pInputConfigInfo,
                                                           inputConfigInfoSize,
                                                           pOutputConfigInfo,
                                                           returnLength,
                                                           out returnLength,
                                                           IntPtr.Zero);
                    if (retVal == ERROR_FILE_NOT_FOUND)
                    {
                        return;
                    }

                    if (ERROR_INSUFFICIENT_BUFFER == retVal) // ERROR_INSUFFICIENT_BUFFER = 122
                    {
                        pOutputConfigInfo = Marshal.AllocCoTaskMem(returnLength);

                        try
                        {
                            retVal = HttpQueryServiceConfiguration(IntPtr.Zero,
                                                                   queryType,
                                                                   pInputConfigInfo,
                                                                   inputConfigInfoSize,
                                                                   pOutputConfigInfo,
                                                                   returnLength,
                                                                   out returnLength,
                                                                   IntPtr.Zero);
                            ThrowWin32ExceptionIfError(retVal);

                            var outputConfigInfo =
                                (HTTP_SERVICE_CONFIG_SSL_SNI_SET)
                                Marshal.PtrToStructure(pOutputConfigInfo, typeof(HTTP_SERVICE_CONFIG_SSL_SNI_SET));

                            byte[] hash = new byte[outputConfigInfo.ParamDesc.SslHashLength];
                            Marshal.Copy(outputConfigInfo.ParamDesc.pSslHash, hash, 0, hash.Length);

                            Guid appId       = outputConfigInfo.ParamDesc.AppId;
                            string storeName = outputConfigInfo.ParamDesc.pSslCertStoreName;

                            var host   = outputConfigInfo.KeyDesc.Host;
                            var ipPort = ReadSockAddrStorageStructure(outputConfigInfo.KeyDesc.IpPort);

                            result = new SslSniInfo {
                                AppId = appId, Hash = hash, StoreName = storeName, Port = ipPort.Port, Host = host
                            };
                        }
                        finally
                        {
                            Marshal.FreeCoTaskMem(pOutputConfigInfo);
                        }
                    }
                    else
                    {
                        ThrowWin32ExceptionIfError(retVal);
                    }
                }
                finally
                {
                    Marshal.FreeCoTaskMem(pInputConfigInfo);
                }
            });

            return(result);
        }
 internal static extern HttpApi.Error HttpSetServiceConfiguration(
     IntPtr                  ServiceHandle,
     HTTP_SERVICE_CONFIG_ID  ConfigId,
     IntPtr                  pConfigInformation,
     int                     ConfigInformationLength,
     IntPtr                  pOverlapped);
 private static extern uint HttpSetServiceConfiguration(
         IntPtr serviceIntPtr,
         HTTP_SERVICE_CONFIG_ID configId,
         IntPtr pConfigInformation,
         int configInformationLength,
         IntPtr pOverlapped);
Example #26
0
 public static extern uint HttpSetServiceConfiguration(
     IntPtr ServiceIntPtr,
     HTTP_SERVICE_CONFIG_ID ConfigId,
     ref HTTP_SERVICE_CONFIG_URLACL_SET pConfigInformation,
     int ConfigInformationLength,
     IntPtr pOverlapped);
        /// <summary>
        /// Возвращает информацию о привязке сертификата к сетевому адресу и порту.
        /// </summary>
        public static BindCertificateInfo GetBindCertificate(IPEndPoint endPoint)
        {
            BindCertificateInfo result = null;

            CallHttpApi(() =>
            {
                var socketAddressHandle = CreateNativeSocketAddress(endPoint);
                var socketAddressPtr    = socketAddressHandle.AddrOfPinnedObject();

                // Запрос на получение записи конфигурации
                var query = new HTTP_SERVICE_CONFIG_SSL_QUERY
                {
                    QueryDesc = HTTP_SERVICE_CONFIG_QUERY_TYPE.HttpServiceConfigQueryExact,
                    KeyDesc   = new HTTP_SERVICE_CONFIG_SSL_KEY(socketAddressPtr)
                };

                var queryPtr = Marshal.AllocCoTaskMem(Marshal.SizeOf(typeof(HTTP_SERVICE_CONFIG_SSL_QUERY)));
                Marshal.StructureToPtr(query, queryPtr, false);

                var queryResultSize = 0;
                var queryResultPtr  = IntPtr.Zero;

                const HTTP_SERVICE_CONFIG_ID queryType = HTTP_SERVICE_CONFIG_ID.HttpServiceConfigSSLCertInfo;

                try
                {
                    // Получение записи конфигурации
                    var querySize    = Marshal.SizeOf(query);
                    var invokeResult = HttpQueryServiceConfiguration(IntPtr.Zero, queryType, queryPtr, querySize,
                                                                     queryResultPtr, queryResultSize, out queryResultSize, IntPtr.Zero);

                    // По запросу ничего не найдено
                    if (invokeResult == ERROR_FILE_NOT_FOUND)
                    {
                        return;
                    }

                    // Недостаточный размер буфера для помещения результата запроса
                    if (invokeResult == ERROR_INSUFFICIENT_BUFFER)
                    {
                        queryResultPtr = Marshal.AllocCoTaskMem(queryResultSize);

                        try
                        {
                            // Получение записи конфигурации (вторая попытка)
                            invokeResult = HttpQueryServiceConfiguration(IntPtr.Zero, queryType, queryPtr, querySize,
                                                                         queryResultPtr, queryResultSize, out queryResultSize, IntPtr.Zero);
                            ThrowWin32ExceptionIfError(invokeResult);

                            // Интерпретация полученного результата запроса из указателя
                            var queryResult =
                                (HTTP_SERVICE_CONFIG_SSL_SET)
                                Marshal.PtrToStructure(queryResultPtr, typeof(HTTP_SERVICE_CONFIG_SSL_SET));

                            var certificate = new byte[queryResult.ParamDesc.SslHashLength];
                            Marshal.Copy(queryResult.ParamDesc.pSslHash, certificate, 0, certificate.Length);

                            StoreName certificateStore;
                            Enum.TryParse(queryResult.ParamDesc.pSslCertStoreName, out certificateStore);

                            result = new BindCertificateInfo
                            {
                                EndPoint         = endPoint,
                                Certificate      = certificate,
                                CertificateStore = certificateStore,
                                ApplicationId    = queryResult.ParamDesc.AppId
                            };
                        }
                        finally
                        {
                            Marshal.FreeCoTaskMem(queryResultPtr);
                        }
                    }
                    else
                    {
                        ThrowWin32ExceptionIfError(invokeResult);
                    }
                }
                finally
                {
                    Marshal.FreeCoTaskMem(queryPtr);

                    if (socketAddressHandle.IsAllocated)
                    {
                        socketAddressHandle.Free();
                    }
                }
            });

            return(result);
        }
Example #28
0
 static extern ErrorCode HttpDeleteServiceConfiguration(IntPtr serviceIntPtr, HTTP_SERVICE_CONFIG_ID configId, ref HTTP_SERVICE_CONFIG_SSL_SET configInformation, int configInformationLength, IntPtr pOverlapped);
        /// <summary>
        /// Возвращает список с информацией о привязках сертификатов к сетевому адресу и порту.
        /// </summary>
        public static BindCertificateInfo[] GetBindCertificates()
        {
            var result = new List <BindCertificateInfo>();

            CallHttpApi(() =>
            {
                uint index = 0;
                uint invokeResult;

                do
                {
                    // Запрос на получение записи конфигурации
                    var query = new HTTP_SERVICE_CONFIG_SSL_QUERY
                    {
                        QueryDesc = HTTP_SERVICE_CONFIG_QUERY_TYPE.HttpServiceConfigQueryNext,
                        dwToken   = index
                    };

                    var queryPtr = Marshal.AllocCoTaskMem(Marshal.SizeOf(typeof(HTTP_SERVICE_CONFIG_SSL_QUERY)));
                    Marshal.StructureToPtr(query, queryPtr, false);

                    var queryResultSize = 0;
                    var queryResultPtr  = IntPtr.Zero;

                    const HTTP_SERVICE_CONFIG_ID queryType = HTTP_SERVICE_CONFIG_ID.HttpServiceConfigSSLCertInfo;

                    try
                    {
                        // Получение записи конфигурации
                        var querySize = Marshal.SizeOf(query);
                        invokeResult  = HttpQueryServiceConfiguration(IntPtr.Zero, queryType, queryPtr, querySize,
                                                                      queryResultPtr, queryResultSize, out queryResultSize, IntPtr.Zero);

                        // Перечисление не содержит элементов
                        if (invokeResult == ERROR_NO_MORE_ITEMS)
                        {
                            break;
                        }

                        // Недостаточный размер буфера для помещения результата запроса
                        if (invokeResult == ERROR_INSUFFICIENT_BUFFER)
                        {
                            queryResultPtr = Marshal.AllocCoTaskMem(queryResultSize);

                            try
                            {
                                // Получение записи конфигурации (вторая попытка)
                                invokeResult = HttpQueryServiceConfiguration(IntPtr.Zero, queryType, queryPtr, querySize,
                                                                             queryResultPtr, queryResultSize, out queryResultSize, IntPtr.Zero);
                                ThrowWin32ExceptionIfError(invokeResult);

                                // Интерпретация полученного результата запроса из указателя
                                var queryResult =
                                    (HTTP_SERVICE_CONFIG_SSL_SET)
                                    Marshal.PtrToStructure(queryResultPtr, typeof(HTTP_SERVICE_CONFIG_SSL_SET));

                                var certificate = new byte[queryResult.ParamDesc.SslHashLength];
                                Marshal.Copy(queryResult.ParamDesc.pSslHash, certificate, 0, certificate.Length);

                                StoreName certificateStore;
                                Enum.TryParse(queryResult.ParamDesc.pSslCertStoreName, out certificateStore);

                                result.Add(new BindCertificateInfo
                                {
                                    EndPoint         = CreateManagedSocketAddress(queryResult.KeyDesc.pIpPort),
                                    Certificate      = certificate,
                                    CertificateStore = certificateStore,
                                    ApplicationId    = queryResult.ParamDesc.AppId
                                });

                                index++;
                            }
                            finally
                            {
                                Marshal.FreeCoTaskMem(queryResultPtr);
                            }
                        }
                        else
                        {
                            ThrowWin32ExceptionIfError(invokeResult);
                        }
                    }
                    finally
                    {
                        Marshal.FreeCoTaskMem(queryPtr);
                    }
                }while (invokeResult == NOERROR);
            });

            return(result.ToArray());
        }
Example #30
0
 static extern ErrorCode HttpSetServiceConfiguration(IntPtr nullHandle, HTTP_SERVICE_CONFIG_ID configId, ref HTTP_SERVICE_CONFIG_SSL_SET configInformation, int configInformationLength, IntPtr pOverlapped);
        /// <summary>
        /// Создает привязку сертификата к сетевому адресу и порту.
        /// </summary>
        public static void CreateBindCertificate(IPEndPoint endPoint, byte[] certificate, StoreName certificateStore,
                                                 Guid applicationId)
        {
            CallHttpApi(() =>
            {
                var socketAddressHandle = CreateNativeSocketAddress(endPoint);
                var socketAddressPtr    = socketAddressHandle.AddrOfPinnedObject();
                var certificateHandle   = GCHandle.Alloc(certificate, GCHandleType.Pinned);

                // Запись конфигурации с информацией о привязке сертификата к сетевому адресу и порту
                var configInfo = new HTTP_SERVICE_CONFIG_SSL_SET
                {
                    KeyDesc   = new HTTP_SERVICE_CONFIG_SSL_KEY(socketAddressPtr),
                    ParamDesc = new HTTP_SERVICE_CONFIG_SSL_PARAM
                    {
                        AppId = applicationId,
                        DefaultCertCheckMode                 = 0,
                        DefaultFlags                         = 0,
                        DefaultRevocationFreshnessTime       = 0,
                        DefaultRevocationUrlRetrievalTimeout = 0,
                        pSslCertStoreName                    = certificateStore.ToString(),
                        pSslHash      = certificateHandle.AddrOfPinnedObject(),
                        SslHashLength = certificate.Length
                    }
                };

                var configInfoPtr = Marshal.AllocCoTaskMem(Marshal.SizeOf(typeof(HTTP_SERVICE_CONFIG_SSL_SET)));
                Marshal.StructureToPtr(configInfo, configInfoPtr, false);

                const HTTP_SERVICE_CONFIG_ID queryType = HTTP_SERVICE_CONFIG_ID.HttpServiceConfigSSLCertInfo;

                try
                {
                    var invokeResult = HttpSetServiceConfiguration(IntPtr.Zero, queryType, configInfoPtr,
                                                                   Marshal.SizeOf(configInfo), IntPtr.Zero);

                    if (invokeResult == ERROR_ALREADY_EXISTS)
                    {
                        // Удаление записи
                        invokeResult = HttpDeleteServiceConfiguration(IntPtr.Zero, queryType, configInfoPtr,
                                                                      Marshal.SizeOf(configInfo), IntPtr.Zero);
                        ThrowWin32ExceptionIfError(invokeResult);

                        // Создание записи
                        invokeResult = HttpSetServiceConfiguration(IntPtr.Zero, queryType, configInfoPtr,
                                                                   Marshal.SizeOf(configInfo), IntPtr.Zero);
                        ThrowWin32ExceptionIfError(invokeResult);
                    }
                    else
                    {
                        ThrowWin32ExceptionIfError(invokeResult);
                    }
                }
                finally
                {
                    Marshal.FreeCoTaskMem(configInfoPtr);

                    if (certificateHandle.IsAllocated)
                    {
                        certificateHandle.Free();
                    }

                    if (socketAddressHandle.IsAllocated)
                    {
                        socketAddressHandle.Free();
                    }
                }
            });
        }
Example #32
0
 public static extern HttpError HttpDeleteServiceConfiguration(
     IntPtr ServiceHandle,
     HTTP_SERVICE_CONFIG_ID ConfigId,
     IntPtr pConfigInformation,
     int ConfigInformationLength,
     IntPtr pOverlapped);
 private static extern uint HttpSetServiceConfiguration(IntPtr serviceIntPtr, HTTP_SERVICE_CONFIG_ID configId, IntPtr pConfigInformation, int configInformationLength, IntPtr pOverlapped);
Example #34
0
 static extern uint HttpDeleteServiceConfiguration(
     IntPtr ServiceIntPtr,
     HTTP_SERVICE_CONFIG_ID ConfigId,
     IntPtr pConfigInformation,
     int ConfigInformationLength,
     IntPtr pOverlapped);
Example #35
0
 internal static extern Win32Error HttpQueryServiceConfiguration(IntPtr serviceHandle, HTTP_SERVICE_CONFIG_ID configId, HTTP_SERVICE_CONFIG_URLACL_QUERY pInputConfigInfo, int inputConfigInfoLength, IntPtr pOutputConfigInfo, int outputConfigInfoLength, out int pReturnLength, IntPtr pOverlapped);