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.

205 lines
4.7 KiB

  1. //+--------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1996-1998
  5. //
  6. // File:
  7. //
  8. // secstore.c
  9. //
  10. // Contents:
  11. //
  12. // History:
  13. //---------------------------------------------------------------------------
  14. #include "secstore.h"
  15. #include <stdlib.h>
  16. #include <tchar.h>
  17. ///////////////////////////////////////////////////////////////////////////////
  18. DWORD
  19. RetrieveKey(
  20. PWCHAR pwszKeyName,
  21. PBYTE * ppbKey,
  22. DWORD * pcbKey )
  23. {
  24. LSA_HANDLE PolicyHandle;
  25. UNICODE_STRING SecretKeyName;
  26. UNICODE_STRING *pSecretData;
  27. DWORD Status;
  28. if( ( NULL == pwszKeyName ) || ( NULL == ppbKey ) || ( NULL == pcbKey ) )
  29. {
  30. return( ERROR_INVALID_PARAMETER );
  31. }
  32. //
  33. // setup the UNICODE_STRINGs for the call.
  34. //
  35. InitLsaString( &SecretKeyName, pwszKeyName );
  36. Status = OpenPolicy( NULL, POLICY_GET_PRIVATE_INFORMATION, &PolicyHandle );
  37. if( Status != ERROR_SUCCESS )
  38. {
  39. return LsaNtStatusToWinError(Status);
  40. }
  41. Status = LsaRetrievePrivateData(
  42. PolicyHandle,
  43. &SecretKeyName,
  44. &pSecretData
  45. );
  46. LsaClose( PolicyHandle );
  47. if( Status != ERROR_SUCCESS )
  48. {
  49. return LsaNtStatusToWinError(Status);
  50. }
  51. if (NULL == pSecretData)
  52. {
  53. return ERROR_INTERNAL_ERROR;
  54. }
  55. if(pSecretData->Length)
  56. {
  57. *ppbKey = ( LPBYTE )LocalAlloc( LPTR, pSecretData->Length );
  58. if( *ppbKey )
  59. {
  60. *pcbKey = pSecretData->Length;
  61. CopyMemory( *ppbKey, pSecretData->Buffer, pSecretData->Length );
  62. Status = ERROR_SUCCESS;
  63. }
  64. else
  65. {
  66. Status = GetLastError();
  67. }
  68. }
  69. else
  70. {
  71. Status = ERROR_FILE_NOT_FOUND;
  72. *pcbKey = 0;
  73. *ppbKey = NULL;
  74. }
  75. SecureZeroMemory( pSecretData->Buffer, pSecretData->Length );
  76. LsaFreeMemory( pSecretData );
  77. return Status;
  78. }
  79. ///////////////////////////////////////////////////////////////////////////////
  80. DWORD
  81. StoreKey(
  82. PWCHAR pwszKeyName,
  83. BYTE * pbKey,
  84. DWORD cbKey )
  85. {
  86. LSA_HANDLE PolicyHandle;
  87. UNICODE_STRING SecretKeyName;
  88. UNICODE_STRING SecretData;
  89. DWORD Status;
  90. if( ( NULL == pwszKeyName ) )
  91. {
  92. return( ERROR_INVALID_PARAMETER );
  93. }
  94. //
  95. // setup the UNICODE_STRINGs for the call.
  96. //
  97. InitLsaString( &SecretKeyName, pwszKeyName );
  98. SecretData.Buffer = ( LPWSTR )pbKey;
  99. SecretData.Length = ( USHORT )cbKey;
  100. SecretData.MaximumLength = ( USHORT )cbKey;
  101. Status = OpenPolicy( NULL, POLICY_CREATE_SECRET, &PolicyHandle );
  102. if( Status != ERROR_SUCCESS )
  103. {
  104. return LsaNtStatusToWinError(Status);
  105. }
  106. Status = LsaStorePrivateData(
  107. PolicyHandle,
  108. &SecretKeyName,
  109. &SecretData
  110. );
  111. LsaClose(PolicyHandle);
  112. return LsaNtStatusToWinError(Status);
  113. }
  114. ///////////////////////////////////////////////////////////////////////////////
  115. DWORD
  116. OpenPolicy(
  117. LPWSTR ServerName,
  118. DWORD DesiredAccess,
  119. PLSA_HANDLE PolicyHandle )
  120. {
  121. LSA_OBJECT_ATTRIBUTES ObjectAttributes;
  122. LSA_UNICODE_STRING ServerString;
  123. PLSA_UNICODE_STRING Server;
  124. //
  125. // Always initialize the object attributes to all zeroes.
  126. //
  127. SecureZeroMemory( &ObjectAttributes, sizeof( ObjectAttributes ) );
  128. if( NULL != ServerName )
  129. {
  130. //
  131. // Make a LSA_UNICODE_STRING out of the LPWSTR passed in
  132. //
  133. InitLsaString( &ServerString, ServerName );
  134. Server = &ServerString;
  135. }
  136. else
  137. {
  138. Server = NULL;
  139. }
  140. //
  141. // Attempt to open the policy.
  142. //
  143. return( LsaOpenPolicy(
  144. Server,
  145. &ObjectAttributes,
  146. DesiredAccess,
  147. PolicyHandle ) );
  148. }
  149. ///////////////////////////////////////////////////////////////////////////////
  150. void
  151. InitLsaString(
  152. PLSA_UNICODE_STRING LsaString,
  153. LPWSTR String )
  154. {
  155. DWORD StringLength;
  156. if( NULL == String )
  157. {
  158. LsaString->Buffer = NULL;
  159. LsaString->Length = 0;
  160. LsaString->MaximumLength = 0;
  161. return;
  162. }
  163. StringLength = lstrlenW( String );
  164. LsaString->Buffer = String;
  165. LsaString->Length = ( USHORT ) StringLength * sizeof( WCHAR );
  166. LsaString->MaximumLength=( USHORT )( StringLength + 1 ) * sizeof( WCHAR );
  167. }