Advertisement
Jimi2000

QueryContextAttributesW

Feb 11th, 2018
946
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 16.19 KB | None | 0 0
  1.         //<sspi.h>
  2.         //<schannel.h>
  3.         private enum ContextAttribute : uint {
  4.             Sizes               = 0x00,
  5.             Names               = 0x01,
  6.             Lifespan            = 0x02,
  7.             DceInfo             = 0x03,
  8.             StreamSizes         = 0x04,
  9.             //KeyInfo             = 0x05, must not be used, see ConnectionInfo instead
  10.             Authority           = 0x06,
  11.             ProtoInfo           = 7,
  12.             PasswordExpiry      = 8,
  13.             SessionKey          = 9,
  14.             PackageInfo         = 0x0A,
  15.             UserFlags           = 11,
  16.             NegotiationInfo     = 0x0C,
  17.             NativeNames         = 13,
  18.             Flags               = 14,
  19.             UseValidated        = 15,
  20.             CredentialName      = 16,
  21.             TargetInformation   = 17,
  22.             AccessToken         = 18,
  23.             Target              = 19,
  24.             AuthenticationId    = 20,
  25.             UniqueBindings      = 0x19,
  26.             EndpointBindings    = 0x1A,
  27.             ClientSpecifiedSpn  = 0x1B,      // SECPKG_ATTR_CLIENT_SPECIFIED_TARGET = 27
  28.             RemoteCertificate   = 0x53,
  29.             LocalCertificate    = 0x54,
  30.             RootStore           = 0x55,
  31.             IssuerListInfoEx    = 0x59,
  32.             ConnectionInfo      = 0x5A,
  33.             EAPKeyBlock         = 0x5b,      // returns SecPkgContext_EapKeyBlock  
  34.             MappedCredAttr      = 0x5c,      // returns SecPkgContext_MappedCredAttr  
  35.             SessionInfo         = 0x5d,      // returns SecPkgContext_SessionInfo  
  36.             AppData             = 0x5e,      // sets/returns SecPkgContext_SessionAppData  
  37.             RemoteCertificates  = 0x5F,      // returns SecPkgContext_Certificates  
  38.             ClientCertPolicy    = 0x60,      // sets    SecPkgCred_ClientCertCtlPolicy  
  39.             CCPolicyResult      = 0x61,      // returns SecPkgContext_ClientCertPolicyResult  
  40.             UseNcrypt           = 0x62,      // Sets the CRED_FLAG_USE_NCRYPT_PROVIDER FLAG on cred group  
  41.             LocalCertInfo       = 0x63,      // returns SecPkgContext_CertInfo  
  42.             CipherInfo          = 0x64,      // returns new CNG SecPkgContext_CipherInfo  
  43.             EAPPrfInfo          = 0x65,      // sets    SecPkgContext_EapPrfInfo  
  44.             SupportedSignatures = 0x66,      // returns SecPkgContext_SupportedSignatures  
  45.             RemoteCertChain     = 0x67,      // returns PCCERT_CONTEXT  
  46.             UiInfo              = 0x68,      // sets SEcPkgContext_UiInfo  
  47.         }
  48.  
  49.         private enum ALG_ID : uint
  50.         {
  51.             CALG_3DES                   = 0x00006603,   //Triple DES encryption algorithm.
  52.             CALG_3DES_112               = 0x00006609,   //Two-key triple DES encryption with effective key length equal to 112 bits.
  53.             CALG_AES                    = 0x00006611,   //Advanced Encryption Standard (AES).
  54.                                                         //This algorithm is supported by the Microsoft AES Cryptographic Provider.
  55.             CALG_AES_128                = 0x0000660e,   //128 bit AES. This algorithm is supported by the Microsoft AES Cryptographic
  56.                                                         //Provider.
  57.             CALG_AES_192                = 0x0000660f,   //192 bit AES. This algorithm is supported by the Microsoft AES Cryptographic
  58.                                                         //Provider.
  59.             CALG_AES_256                = 0x00006610,   //256 bit AES. This algorithm is supported by the Microsoft AES Cryptographic
  60.                                                         //Provider.
  61.             CALG_AGREEDKEY_ANY          = 0x0000aa03,   //Temporary algorithm identifier for handles of Diffie-Hellman–agreed keys.
  62.             CALG_CYLINK_MEK             = 0x0000660c,   //An algorithm to create a 40-bit DES key that has parity bits and zeroed
  63.                                                         //key bits to make its key length 64 bits.
  64.                                                         //This algorithm is supported by the Microsoft
  65.                                                         //Base Cryptographic Provider.
  66.             CALG_DES                    = 0x00006601,   //DES encryption algorithm.
  67.             CALG_DESX                   = 0x00006604,   //DESX encryption algorithm.
  68.             CALG_DH_EPHEM               = 0x0000aa02,   //Diffie-Hellman ephemeral key exchange algorithm.
  69.             CALG_DH_SF                  = 0x0000aa01,   //Diffie-Hellman store and forward key exchange algorithm.
  70.             CALG_DSS_SIGN               = 0x00002200,   //DSA public key signature algorithm.
  71.             CALG_ECDH                   = 0x0000aa05,   //Elliptic curve Diffie-Hellman key exchange algorithm.
  72.                                                         //Note This algorithm is supported only through Cryptography API:
  73.                                                         //Next Generation. Windows Server 2003
  74.                                                         //and Windows XP:  This algorithm is not supported.
  75.             CALG_ECDH_EPHEM             = 0x0000ae06,   //Ephemeral elliptic curve Diffie-Hellman key exchange algorithm.
  76.                                                         //Note  This algorithm is supported only through Cryptography API:
  77.                                                         //Next Generation Windows Server 2003 and Windows XP:
  78.                                                         //This algorithm is not supported.
  79.             CALG_ECDSA                  = 0x00002203,   //Elliptic curve digital signature algorithm. Note  This algorithm is
  80.                                                         //supported only through Cryptography API: Next Generation.
  81.                                                         //Windows Server 2003 and Windows XP:  This algorithm is not supported.
  82.             CALG_ECMQV                  = 0x0000a001,   //Elliptic curve Menezes, Qu, and Vanstone (MQV) key exchange algorithm.
  83.                                                         //This algorithm is not supported.
  84.             CALG_HASH_REPLACE_OWF       = 0x0000800b,   //One way function hashing algorithm.
  85.             CALG_HUGHES_MD5             = 0x0000a003,   //Hughes MD5 hashing algorithm.
  86.             CALG_HMAC                   = 0x00008009,   //HMAC keyed hash algorithm. This algorithm is supported by the
  87.                                                         //Microsoft Base Cryptographic Provider.
  88.             CALG_KEA_KEYX               = 0x0000aa04,   //KEA key exchange algorithm (FORTEZZA). This algorithm is not supported.
  89.             CALG_MAC                    = 0x00008005,   //MAC keyed hash algorithm. This algorithm is supported by the
  90.                                                         //Microsoft Base Cryptographic Provider.
  91.             CALG_MD2                    = 0x00008001,   //MD2 hashing algorithm. This algorithm is supported by the
  92.                                                         //Microsoft Base Cryptographic Provider.
  93.             CALG_MD4                    = 0x00008002,   //MD4 hashing algorithm.
  94.             CALG_MD5                    = 0x00008003,   //MD5 hashing algorithm. This algorithm is supported by the
  95.                                                         //Microsoft Base Cryptographic Provider.
  96.             CALG_NO_SIGN                = 0x00002000,   //No signature algorithm.
  97.             CALG_OID_INFO_CNG_ONLY      = 0xffffffff,   //The algorithm is only implemented in CNG.
  98.                                                         //The macro, IS_SPECIAL_OID_INFO_ALGID, can be used to determine whether
  99.                                                         //a cryptography algorithm is only supported by using the CNG functions.
  100.             CALG_OID_INFO_PARAMETERS    = 0xfffffffe,   //The algorithm is defined in the encoded parameters. The algorithm is only
  101.                                                         //supported by using CNG. The macro, IS_SPECIAL_OID_INFO_ALGID, can be used
  102.                                                         //to determine whether a cryptography algorithm is only supported by
  103.                                                         //using the CNG functions.
  104.             CALG_PCT1_MASTER            = 0x00004c04,   //Used by the Schannel.dll operations system.
  105.                                                         //This ALG_ID should not be used by applications.
  106.             CALG_RC2                    = 0x00006602,   //RC2 block encryption algorithm. This algorithm is supported by the
  107.                                                         //Microsoft Base Cryptographic Provider.
  108.             CALG_RC4                    = 0x00006801,   //RC4 stream encryption algorithm.
  109.                                                         //This algorithm is supported by the Microsoft Base Cryptographic Provider.
  110.             CALG_RC5                    = 0x0000660d,   //RC5 block encryption algorithm.
  111.             CALG_RSA_KEYX               = 0x0000a400,   //RSA public key exchange algorithm. This algorithm is supported by the
  112.                                                         //Microsoft Base Cryptographic Provider.
  113.             CALG_RSA_SIGN               = 0x00002400,   //RSA public key signature algorithm. This algorithm is supported by the
  114.                                                         //Microsoft Base Cryptographic Provider.
  115.             CALG_SCHANNEL_ENC_KEY       = 0x00004c07,   //Used by the Schannel.dll operations system.
  116.                                                         //This ALG_ID should not be used by applications.
  117.             CALG_SCHANNEL_MAC_KEY       = 0x00004c03,   //Used by the Schannel.dll operations system.
  118.                                                         //This ALG_ID should not be used by applications.
  119.             CALG_SCHANNEL_MASTER_HASH   = 0x00004c02,   //Used by the Schannel.dll operations system.
  120.                                                         //This ALG_ID should not be used by applications.
  121.             CALG_SEAL                   = 0x00006802,   //SEAL encryption algorithm. This algorithm is not supported.
  122.             CALG_SHA                    = 0x00008004,   //SHA hashing algorithm. This algorithm is supported by the
  123.                                                         //Microsoft Base Cryptographic Provider.
  124.             CALG_SHA1                   = 0x00008004,   //Same as CALG_SHA. This algorithm is supported by the
  125.                                                         //Microsoft Base Cryptographic Provider.
  126.             CALG_SHA_256                = 0x0000800c,   //256 bit SHA hashing algorithm. This algorithm is supported by
  127.                                                         //Microsoft Enhanced RSA and AES Cryptographic Provider.
  128.                                                         //Windows XP with SP3: This algorithm is supported by the Microsoft
  129.                                                         //Enhanced RSA and AES Cryptographic Provider (Prototype).
  130.                                                         //Windows XP with SP2, Windows XP with SP1 and Windows XP:
  131.                                                         // This algorithm is not supported.
  132.             CALG_SHA_384                = 0x0000800d,   //384 bit SHA hashing algorithm. This algorithm is supported by
  133.                                                         //Microsoft Enhanced RSA and AES Cryptographic Provider. Windows XP with SP3:
  134.                                                         //This algorithm is supported by the Microsoft Enhanced RSA and AES
  135.                                                         //Cryptographic Provider (Prototype). Windows XP with SP2,
  136.                                                         //Windows XP with SP1 and Windows XP: This algorithm is not supported.
  137.             CALG_SHA_512                = 0x0000800e,   //512 bit SHA hashing algorithm. This algorithm is supported by
  138.                                                         //Microsoft Enhanced RSA and AES Cryptographic Provider. Windows XP with SP3:
  139.                                                         //This algorithm is supported by the Microsoft Enhanced RSA and AES
  140.                                                         //Cryptographic Provider (Prototype). Windows XP with SP2,
  141.                                                         //Windows XP with SP1 and Windows XP: This algorithm is not supported.
  142.             CALG_SKIPJACK               = 0x0000660a,   //Skipjack block encryption algorithm (FORTEZZA).
  143.                                                         //This algorithm is not supported.
  144.             CALG_SSL2_MASTER            = 0x00004c05,   //Used by the Schannel.dll operations system.
  145.                                                         //This ALG_ID should not be used by applications.
  146.             CALG_SSL3_MASTER            = 0x00004c01,   //Used by the Schannel.dll operations system.
  147.                                                         //This ALG_ID should not be used by applications.
  148.             CALG_SSL3_SHAMD5            = 0x00008008,   //Used by the Schannel.dll operations system.
  149.                                                         //This ALG_ID should not be used by applications.
  150.             CALG_TEK                    = 0x0000660b,   //TEK (FORTEZZA). This algorithm is not supported.
  151.             CALG_TLS1_MASTER            = 0x00004c06,   //Used by the Schannel.dll operations system.
  152.                                                         //This ALG_ID should not be used by applications.
  153.             CALG_TLS1PRF                = 0x0000800a    //Used by the Schannel.dll operations system.
  154.                                                         //This ALG_ID should not be used by applications.
  155.         }
  156.  
  157.         private enum SchProtocols {
  158.             Zero                = 0,
  159.             PctClient           = 0x00000002,
  160.             PctServer           = 0x00000001,
  161.             Pct                 = (PctClient | PctServer),
  162.             Ssl2Client          = 0x00000008,
  163.             Ssl2Server          = 0x00000004,
  164.             Ssl2                = (Ssl2Client | Ssl2Server),
  165.             Ssl3Client          = 0x00000020,
  166.             Ssl3Server          = 0x00000010,
  167.             Ssl3                = (Ssl3Client | Ssl3Server),
  168.             Tls10Client         = 0x00000080,
  169.             Tls10Server         = 0x00000040,
  170.             Tls10               = (Tls10Client | Tls10Server),
  171.             Tls11Client         = 0x00000200,
  172.             Tls11Server         = 0x00000100,
  173.             Tls11               = (Tls11Client | Tls11Server),
  174.             Tls12Client         = 0x00000800,
  175.             Tls12Server         = 0x00000400,
  176.             Tls12               = (Tls12Client | Tls12Server),
  177.             Ssl3Tls             = (Ssl3 | Tls10),
  178.             UniClient           = unchecked((int)0x80000000),
  179.             UniServer           = 0x40000000,
  180.             Unified             = (UniClient | UniServer),
  181.             ClientMask          = (PctClient | Ssl2Client | Ssl3Client | Tls10Client | Tls11Client | Tls12Client | UniClient),
  182.             ServerMask          = (PctServer | Ssl2Server | Ssl3Server | Tls10Server | Tls11Server | Tls12Server | UniServer)
  183.         };
  184.  
  185.         private enum SchProtocolsSimple : int
  186.         {
  187.             SP_PROT_TLS1_CLIENT = 0x80,       //Transport Layer Security 1.0 client-side.
  188.             SP_PROT_TLS1_SERVER = 0x40,       //Transport Layer Security 1.0 server-side.
  189.             SP_PROT_SSL3_CLIENT = 0x20,       //Secure Sockets Layer 3.0 client-side.
  190.             SP_PROT_SSL3_SERVER = 0x10,       //Secure Sockets Layer 3.0 server-side.
  191.             SP_PROT_TLS1_1_CLIENT = 0x200,    //Transport Layer Security 1.1 client-side.
  192.             SP_PROT_TLS1_1_SERVER = 0x100,    //Transport Layer Security 1.1 server-side.
  193.             SP_PROT_TLS1_2_CLIENT = 0x800,    //Transport Layer Security 1.2 client-side.
  194.             SP_PROT_TLS1_2_SERVER = 0x400,    //Transport Layer Security 1.2 server-side.
  195.             SP_PROT_PCT1_CLIENT = 0x2,        //Private Communications Technology 1.0 client-side. Obsolete.
  196.             SP_PROT_PCT1_SERVER = 0x1,        //Private Communications Technology 1.0 server-side. Obsolete.
  197.             SP_PROT_SSL2_CLIENT = 0x8,        //Secure Sockets Layer 2.0 client-side. Superseded by SP_PROT_TLS1_CLIENT.
  198.             SP_PROT_SSL2_SERVER = 0x4,        //Secure Sockets Layer 2.0 server-side. Superseded by SP_PROT_TLS1_SERVER.
  199.         }
  200.  
  201.        
  202.         public const int SEC_E_OK = 0;
  203.  
  204.         [DllImport("secur32.dll", CharSet = CharSet.Auto, ExactSpelling=true, SetLastError=false)]
  205.         private static extern int QueryContextAttributesW(SSPIHandle contextHandle,
  206.                                                           [In] ContextAttribute attribute,
  207.                                                           [In] [Out] ref SecPkgContext_ConnectionInfo ConnectionInfo);
  208.  
  209.         [StructLayout(LayoutKind.Sequential, Pack=1)]
  210.         private struct SSPIHandle {
  211.             private IntPtr HandleHi;
  212.             private IntPtr HandleLo;
  213.  
  214.             public bool IsZero {
  215.                 get {return HandleHi == IntPtr.Zero && HandleLo == IntPtr.Zero;}
  216.             }
  217.  
  218.             internal void SetToInvalid() {
  219.                 HandleHi = IntPtr.Zero;
  220.                 HandleLo = IntPtr.Zero;
  221.             }
  222.  
  223.             public override string ToString() {
  224.                 { return HandleHi.ToString("x") + ":" + HandleLo.ToString("x");}
  225.             }
  226.         }
  227.  
  228.         //[SuppressUnmanagedCodeSecurity]
  229.         private struct SecPkgContext_ConnectionInfo
  230.         {
  231.             public SchProtocols dwProtocol;
  232.             public ALG_ID aiCipher;
  233.             public int dwCipherStrength;
  234.             public ALG_ID aiHash;
  235.             public int dwHashStrength;
  236.             public ALG_ID aiExch;
  237.             public int dwExchStrength;
  238.         }
  239.  
  240.  
  241. ====================================================================================
  242.  
  243.        SSPIHandle ContextHandle = new SSPIHandle();
  244.        ContextHandle = [m_Worker.Context.m_SecurityContext._handle]
  245.  
  246.        SecPkgContext_ConnectionInfo SecConnectionInfo = new SecPkgContext_ConnectionInfo();
  247.  
  248.        if (QueryContextAttributesW(ContextHandle, ContextAttribute.ConnectionInfo, ref SecConnectionInfo) == SEC_E_OK)
  249.        {
  250.            SslProtocols SslProtocol = (SslProtocols)SecConnectionInfo.dwProtocol;
  251.        }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement