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.

125 lines
3.2 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (C) Microsoft Corporation, 2002
  6. //
  7. // File: sfscript.cpp
  8. //
  9. //--------------------------------------------------------------------------
  10. #include <windows.h>
  11. #include <wincrypt.h>
  12. #include <dbgdef.h>
  13. #include "unicode.h"
  14. #include "resource.h"
  15. #include "sfscript.h"
  16. // handle to xenroll initalized in DllMain
  17. extern HINSTANCE hInstanceXEnroll;
  18. // implemented in cenroll.cpp
  19. HRESULT xeLoadRCString(IN HINSTANCE hInstance,
  20. IN int iRCId,
  21. OUT WCHAR **ppwsz);
  22. BOOL VerifyProviderFlagsSafeForScripting(DWORD dwFlags) {
  23. DWORD dwSafeFlags = CRYPT_MACHINE_KEYSET;
  24. // Return FALSE if the flag contains an unsafe flag.
  25. return 0 == (dwFlags & ~dwSafeFlags);
  26. }
  27. BOOL VerifyStoreFlagsSafeForScripting(DWORD dwFlags) {
  28. DWORD dwSafeFlags = CERT_SYSTEM_STORE_LOCAL_MACHINE | CERT_SYSTEM_STORE_CURRENT_USER;
  29. // Return FALSE if the flag contains an unsafe flag.
  30. return 0 == (dwFlags & ~dwSafeFlags);
  31. }
  32. BOOL VerifyStoreSafeForScripting(HCERTSTORE hStore) {
  33. DWORD fRet = FALSE;
  34. PCCERT_CONTEXT pCertContext = NULL;
  35. DWORD dwCertCnt = 0;
  36. WCHAR *pwszSafety = NULL;
  37. WCHAR *pwszMsg = NULL;
  38. HRESULT hr;
  39. // count how many requests in the store
  40. while(NULL != (pCertContext = CertEnumCertificatesInStore(
  41. hStore,
  42. pCertContext)))
  43. dwCertCnt++;
  44. if(dwCertCnt >= MAX_SAFE_FOR_SCRIPTING_REQUEST_STORE_COUNT)
  45. {
  46. hr = xeLoadRCString(hInstanceXEnroll, IDS_NOTSAFEACTION, &pwszSafety);
  47. if (S_OK != hr)
  48. {
  49. goto xeLoadRCStringError;
  50. }
  51. hr = xeLoadRCString(hInstanceXEnroll, IDS_REQ_STORE_FULL, &pwszMsg);
  52. if (S_OK != hr)
  53. {
  54. goto xeLoadRCStringError;
  55. }
  56. switch(MessageBoxU(NULL, pwszMsg, pwszSafety, MB_YESNO | MB_ICONWARNING)) {
  57. case IDYES:
  58. break;
  59. case IDNO:
  60. default:
  61. SetLastError(ERROR_CANCELLED);
  62. goto ErrorCancelled;
  63. break;
  64. }
  65. }
  66. fRet = TRUE;
  67. ErrorReturn:
  68. if (NULL != pwszMsg)
  69. {
  70. LocalFree(pwszMsg);
  71. }
  72. if (NULL != pwszSafety)
  73. {
  74. LocalFree(pwszSafety);
  75. }
  76. return(fRet);
  77. TRACE_ERROR(ErrorCancelled);
  78. TRACE_ERROR(xeLoadRCStringError);
  79. }
  80. BOOL WINAPI MySafeCertAddCertificateContextToStore(HCERTSTORE hCertStore,
  81. PCCERT_CONTEXT pCertContext,
  82. DWORD dwAddDisposition,
  83. PCCERT_CONTEXT *ppStoreContext,
  84. DWORD dwSafetyOptions)
  85. {
  86. BOOL fResult;
  87. if (0 != dwSafetyOptions) {
  88. fResult = VerifyStoreSafeForScripting(hCertStore);
  89. if (!fResult)
  90. goto AccessDeniedError;
  91. }
  92. fResult = CertAddCertificateContextToStore(hCertStore, pCertContext, dwAddDisposition, ppStoreContext);
  93. if (!fResult)
  94. goto CertAddCertificateContextToStoreError;
  95. fResult = TRUE;
  96. ErrorReturn:
  97. return fResult;
  98. SET_ERROR(AccessDeniedError, ERROR_ACCESS_DENIED);
  99. TRACE_ERROR(CertAddCertificateContextToStoreError);
  100. }