Source code of Windows XP (NT5)
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.

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