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.

266 lines
8.0 KiB

  1. //+--------------------------------------------------------------------------
  2. // File: certsd.h
  3. // Contents: CA's security descriptor class declaration
  4. //---------------------------------------------------------------------------
  5. #ifndef __CERTSD_H__
  6. #define __CERTSD_H__
  7. namespace CertSrv
  8. {
  9. typedef struct _SID_LIST
  10. {
  11. DWORD dwSidCount;
  12. DWORD SidListStart[ANYSIZE_ARRAY];
  13. } SID_LIST, *PSID_LIST;
  14. HRESULT GetWellKnownSID(
  15. PSID *ppSid,
  16. SID_IDENTIFIER_AUTHORITY *pAuth,
  17. BYTE SubauthorityCount,
  18. DWORD SubAuthority1,
  19. DWORD SubAuthority2=0,
  20. DWORD SubAuthority3=0,
  21. DWORD SubAuthority4=0,
  22. DWORD SubAuthority5=0,
  23. DWORD SubAuthority6=0,
  24. DWORD SubAuthority7=0,
  25. DWORD SubAuthority8=0);
  26. // caller is responsible for LocalFree'ing PSID
  27. HRESULT GetEveryoneSID(PSID *ppSid);
  28. HRESULT GetLocalSystemSID(PSID *ppSid);
  29. HRESULT GetBuiltinAdministratorsSID(PSID *ppSid);
  30. HRESULT GetLocalSID(PSID *ppSid);
  31. HRESULT GetNetworkSID(PSID *ppSid);
  32. // This class wraps a SD with single writer multiple reader access control on
  33. // it. Allows any number of threads to LockGet() the pointer to the SD if no
  34. // thread is in the middle of a Set(). Set() is blocked until all threads
  35. // that already retrieved the SD released it (calling Unlock)
  36. //
  37. // Also provides the support for persistently saving/loading the SD to registy
  38. class CProtectedSecurityDescriptor
  39. {
  40. public:
  41. CProtectedSecurityDescriptor() :
  42. m_pSD(NULL),
  43. m_fInitialized(false),
  44. m_cReaders(0),
  45. m_hevtNoReaders(NULL),
  46. m_pcwszSanitizedName(NULL),
  47. m_pcwszPersistRegVal(NULL)
  48. {};
  49. ~CProtectedSecurityDescriptor()
  50. {
  51. Uninitialize();
  52. }
  53. void Uninitialize()
  54. {
  55. if(m_pSD)
  56. {
  57. LocalFree(m_pSD);
  58. m_pSD = NULL;
  59. }
  60. if(m_hevtNoReaders)
  61. {
  62. CloseHandle(m_hevtNoReaders);
  63. m_hevtNoReaders = NULL;
  64. }
  65. if(IsInitialized())
  66. {
  67. DeleteCriticalSection(&m_csWrite);
  68. }
  69. m_pcwszSanitizedName = NULL;
  70. m_fInitialized = false;
  71. m_cReaders = 0;
  72. }
  73. BOOL IsInitialized() const { return m_fInitialized;}
  74. // init, loading SD from the registry
  75. HRESULT Initialize(LPCWSTR pwszSanitizedName);
  76. // init from supplied SD
  77. HRESULT Initialize(const PSECURITY_DESCRIPTOR pSD, LPCWSTR pwszSanitizedName);
  78. HRESULT InitializeFromTemplate(LPCWSTR pcwszTemplate, LPCWSTR pwszSanitizedName);
  79. HRESULT Set(const PSECURITY_DESCRIPTOR pSD);
  80. HRESULT LockGet(PSECURITY_DESCRIPTOR *ppSD);
  81. HRESULT Unlock();
  82. PSECURITY_DESCRIPTOR Get() { return m_pSD; };
  83. // load SD from registry
  84. HRESULT Load();
  85. // save SD to registry
  86. HRESULT Save();
  87. // delete SD from registry
  88. HRESULT Delete();
  89. LPCWSTR GetPersistRegistryVal() { return m_pcwszPersistRegVal;}
  90. void ImportResourceStrings(LPCWSTR *pcwszStrings)
  91. {m_pcwszResources = pcwszStrings;};
  92. protected:
  93. HRESULT Init(LPCWSTR pwszSanitizedName);
  94. HRESULT SetSD(PSECURITY_DESCRIPTOR pSD);
  95. PSECURITY_DESCRIPTOR m_pSD;
  96. bool m_fInitialized;
  97. LONG m_cReaders;
  98. HANDLE m_hevtNoReaders;
  99. CRITICAL_SECTION m_csWrite;
  100. LPCWSTR m_pcwszSanitizedName; // no free
  101. LPCWSTR m_pcwszPersistRegVal; // no free
  102. static LPCWSTR const *m_pcwszResources; // no free
  103. }; //class CProtectedSecurityDescriptor
  104. // The class stores a list of officers/groups and the principals they are
  105. // allowed to manage certificates for:
  106. //
  107. // officerSID1 -> clientSID1, clientSID2...
  108. // officerSID2 -> clientSID3, clientSID4...
  109. //
  110. // The information is stored as a DACL containing callback ACEs .
  111. // The officer SID is stored as in the ACE's SID and the list of client
  112. // SIDs are stored in the custom data space following the officer SID
  113. // (see definition of _ACCESS_*_CALLBACK_ACE)
  114. //
  115. // The DACL will be used to AccessCheck if an officer is allowed to perform
  116. // an action over a certificate.
  117. //
  118. // The SD contains only the officer DACL, SACL or other data is not used.
  119. class COfficerRightsSD : public CProtectedSecurityDescriptor
  120. {
  121. public:
  122. COfficerRightsSD() : m_fEnabled(FALSE)
  123. { m_pcwszPersistRegVal = wszREGOFFICERRIGHTS; }
  124. HRESULT InitializeEmpty();
  125. HRESULT Initialize(LPCWSTR pwszSanitizedName);
  126. // The officer rights have to be in sync with the CA security descriptor.
  127. // An officer ACE for a certain SID can exist only if the principal is
  128. // an officer as defined by the CA SD.
  129. // Merge sets the internal officer DACL making sure it's in sync
  130. // with the CA SD:
  131. // - removes any ACE found in the officer DACL which is not present as an
  132. // allow ACE in the CA DACL
  133. // - add an Everyone ACE in the officer DACL for each allow ACE in CA DACL
  134. // that is not already present
  135. HRESULT Merge(
  136. PSECURITY_DESCRIPTOR pOfficerSD,
  137. PSECURITY_DESCRIPTOR pCASD);
  138. // Same as above but using the internal officer SD. Used to generate the
  139. // initial officer SD and to update it when CA SD changes
  140. HRESULT Adjust(
  141. PSECURITY_DESCRIPTOR pCASD);
  142. BOOL IsEnabled() { return m_fEnabled; }
  143. void SetEnable(BOOL fEnable) { m_fEnabled = fEnable;}
  144. HRESULT Save();
  145. HRESULT Load();
  146. HRESULT Validate() { return S_OK; }
  147. static HRESULT ConvertToString(
  148. IN PSECURITY_DESCRIPTOR pSD,
  149. OUT LPWSTR& rpwszSD);
  150. protected:
  151. static HRESULT ConvertAceToString(
  152. IN PACCESS_ALLOWED_CALLBACK_ACE pAce,
  153. OUT OPTIONAL PDWORD pdwSize,
  154. IN OUT OPTIONAL LPWSTR pwszSD);
  155. BOOL m_fEnabled;
  156. }; // class COfficerRightsSD
  157. class CCertificateAuthoritySD : public CProtectedSecurityDescriptor
  158. {
  159. public:
  160. CCertificateAuthoritySD() :
  161. m_pDefaultDSSD(NULL),
  162. m_pDefaultServiceSD(NULL),
  163. m_pDefaultDSAcl(NULL),
  164. m_pDefaultServiceAcl(NULL),
  165. m_pwszComputerSID(NULL)
  166. { m_pcwszPersistRegVal = wszREGCASECURITY; }
  167. ~CCertificateAuthoritySD()
  168. {
  169. if(m_pDefaultDSSD)
  170. LocalFree(m_pDefaultDSSD);
  171. if(m_pDefaultServiceSD)
  172. LocalFree(m_pDefaultServiceSD);
  173. if(m_pwszComputerSID)
  174. LocalFree(m_pwszComputerSID);
  175. }
  176. // Sets a new CA SD. Uses the new DACL but keeps the old owner, group and
  177. // SACL.
  178. // Also rebuilds the DACL for objects CA owns (eg DS pKIEnrollmentService,
  179. // service). The new DACL contains a default DACL plus additional aces
  180. // depending on the object:
  181. // DS - add an enroll ace for each enroll ace found in CA DACL
  182. // Service - add a full control ace for each CA admin ace
  183. HRESULT Set(const PSECURITY_DESCRIPTOR pSD, bool fSetDSSecurity);
  184. static HRESULT Validate(const PSECURITY_DESCRIPTOR pSD);
  185. HRESULT ResetSACL();
  186. HRESULT MapAndSetDaclOnObjects(bool fSetDSSecurity);
  187. // Upgrade SD from Win2k.
  188. HRESULT UpgradeWin2k(bool fUseEnterpriseAcl);
  189. static HRESULT ConvertToString(
  190. IN PSECURITY_DESCRIPTOR pSD,
  191. OUT LPWSTR& rpwszSD);
  192. protected:
  193. enum ObjType
  194. {
  195. ObjType_DS,
  196. ObjType_Service,
  197. };
  198. HRESULT MapAclGetSize(PVOID pAce, ObjType type, DWORD& dwSize);
  199. HRESULT MapAclAddAce(PACL pAcl, ObjType type, PVOID pAce);
  200. HRESULT SetDefaultAcl(ObjType type);
  201. HRESULT SetComputerSID();
  202. HRESULT MapAclSetOnDS(const PACL pAcl);
  203. HRESULT MapAclSetOnService(const PACL pAcl);
  204. DWORD GetUpgradeAceSizeAndType(PVOID pAce, DWORD *pdwType, PSID *ppSid);
  205. static HRESULT ConvertAceToString(
  206. IN PACCESS_ALLOWED_ACE pAce,
  207. OUT OPTIONAL PDWORD pdwSize,
  208. IN OUT OPTIONAL LPWSTR pwszSD);
  209. PSECURITY_DESCRIPTOR m_pDefaultDSSD;
  210. PSECURITY_DESCRIPTOR m_pDefaultServiceSD;
  211. PACL m_pDefaultDSAcl; // no free
  212. PACL m_pDefaultServiceAcl; // no free
  213. LPWSTR m_pwszComputerSID;
  214. };
  215. } // namespace CertSrv
  216. #endif //__CERTSD_H__