JwaWinSafer.pas
上传用户:davidchvip
上传日期:2009-07-28
资源大小:1749k
文件大小:24k
源码类别:

Windows编程

开发平台:

Delphi

  1. {******************************************************************************}
  2. {                                                                       }
  3. { Windows Safer API interface Unit for Object Pascal                           }
  4. {                                                                       }
  5. { Portions created by Microsoft are Copyright (C) 1995-2001 Microsoft          }
  6. { Corporation. All Rights Reserved.                                            }
  7. {                 }
  8. { The original file is: winsafer.h, released Nov 2001. The original Pascal     }
  9. { code is: WinSafer.pas, released Februari 2002. The initial developer of the  }
  10. { Pascal code is Marcel van Brakel (brakelm@chello.nl).                        }
  11. {                                                                              }
  12. { Portions created by Marcel van Brakel are Copyright (C) 1999-2001            }
  13. { Marcel van Brakel. All Rights Reserved.                                      }
  14. {                 }
  15. { Obtained through: Joint Endeavour of Delphi Innovators (Project JEDI)        }
  16. {                }
  17. { You may retrieve the latest version of this file at the Project JEDI home    }
  18. { page, located at http://delphi-jedi.org or my personal homepage located at   }
  19. { http://members.chello.nl/m.vanbrakel2                                        }
  20. {                }
  21. { The contents of this file are used with permission, subject to the Mozilla   }
  22. { Public License Version 1.1 (the "License"); you may not use this file except }
  23. { in compliance with the License. You may obtain a copy of the License at      }
  24. { http://www.mozilla.org/MPL/MPL-1.1.html                                      }
  25. {                                                                              }
  26. { Software distributed under the License is distributed on an "AS IS" basis,   }
  27. { WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for }
  28. { the specific language governing rights and limitations under the License.    }
  29. {                                                                              }
  30. { Alternatively, the contents of this file may be used under the terms of the  }
  31. { GNU Lesser General Public License (the  "LGPL License"), in which case the   }
  32. { provisions of the LGPL License are applicable instead of those above.        }
  33. { If you wish to allow use of your version of this file only under the terms   }
  34. { of the LGPL License and not to allow others to use your version of this file }
  35. { under the MPL, indicate your decision by deleting  the provisions above and  }
  36. { replace  them with the notice and other provisions required by the LGPL      }
  37. { License.  If you do not delete the provisions above, a recipient may use     }
  38. { your version of this file under either the MPL or the LGPL License.          }
  39. {                 }
  40. { For more information about the LGPL: http://www.gnu.org/copyleft/lesser.html }
  41. {                 }
  42. {******************************************************************************}
  43. unit JwaWinSafer;
  44. {$WEAKPACKAGEUNIT}
  45. {$HPPEMIT ''}
  46. {$HPPEMIT '#include "winsafer.h"'}
  47. {$HPPEMIT ''}
  48. {$I WINDEFINES.INC}
  49. interface
  50. uses
  51.   JwaWinCrypt, JwaWinType;
  52. //
  53. // Opaque datatype for representing handles to Safer objects.
  54. //
  55. type
  56.   SAFER_LEVEL_HANDLE = HANDLE;
  57.   {$EXTERNALSYM SAFER_LEVEL_HANDLE}
  58.   PSAFER_LEVEL_HANDLE = ^SAFER_LEVEL_HANDLE;
  59.   {$EXTERNALSYM PSAFER_LEVEL_HANDLE}
  60.   TSaferLevelHandle = SAFER_LEVEL_HANDLE;
  61.   PSaferLevelHandle = PSAFER_LEVEL_HANDLE;
  62. //
  63. // Constants to represent scope with SaferCreateLevel and others.
  64. //
  65. const
  66.   SAFER_SCOPEID_MACHINE = 1;
  67.   {$EXTERNALSYM SAFER_SCOPEID_MACHINE}
  68.   SAFER_SCOPEID_USER    = 2;
  69.   {$EXTERNALSYM SAFER_SCOPEID_USER}
  70. //
  71. // Pre-defined levels that can be used with SaferCreateLevel
  72. //
  73.   SAFER_LEVELID_FULLYTRUSTED = $40000;
  74.   {$EXTERNALSYM SAFER_LEVELID_FULLYTRUSTED}
  75.   SAFER_LEVELID_NORMALUSER   = $20000;
  76.   {$EXTERNALSYM SAFER_LEVELID_NORMALUSER}
  77.   SAFER_LEVELID_CONSTRAINED  = $10000;
  78.   {$EXTERNALSYM SAFER_LEVELID_CONSTRAINED}
  79.   SAFER_LEVELID_UNTRUSTED    = $01000;
  80.   {$EXTERNALSYM SAFER_LEVELID_UNTRUSTED}
  81.   SAFER_LEVELID_DISALLOWED   = $00000;
  82.   {$EXTERNALSYM SAFER_LEVELID_DISALLOWED}
  83. //
  84. // Flags to use when creating/opening a Level with SaferCreateLevel
  85. //
  86.   SAFER_LEVEL_OPEN = 1;
  87.   {$EXTERNALSYM SAFER_LEVEL_OPEN}
  88. //
  89. // Maximum string size.
  90. //
  91.   SAFER_MAX_FRIENDLYNAME_SIZE = 256;
  92.   {$EXTERNALSYM SAFER_MAX_FRIENDLYNAME_SIZE}
  93.   SAFER_MAX_DESCRIPTION_SIZE  = 256;
  94.   {$EXTERNALSYM SAFER_MAX_DESCRIPTION_SIZE}
  95.   SAFER_MAX_HASH_SIZE         = 64;
  96.   {$EXTERNALSYM SAFER_MAX_HASH_SIZE}
  97. //
  98. // Flags to use with SaferComputeTokenFromLevel.
  99. //
  100.   SAFER_TOKEN_NULL_IF_EQUAL = $00000001;
  101.   {$EXTERNALSYM SAFER_TOKEN_NULL_IF_EQUAL}
  102.   SAFER_TOKEN_COMPARE_ONLY  = $00000002;
  103.   {$EXTERNALSYM SAFER_TOKEN_COMPARE_ONLY}
  104.   SAFER_TOKEN_MAKE_INERT    = $00000004;
  105.   {$EXTERNALSYM SAFER_TOKEN_MAKE_INERT}
  106.   SAFER_TOKEN_WANT_FLAGS    = $00000008;
  107.   {$EXTERNALSYM SAFER_TOKEN_WANT_FLAGS}
  108. //
  109. // Flags for specifying what criteria within SAFER_CODE_PROPERTIES to evaluate
  110. // when finding code identity with SaferIdentifyLevel.
  111. //
  112.   SAFER_CRITERIA_IMAGEPATH    = $00001;
  113.   {$EXTERNALSYM SAFER_CRITERIA_IMAGEPATH}
  114.   SAFER_CRITERIA_NOSIGNEDHASH = $00002;
  115.   {$EXTERNALSYM SAFER_CRITERIA_NOSIGNEDHASH}
  116.   SAFER_CRITERIA_IMAGEHASH    = $00004;
  117.   {$EXTERNALSYM SAFER_CRITERIA_IMAGEHASH}
  118.   SAFER_CRITERIA_AUTHENTICODE = $00008;
  119.   {$EXTERNALSYM SAFER_CRITERIA_AUTHENTICODE}
  120.   SAFER_CRITERIA_URLZONE      = $00010;
  121.   {$EXTERNALSYM SAFER_CRITERIA_URLZONE}
  122.   SAFER_CRITERIA_IMAGEPATH_NT = $01000;
  123.   {$EXTERNALSYM SAFER_CRITERIA_IMAGEPATH_NT}
  124. //
  125. // Code image information structure passed to SaferIdentifyLevel.
  126. //
  127. // #include <pshpack8.h> todo
  128. type
  129.   _SAFER_CODE_PROPERTIES = record
  130.     //
  131.     // Must be initialized to be the size of this structure,
  132.     // for the purposes of future/backwards compatibility.
  133.     //
  134.     cbSize: DWORD;
  135.     //
  136.     // Must be initialized to the types of criteria that should
  137.     // be considered when evaluating this structure.  This can be
  138.     // a combination of the SAFER_CRITERIA_xxxx flags.  If not enough
  139.     // of the structure elements needed to evaluate the criteria
  140.     // types indicated were supplied, then some of those criteria
  141.     // flags may be silently ignored.  Specifying 0 for this value
  142.     // will cause the entire structure's contents to be ignored.
  143.     //
  144.     dwCheckFlags: DWORD;
  145.     //
  146.     // Optionally specifies the fully-qualified path and filename
  147.     // to be used for discrimination checks based on the path.
  148.     // The ImagePath will additionally be used to open and read the
  149.     // file to identify any other discrimination criteria that was
  150.     // unsupplied in this structure.
  151.     //
  152.     ImagePath: LPCWSTR;
  153.     //
  154.     // Optionally specifies a file handle that has been opened to
  155.     // code image with at least GENERIC_READ access.  The handle will
  156.     // be used instead of explicitly opening the file again to compute
  157.     // other discrimination criteria that was unsupplied in this structure.
  158.     //
  159.     hImageFileHandle: HANDLE;
  160.     //
  161.     // Optionally specifies the pre-determined Internet Explorer
  162.     // security zone.  These enums are defined within urlmon.h
  163.     // For example: URLZONE_LOCAL_MACHINE, URLZONE_INTRANET,
  164.     //   URLZONE_TRUSTED, URLZONE_INTERNET, or URLZONE_UNTRUSTED.
  165.     //
  166.     UrlZoneId: DWORD;
  167.     //
  168.     // Optionally specifies the pre-computed hash of the image.
  169.     // The supplied hash is interpreted as being valid if ImageSize
  170.     // is non-zero and dwImageHashSize is non-zero and HashAlgorithm
  171.     // represents a valid hashing algorithm from wincrypt.h
  172.     //
  173.     // If the supplied hash fails to meet the conditions above, then
  174.     // the hash will be automatically computed against:
  175.     //      1) by using ImageSize and pByteBlock if both are non-zero.
  176.     //      2) by using hImageFileHandle if it is non-null.
  177.     //      3) by attempting to open ImagePath if it is non-null.
  178.     //
  179.     ImageHash: array [0..SAFER_MAX_HASH_SIZE - 1] of BYTE;
  180.     dwImageHashSize: DWORD;
  181.     ImageSize: LARGE_INTEGER;
  182.     HashAlgorithm: ALG_ID;
  183.     //
  184.     // Optionally specifies a memory block of memory representing
  185.     // the image for which the trust is being requested for.  When
  186.     // this member is specified, ImageSize must also be supplied.
  187.     //
  188.     pByteBlock: LPBYTE;
  189.     //
  190.     // Optionally gives the arguments used for Authenticode signer
  191.     // certificate verification.  These arguments are supplied to the
  192.     // WinVerifyTrust() API and control the user-interface prompting
  193.     // to accept untrusted certificates.
  194.     //
  195.     hWndParent: HWND;
  196.     dwWVTUIChoice: DWORD;
  197.   end;
  198.   {$EXTERNALSYM _SAFER_CODE_PROPERTIES}
  199.   SAFER_CODE_PROPERTIES = _SAFER_CODE_PROPERTIES;
  200.   {$EXTERNALSYM SAFER_CODE_PROPERTIES}
  201.   PSAFER_CODE_PROPERTIES = ^SAFER_CODE_PROPERTIES;
  202.   {$EXTERNALSYM PSAFER_CODE_PROPERTIES}
  203.   TSaferCodeProperties = SAFER_CODE_PROPERTIES;
  204.   PSaferCodeProperties = PSAFER_CODE_PROPERTIES;
  205. // #include <poppack.h>
  206. //
  207. // Masks for the per-identity WinSafer flags
  208. //
  209. const
  210.   SAFER_POLICY_JOBID_MASK                 = DWORD($FF000000);
  211.   {$EXTERNALSYM SAFER_POLICY_JOBID_MASK}
  212.   SAFER_POLICY_JOBID_CONSTRAINED          = $04000000;
  213.   {$EXTERNALSYM SAFER_POLICY_JOBID_CONSTRAINED}
  214.   SAFER_POLICY_JOBID_UNTRUSTED            = $03000000;
  215.   {$EXTERNALSYM SAFER_POLICY_JOBID_UNTRUSTED}
  216.   SAFER_POLICY_ONLY_EXES                  = $00010000;
  217.   {$EXTERNALSYM SAFER_POLICY_ONLY_EXES}
  218.   SAFER_POLICY_SANDBOX_INERT              = $00020000;
  219.   {$EXTERNALSYM SAFER_POLICY_SANDBOX_INERT}
  220.   SAFER_POLICY_UIFLAGS_MASK               = $000000FF;
  221.   {$EXTERNALSYM SAFER_POLICY_UIFLAGS_MASK}
  222.   SAFER_POLICY_UIFLAGS_INFORMATION_PROMPT = $00000001;
  223.   {$EXTERNALSYM SAFER_POLICY_UIFLAGS_INFORMATION_PROMPT}
  224.   SAFER_POLICY_UIFLAGS_OPTION_PROMPT      = $00000002;
  225.   {$EXTERNALSYM SAFER_POLICY_UIFLAGS_OPTION_PROMPT}
  226. //
  227. // Information classes on the overall policy that can be queried
  228. // with SaferSet/GetPolicyInformation and set at different
  229. // policy scopes based on access of the caller.
  230. //
  231. type
  232.   _SAFER_POLICY_INFO_CLASS = (
  233.     spicFiller0,
  234.     //
  235.     // Accesses the list of all Levels defined in a policy.
  236.     // The corresponding data element is a buffer that is filled
  237.     // with multiple DWORDs, each representing the LevelIds that
  238.     // are defined within this scope.
  239.     //
  240.     
  241.     SaferPolicyLevelList,
  242.     //
  243.     // for transparent enforcement of policy in the execution
  244.     // framework -- will be used by native code execution but can
  245.     // be used by any policy enforcement environment.
  246.     // Corresponding data element is a DWORD holding a Boolean value.
  247.     //
  248.     
  249.     SaferPolicyEnableTransparentEnforcement,
  250.     //
  251.     // Returns the name of the Level that has been designed
  252.     // as the default level within the specified scope.
  253.     // The corresponding data element is a single DWORD buffer
  254.     // representing the LevelId of the default Level.  If no
  255.     // level has been configured to be the default, then the
  256.     // GetInfo API will return FALSE and GetLastError will
  257.     // return ERROR_NOT_FOUND.
  258.     //
  259.     
  260.     SaferPolicyDefaultLevel,
  261.     //
  262.     // Returns whether Code Identities or Default Level within the
  263.     // user scope can be considered during identification.
  264.     //
  265.     
  266.     SaferPolicyEvaluateUserScope,
  267.     
  268.     //
  269.     // Control Flags for for safer policy scope.
  270.     //
  271.     
  272.     SaferPolicyScopeFlags);
  273.   {$EXTERNALSYM _SAFER_POLICY_INFO_CLASS}
  274.   SAFER_POLICY_INFO_CLASS = _SAFER_POLICY_INFO_CLASS;
  275.   {$EXTERNALSYM SAFER_POLICY_INFO_CLASS}
  276.   TSaferPolicyInfoClass = SAFER_POLICY_INFO_CLASS;
  277. //
  278. // Enumerations used for retrieving specific information about a
  279. // single authorization Level via SaferGet/SetInformationFromLevel.
  280. //
  281.   _SAFER_OBJECT_INFO_CLASS = (
  282.     soicFillter0,
  283.     SaferObjectLevelId,               // get: DWORD
  284.     SaferObjectScopeId,                   // get: DWORD
  285.     SaferObjectFriendlyName,              // get/set: LPCWSTR 
  286.     SaferObjectDescription,               // get/set: LPCWSTR
  287.     SaferObjectBuiltin,                   // get: DWORD boolean
  288.     SaferObjectDisallowed,                // get: DWORD boolean
  289.     SaferObjectDisableMaxPrivilege,       // get: DWORD boolean
  290.     SaferObjectInvertDeletedPrivileges,   // get: DWORD boolean
  291.     SaferObjectDeletedPrivileges,         // get: TOKEN_PRIVILEGES
  292.     SaferObjectDefaultOwner,              // get: TOKEN_OWNER
  293.     SaferObjectSidsToDisable,             // get: TOKEN_GROUPS
  294.     SaferObjectRestrictedSidsInverted,    // get: TOKEN_GROUPS
  295.     SaferObjectRestrictedSidsAdded,       // get: TOKEN_GROUPS
  296.     //
  297.     // To enumerate all identities, call GetInfo with
  298.     //      SaferObjectAllIdentificationGuids.
  299.     //
  300.     
  301.     SaferObjectAllIdentificationGuids,    // get: SAFER_IDENTIFICATION_GUIDS
  302.     //
  303.     // To create a new identity, call SetInfo with
  304.     //      SaferObjectSingleIdentification with a new
  305.     //      unique GUID that you have generated.
  306.     // To get details on a single identity, call GetInfo with
  307.     //      SaferObjectSingleIdentification with desired GUID.
  308.     // To modify details of a single identity, call SetInfo with
  309.     //      SaferObjectSingleIdentification with desired info and GUID.
  310.     // To delete an identity, call SetInfo with
  311.     //      SaferObjectSingleIdentification with the
  312.     //      header.dwIdentificationType set to 0.
  313.     //
  314.     
  315.     SaferObjectSingleIdentification,      // get/set: SAFER_IDENTIFICATION_*
  316.     SaferObjectExtendedError);            // get: DWORD dwError
  317.   {$EXTERNALSYM _SAFER_OBJECT_INFO_CLASS}
  318.   SAFER_OBJECT_INFO_CLASS = _SAFER_OBJECT_INFO_CLASS;
  319.   {$EXTERNALSYM SAFER_OBJECT_INFO_CLASS}
  320.   TSaferObjectInfoClass = SAFER_OBJECT_INFO_CLASS;
  321. //
  322. // Structures and enums used by the SaferGet/SetLevelInformation APIs.
  323. //
  324. // #include <pshpack8.h> todo
  325.   _SAFER_IDENTIFICATION_TYPES = (
  326.     SaferIdentityDefault,
  327.     SaferIdentityTypeImageName,
  328.     SaferIdentityTypeImageHash,
  329.     SaferIdentityTypeUrlZone,
  330.     SaferIdentityTypeCertificate);
  331.   {$EXTERNALSYM _SAFER_IDENTIFICATION_TYPES}
  332.   SAFER_IDENTIFICATION_TYPES = _SAFER_IDENTIFICATION_TYPES;
  333.   {$EXTERNALSYM SAFER_IDENTIFICATION_TYPES}
  334.   TSaferIdentificationTypes = SAFER_IDENTIFICATION_TYPES;
  335.   _SAFER_IDENTIFICATION_HEADER = record
  336.     //
  337.     // indicates the type of the structure, one of SaferIdentityType*
  338.     //
  339.     dwIdentificationType: SAFER_IDENTIFICATION_TYPES;
  340.     //
  341.     // size of the whole structure, not just the common header.
  342.     //
  343.     cbStructSize: DWORD;
  344.     //
  345.     // the unique GUID of the Identity in question.
  346.     //
  347.     IdentificationGuid: GUID;
  348.     //
  349.     // last change of this identification.
  350.     //
  351.     lastModified: FILETIME;
  352.   end;
  353.   {$EXTERNALSYM _SAFER_IDENTIFICATION_HEADER}
  354.   SAFER_IDENTIFICATION_HEADER = _SAFER_IDENTIFICATION_HEADER;
  355.   {$EXTERNALSYM SAFER_IDENTIFICATION_HEADER}
  356.   PSAFER_IDENTIFICATION_HEADER = ^SAFER_IDENTIFICATION_HEADER;
  357.   {$EXTERNALSYM PSAFER_IDENTIFICATION_HEADER}
  358.   TSaferIdentificationHeader = SAFER_IDENTIFICATION_HEADER;
  359.   PSaferIdentificationHeader = PSAFER_IDENTIFICATION_HEADER;
  360.   _SAFER_PATHNAME_IDENTIFICATION = record
  361.     //
  362.     // header.dwIdentificationType must be SaferIdentityTypeImageName
  363.     // header.cbStructSize must be sizeof(SAFER_PATHNAME_IDENTIFICATION)
  364.     //
  365.     header: SAFER_IDENTIFICATION_HEADER;
  366.     //
  367.     // user-entered description
  368.     //
  369.     Description: array [0..SAFER_MAX_DESCRIPTION_SIZE - 1] of WCHAR;
  370.     //
  371.     // filepath or name, possibly with vars
  372.     //
  373.     ImageName: PWCHAR;
  374.     //
  375.     // any combo of SAFER_POL_SAFERFLAGS_*
  376.     //
  377.     dwSaferFlags: DWORD;
  378.   end;
  379.   {$EXTERNALSYM _SAFER_PATHNAME_IDENTIFICATION}
  380.   SAFER_PATHNAME_IDENTIFICATION = _SAFER_PATHNAME_IDENTIFICATION;
  381.   {$EXTERNALSYM SAFER_PATHNAME_IDENTIFICATION}
  382.   PSAFER_PATHNAME_IDENTIFICATION = ^SAFER_PATHNAME_IDENTIFICATION;
  383.   {$EXTERNALSYM PSAFER_PATHNAME_IDENTIFICATION}
  384.   TSaferPathNameIdentification = SAFER_PATHNAME_IDENTIFICATION;
  385.   PSaferPathNameIdentification = PSAFER_PATHNAME_IDENTIFICATION;
  386.   _SAFER_HASH_IDENTIFICATION = record
  387.     //
  388.     // header.dwIdentificationType must be SaferIdentityTypeImageHash
  389.     // header.cbStructSize must be sizeof(SAFER_HASH_IDENTIFICATION)
  390.     //
  391.     header: SAFER_IDENTIFICATION_HEADER;
  392.     //
  393.     // user-entered friendly name, initially from file's resources.
  394.     //
  395.     Description: array [0..SAFER_MAX_DESCRIPTION_SIZE - 1] of WCHAR;
  396.     //
  397.     // user-entered description.
  398.     //
  399.     FriendlyName: array [0..SAFER_MAX_FRIENDLYNAME_SIZE - 1] of WCHAR;
  400.     //
  401.     // amount of ImageHash actually used, in bytes (MD5 is 16 bytes).
  402.     //
  403.     HashSize: DWORD;
  404.     //
  405.     // computed hash data itself.
  406.     //
  407.     ImageHash: array [0..SAFER_MAX_HASH_SIZE - 1] of BYTE;
  408.     //
  409.     // algorithm in which the hash was computed (CALG_MD5, etc).
  410.     //
  411.     HashAlgorithm: ALG_ID;
  412.     //
  413.     // size of the original file in bytes.
  414.     //
  415.     ImageSize: LARGE_INTEGER;
  416.     //
  417.     // any combo of SAFER_POL_SAFERFLAGS_*
  418.     //
  419.     dwSaferFlags: DWORD;
  420.   end;
  421.   {$EXTERNALSYM _SAFER_HASH_IDENTIFICATION}
  422.   SAFER_HASH_IDENTIFICATION = _SAFER_HASH_IDENTIFICATION;
  423.   {$EXTERNALSYM SAFER_HASH_IDENTIFICATION}
  424.   PSAFER_HASH_IDENTIFICATION = ^SAFER_HASH_IDENTIFICATION;
  425.   {$EXTERNALSYM PSAFER_HASH_IDENTIFICATION}
  426.   TSaferHashIdentification = SAFER_HASH_IDENTIFICATION;
  427.   PSaferHashIdentification = PSAFER_HASH_IDENTIFICATION;
  428.   _SAFER_URLZONE_IDENTIFICATION = record
  429.     //
  430.     // header.dwIdentificationType must be SaferIdentityTypeUrlZone
  431.     // header.cbStructSize must be sizeof(SAFER_URLZONE_IDENTIFICATION)
  432.     //
  433.     header: SAFER_IDENTIFICATION_HEADER;
  434.     //
  435.     // any single URLZONE_* from urlmon.h
  436.     //
  437.     UrlZoneId: DWORD;
  438.     //
  439.     // any combo of SAFER_POLICY_*
  440.     //
  441.     dwSaferFlags: DWORD;
  442.   end;
  443.   {$EXTERNALSYM _SAFER_URLZONE_IDENTIFICATION}
  444.   SAFER_URLZONE_IDENTIFICATION = _SAFER_URLZONE_IDENTIFICATION;
  445.   {$EXTERNALSYM SAFER_URLZONE_IDENTIFICATION}
  446.   PSAFER_URLZONE_IDENTIFICATION = ^SAFER_URLZONE_IDENTIFICATION;
  447.   {$EXTERNALSYM PSAFER_URLZONE_IDENTIFICATION}
  448.   TSaferUrlZoneIdentification = SAFER_URLZONE_IDENTIFICATION;
  449.   PSaferUrlZoneIdentification = PSAFER_URLZONE_IDENTIFICATION;
  450. // #include <poppack.h>
  451. //
  452. // Functions related to querying and setting the global policy
  453. // controls to disable transparent enforcement, and perform level
  454. // enumeration operations.
  455. //
  456. function SaferGetPolicyInformation(dwScopeId: DWORD; SaferPolicyInfoClass: SAFER_POLICY_INFO_CLASS;
  457.   InfoBufferSize: DWORD; InfoBuffer: PVOID; var InfoBufferRetSize: DWORD; lpReserved: LPVOID): BOOL; stdcall;
  458. {$EXTERNALSYM SaferGetPolicyInformation}
  459. function SaferSetPolicyInformation(dwScopeId: DWORD; SaferPolicyInfoClass: SAFER_POLICY_INFO_CLASS;
  460.   InfoBufferSize: DWORD; InfoBuffer: PVOID; lpReserved: LPVOID): BOOL; stdcall;
  461. {$EXTERNALSYM SaferSetPolicyInformation}
  462. //
  463. // Functions to open or close a handle to a Safer Level.
  464. //
  465. function SaferCreateLevel(dwScopeId, dwLevelId, OpenFlags: DWORD; pLevelHandle: PSAFER_LEVEL_HANDLE; lpReserved: LPVOID): BOOL; stdcall;
  466. {$EXTERNALSYM SaferCreateLevel}
  467. function SaferCloseLevel(hLevelHandle: SAFER_LEVEL_HANDLE): BOOL; stdcall;
  468. {$EXTERNALSYM SaferCloseLevel}
  469. function SaferIdentifyLevel(dwNumProperties: DWORD; pCodeProperties: PSAFER_CODE_PROPERTIES;
  470.   var pLevelHandle: SAFER_LEVEL_HANDLE; lpReserved: LPVOID): BOOL; stdcall;
  471. {$EXTERNALSYM SaferIdentifyLevel}
  472. function SaferComputeTokenFromLevel(LevelHandle: SAFER_LEVEL_HANDLE; InAccessToken: HANDLE; OutAccessToken: PHANDLE;
  473.   dwFlags: DWORD; lpReserved: LPVOID): BOOL; stdcall;
  474. {$EXTERNALSYM SaferComputeTokenFromLevel}
  475. function SaferGetLevelInformation(LevelHandle: SAFER_LEVEL_HANDLE; dwInfoType: SAFER_OBJECT_INFO_CLASS;
  476.   lpQueryBuffer: LPVOID; dwInBufferSize: DWORD; var lpdwOutBufferSize: DWORD): BOOL; stdcall;
  477. {$EXTERNALSYM SaferGetLevelInformation}
  478. function SaferSetLevelInformation(LevelHandle: SAFER_LEVEL_HANDLE; dwInfoType: SAFER_OBJECT_INFO_CLASS;
  479.   lpQueryBuffer: LPVOID; dwInBufferSize: DWORD): BOOL; stdcall;
  480. {$EXTERNALSYM SaferSetLevelInformation}
  481. //
  482. // This function performs logging of messages to the Application
  483. // event log.  This is called by the hooks within CreateProcess,
  484. // ShellExecute and cmd when a lower trust evaluation result occurs.
  485. //
  486. function SaferRecordEventLogEntry(hLevel: SAFER_LEVEL_HANDLE; szTargetPath: LPCWSTR; lpReserved: LPVOID): BOOL; stdcall;
  487. {$EXTERNALSYM SaferRecordEventLogEntry}
  488. implementation
  489. const
  490.   advapi32 = 'advapi32.dll';
  491. {$IFDEF DYNAMIC_LINK}
  492. var
  493.   _SaferGetPolicyInformation: Pointer;
  494. function SaferGetPolicyInformation;
  495. begin
  496.   GetProcedureAddress(_SaferGetPolicyInformation, advapi32, 'SaferGetPolicyInformation');
  497.   asm
  498.     mov esp, ebp
  499.     pop ebp
  500.     jmp [_SaferGetPolicyInformation]
  501.   end;
  502. end;
  503. {$ELSE}
  504. function SaferGetPolicyInformation; external advapi32 name 'SaferGetPolicyInformation';
  505. {$ENDIF DYNAMIC_LINK}
  506. {$IFDEF DYNAMIC_LINK}
  507. var
  508.   _SaferSetPolicyInformation: Pointer;
  509. function SaferSetPolicyInformation;
  510. begin
  511.   GetProcedureAddress(_SaferSetPolicyInformation, advapi32, 'SaferSetPolicyInformation');
  512.   asm
  513.     mov esp, ebp
  514.     pop ebp
  515.     jmp [_SaferSetPolicyInformation]
  516.   end;
  517. end;
  518. {$ELSE}
  519. function SaferSetPolicyInformation; external advapi32 name 'SaferSetPolicyInformation';
  520. {$ENDIF DYNAMIC_LINK}
  521. {$IFDEF DYNAMIC_LINK}
  522. var
  523.   _SaferCreateLevel: Pointer;
  524. function SaferCreateLevel;
  525. begin
  526.   GetProcedureAddress(_SaferCreateLevel, advapi32, 'SaferCreateLevel');
  527.   asm
  528.     mov esp, ebp
  529.     pop ebp
  530.     jmp [_SaferCreateLevel]
  531.   end;
  532. end;
  533. {$ELSE}
  534. function SaferCreateLevel; external advapi32 name 'SaferCreateLevel';
  535. {$ENDIF DYNAMIC_LINK}
  536. {$IFDEF DYNAMIC_LINK}
  537. var
  538.   _SaferCloseLevel: Pointer;
  539. function SaferCloseLevel;
  540. begin
  541.   GetProcedureAddress(_SaferCloseLevel, advapi32, 'SaferCloseLevel');
  542.   asm
  543.     mov esp, ebp
  544.     pop ebp
  545.     jmp [_SaferCloseLevel]
  546.   end;
  547. end;
  548. {$ELSE}
  549. function SaferCloseLevel; external advapi32 name 'SaferCloseLevel';
  550. {$ENDIF DYNAMIC_LINK}
  551. {$IFDEF DYNAMIC_LINK}
  552. var
  553.   _SaferIdentifyLevel: Pointer;
  554. function SaferIdentifyLevel;
  555. begin
  556.   GetProcedureAddress(_SaferIdentifyLevel, advapi32, 'SaferIdentifyLevel');
  557.   asm
  558.     mov esp, ebp
  559.     pop ebp
  560.     jmp [_SaferIdentifyLevel]
  561.   end;
  562. end;
  563. {$ELSE}
  564. function SaferIdentifyLevel; external advapi32 name 'SaferIdentifyLevel';
  565. {$ENDIF DYNAMIC_LINK}
  566. {$IFDEF DYNAMIC_LINK}
  567. var
  568.   _SaferComputeTokenFromLevel: Pointer;
  569. function SaferComputeTokenFromLevel;
  570. begin
  571.   GetProcedureAddress(_SaferComputeTokenFromLevel, advapi32, 'SaferComputeTokenFromLevel');
  572.   asm
  573.     mov esp, ebp
  574.     pop ebp
  575.     jmp [_SaferComputeTokenFromLevel]
  576.   end;
  577. end;
  578. {$ELSE}
  579. function SaferComputeTokenFromLevel; external advapi32 name 'SaferComputeTokenFromLevel';
  580. {$ENDIF DYNAMIC_LINK}
  581. {$IFDEF DYNAMIC_LINK}
  582. var
  583.   _SaferGetLevelInformation: Pointer;
  584. function SaferGetLevelInformation;
  585. begin
  586.   GetProcedureAddress(_SaferGetLevelInformation, advapi32, 'SaferGetLevelInformation');
  587.   asm
  588.     mov esp, ebp
  589.     pop ebp
  590.     jmp [_SaferGetLevelInformation]
  591.   end;
  592. end;
  593. {$ELSE}
  594. function SaferGetLevelInformation; external advapi32 name 'SaferGetLevelInformation';
  595. {$ENDIF DYNAMIC_LINK}
  596. {$IFDEF DYNAMIC_LINK}
  597. var
  598.   _SaferSetLevelInformation: Pointer;
  599. function SaferSetLevelInformation;
  600. begin
  601.   GetProcedureAddress(_SaferSetLevelInformation, advapi32, 'SaferSetLevelInformation');
  602.   asm
  603.     mov esp, ebp
  604.     pop ebp
  605.     jmp [_SaferSetLevelInformation]
  606.   end;
  607. end;
  608. {$ELSE}
  609. function SaferSetLevelInformation; external advapi32 name 'SaferSetLevelInformation';
  610. {$ENDIF DYNAMIC_LINK}
  611. {$IFDEF DYNAMIC_LINK}
  612. var
  613.   _SaferRecordEventLogEntry: Pointer;
  614. function SaferRecordEventLogEntry;
  615. begin
  616.   GetProcedureAddress(_SaferRecordEventLogEntry, advapi32, 'SaferRecordEventLogEntry');
  617.   asm
  618.     mov esp, ebp
  619.     pop ebp
  620.     jmp [_SaferRecordEventLogEntry]
  621.   end;
  622. end;
  623. {$ELSE}
  624. function SaferRecordEventLogEntry; external advapi32 name 'SaferRecordEventLogEntry';
  625. {$ENDIF DYNAMIC_LINK}
  626. end.