Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

245 lines
4.3 KiB

  1. /*++
  2. Copyright (c) 1997 Microsoft Corporation
  3. Module Name:
  4. security.cxx
  5. Abstract:
  6. IIS MetaBase security routines.
  7. Author:
  8. Keith Moore (keithmo) 13-Mar-1997
  9. Revision History:
  10. --*/
  11. #include "precomp.hxx"
  12. //
  13. // Private data.
  14. //
  15. CRITICAL_SECTION p_SecurityLock;
  16. HCRYPTPROV p_CryptoProvider = CRYPT_NULL;
  17. HCRYPTPROV p_CryptoProvider2 = CRYPT_NULL;
  18. //
  19. // Public functions.
  20. //
  21. BOOL
  22. InitializeMetabaseSecurity(
  23. VOID
  24. )
  25. /*++
  26. Routine Description:
  27. Initializes metabase security.
  28. Arguments:
  29. None.
  30. Return Value:
  31. BOOL - TRUE if successful, FALSE otherwise.
  32. --*/
  33. {
  34. HRESULT result;
  35. INITIALIZE_CRITICAL_SECTION( &p_SecurityLock );
  36. result = ::IISCryptoInitialize();
  37. if( FAILED(result) ) {
  38. DBGPRINTF((
  39. DBG_CONTEXT,
  40. "InitializeMetabaseSecurity: error %lx\n",
  41. result
  42. ));
  43. }
  44. return SUCCEEDED(result);
  45. } // InitializeMetabaseSecurity
  46. VOID
  47. TerminateMetabaseSecurity(
  48. VOID
  49. )
  50. /*++
  51. Routine Description:
  52. Terminates metabase security. Basically, undoes anything done in
  53. InitializeMetabaseSecurity().
  54. Arguments:
  55. None.
  56. Return Value:
  57. None.
  58. --*/
  59. {
  60. HRESULT result;
  61. if( p_CryptoProvider != CRYPT_NULL ) {
  62. result = ::IISCryptoCloseContainer( p_CryptoProvider );
  63. DBG_ASSERT( SUCCEEDED(result) );
  64. }
  65. if( p_CryptoProvider2 != CRYPT_NULL ) {
  66. result = ::IISCryptoCloseContainer( p_CryptoProvider2 );
  67. DBG_ASSERT( SUCCEEDED(result) );
  68. }
  69. result = ::IISCryptoTerminate();
  70. DBG_ASSERT( SUCCEEDED(result) );
  71. DeleteCriticalSection( &p_SecurityLock );
  72. } // TerminateMetabaseSecurity
  73. HRESULT
  74. GetCryptoProvider(
  75. HCRYPTPROV *Provider
  76. )
  77. /*++
  78. Routine Description:
  79. This routine returns a handle to the crypto provider we need to
  80. use, deferring creation of the handle until it is actually needed.
  81. Arguments:
  82. Provider - Receives the handle to the provider.
  83. Return Value:
  84. HRESULT - 0 if successful, !0 otherwise.
  85. --*/
  86. {
  87. HRESULT result = NO_ERROR;
  88. HCRYPTPROV hprov;
  89. //
  90. // If the handle is already initialized, then just use it. Otherwise,
  91. // grab the lock and check it again.
  92. //
  93. hprov = p_CryptoProvider;
  94. if( hprov == CRYPT_NULL ) {
  95. EnterCriticalSection( &p_SecurityLock );
  96. hprov = p_CryptoProvider;
  97. if( hprov == CRYPT_NULL ) {
  98. result = ::IISCryptoGetStandardContainer(
  99. &hprov,
  100. CRYPT_MACHINE_KEYSET
  101. );
  102. if( SUCCEEDED(result) ) {
  103. p_CryptoProvider = hprov;
  104. }
  105. }
  106. LeaveCriticalSection( &p_SecurityLock );
  107. }
  108. *Provider = hprov;
  109. return result;
  110. } // GetCryptoProvider
  111. HRESULT
  112. GetCryptoProvider2(
  113. HCRYPTPROV *Provider
  114. )
  115. /*++
  116. Routine Description:
  117. This routine returns a handle to the crypto provider we need to
  118. use, deferring creation of the handle until it is actually needed.
  119. Arguments:
  120. Provider - Receives the handle to the provider.
  121. Return Value:
  122. HRESULT - 0 if successful, !0 otherwise.
  123. --*/
  124. {
  125. HRESULT hr = S_OK;
  126. HCRYPTPROV hprov;
  127. HCRYPTPROV hprov1 = CRYPT_NULL;
  128. //
  129. // If the handle is already initialized, then just use it. Otherwise,
  130. // grab the lock and check it again.
  131. //
  132. hprov = p_CryptoProvider2;
  133. if ( hprov == CRYPT_NULL )
  134. {
  135. EnterCriticalSection( &p_SecurityLock );
  136. hprov = p_CryptoProvider2;
  137. if ( hprov == CRYPT_NULL )
  138. {
  139. hr = ::IISCryptoGetStandardContainer2( &hprov );
  140. if ( SUCCEEDED( hr ) )
  141. {
  142. p_CryptoProvider2 = hprov;
  143. }
  144. }
  145. LeaveCriticalSection( &p_SecurityLock );
  146. }
  147. if ( SUCCEEDED( hr ) )
  148. {
  149. hr = GetCryptoProvider( &hprov1 );
  150. }
  151. if ( SUCCEEDED( hr ) )
  152. {
  153. hr = IISCryptoCacheHashLength( hprov1 );
  154. }
  155. if ( SUCCEEDED( hr ) )
  156. {
  157. *Provider = hprov;
  158. }
  159. else
  160. {
  161. *Provider = CRYPT_NULL;
  162. }
  163. return hr;
  164. } // GetCryptoProvider2