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.

238 lines
5.6 KiB

  1. #include "utils.h"
  2. #include <wincrypt.h>
  3. //***************************************************************************
  4. //*
  5. //* purpose:
  6. //*
  7. //***************************************************************************
  8. LPSTR StripWhitespace( LPSTR pszString )
  9. {
  10. LPSTR pszTemp = NULL;
  11. if ( pszString == NULL )
  12. {
  13. return NULL;
  14. }
  15. while ( *pszString == ' ' || *pszString == '\t' )
  16. {
  17. pszString += 1;
  18. }
  19. // Catch case where string consists entirely of whitespace or empty string.
  20. if ( *pszString == '\0' )
  21. {
  22. return pszString;
  23. }
  24. pszTemp = pszString;
  25. pszString += lstrlenA(pszString) - 1;
  26. while ( *pszString == ' ' || *pszString == '\t' )
  27. {
  28. *pszString = '\0';
  29. pszString -= 1;
  30. }
  31. return pszTemp;
  32. }
  33. //***************************************************************************
  34. //*
  35. //* purpose: return back a Alocated wide string from a ansi string
  36. //* caller must free the returned back pointer with GlobalFree()
  37. //*
  38. //***************************************************************************
  39. LPWSTR MakeWideStrFromAnsi(UINT uiCodePage, LPSTR psz)
  40. {
  41. LPWSTR pwsz;
  42. int i;
  43. // make sure they gave us something
  44. if (!psz)
  45. {
  46. return NULL;
  47. }
  48. // compute the length
  49. i = MultiByteToWideChar(uiCodePage, 0, psz, -1, NULL, 0);
  50. if (i <= 0) return NULL;
  51. // allocate memory in that length
  52. pwsz = (LPWSTR) GlobalAlloc(GPTR,i * sizeof(WCHAR));
  53. if (!pwsz) return NULL;
  54. // clear out memory
  55. memset(pwsz, 0, wcslen(pwsz) * sizeof(WCHAR));
  56. // convert the ansi string into unicode
  57. i = MultiByteToWideChar(uiCodePage, 0, (LPSTR) psz, -1, pwsz, i);
  58. if (i <= 0)
  59. {
  60. GlobalFree(pwsz);
  61. pwsz = NULL;
  62. return NULL;
  63. }
  64. // make sure ends with null
  65. pwsz[i - 1] = 0;
  66. // return the pointer
  67. return pwsz;
  68. }
  69. BOOL IsFileExist(LPCTSTR szFile)
  70. {
  71. // Check if the file has expandable Environment strings
  72. LPTSTR pch = NULL;
  73. pch = _tcschr( (LPTSTR) szFile, _T('%'));
  74. if (pch)
  75. {
  76. TCHAR szValue[_MAX_PATH];
  77. _tcscpy(szValue,szFile);
  78. if (!ExpandEnvironmentStrings( (LPCTSTR)szFile, szValue, sizeof(szValue)/sizeof(TCHAR)))
  79. {_tcscpy(szValue,szFile);}
  80. return (GetFileAttributes(szValue) != 0xFFFFFFFF);
  81. }
  82. else
  83. {
  84. return (GetFileAttributes(szFile) != 0xFFFFFFFF);
  85. }
  86. }
  87. void AddPath(LPTSTR szPath, LPCTSTR szName )
  88. {
  89. LPTSTR p = szPath;
  90. // Find end of the string
  91. while (*p){p = _tcsinc(p);}
  92. // If no trailing backslash then add one
  93. if (*(_tcsdec(szPath, p)) != _T('\\'))
  94. {_tcscat(szPath, _T("\\"));}
  95. // if there are spaces precluding szName, then skip
  96. while ( *szName == ' ' ) szName = _tcsinc(szName);;
  97. // Add new name to existing path string
  98. _tcscat(szPath, szName);
  99. }
  100. void DoExpandEnvironmentStrings(LPTSTR szFile)
  101. {
  102. TCHAR szValue[_MAX_PATH];
  103. _tcscpy(szValue,szFile);
  104. // Check if the file has expandable Environment strings
  105. LPTSTR pch = NULL;
  106. pch = _tcschr( (LPTSTR) szFile, _T('%'));
  107. if (pch)
  108. {
  109. if (!ExpandEnvironmentStrings( (LPCTSTR)szFile, szValue, sizeof(szValue)/sizeof(TCHAR)))
  110. {
  111. _tcscpy(szValue,szFile);
  112. }
  113. }
  114. _tcscpy(szFile,szValue);
  115. return;
  116. }
  117. // return -1 for error
  118. // return 0 for not exportable
  119. // reutrn 1 for exportable
  120. #define PRIVATE_KEY_ERROR -1
  121. #define PRIVATE_KEY_NOT_EXPORTABLE 0
  122. #define PRIVATE_KEY_EXPORTABLE 1
  123. DWORD CheckPrivateKeyStatus(PCCERT_CONTEXT pCertContextRequest)
  124. {
  125. HCRYPTPROV hCryptProv = NULL;
  126. DWORD dwKeySpec = 0;
  127. BOOL fCallerFreeProv = FALSE;
  128. BOOL dwRet = PRIVATE_KEY_ERROR;
  129. HCRYPTKEY hKey = NULL;
  130. DWORD dwPermissions = 0;
  131. DWORD dwSize = 0;
  132. //
  133. // first get the private key context
  134. //
  135. if (!CryptAcquireCertificatePrivateKey(
  136. pCertContextRequest,
  137. CRYPT_ACQUIRE_USE_PROV_INFO_FLAG | CRYPT_ACQUIRE_COMPARE_KEY_FLAG,
  138. NULL,
  139. &hCryptProv,
  140. &dwKeySpec,
  141. &fCallerFreeProv))
  142. {
  143. DWORD dw = GetLastError();
  144. dwRet = PRIVATE_KEY_ERROR;
  145. goto ErrorReturn;
  146. }
  147. //
  148. // get the handle to the key
  149. //
  150. if (!CryptGetUserKey(hCryptProv, dwKeySpec, &hKey))
  151. {
  152. dwRet = PRIVATE_KEY_ERROR;
  153. goto ErrorReturn;
  154. }
  155. //
  156. // finally, get the permissions on the key and check if it is exportable
  157. //
  158. dwSize = sizeof(dwPermissions);
  159. if (!CryptGetKeyParam(hKey, KP_PERMISSIONS, (PBYTE)&dwPermissions, &dwSize, 0))
  160. {
  161. goto ErrorReturn;
  162. }
  163. dwRet = (dwPermissions & CRYPT_EXPORT) ? PRIVATE_KEY_EXPORTABLE : PRIVATE_KEY_NOT_EXPORTABLE;
  164. CleanUp:
  165. if (hKey != NULL)
  166. {
  167. CryptDestroyKey(hKey);
  168. }
  169. if (fCallerFreeProv)
  170. {
  171. CryptReleaseContext(hCryptProv, 0);
  172. }
  173. return dwRet;
  174. ErrorReturn:
  175. goto CleanUp;
  176. }
  177. HRESULT AttachFriendlyName(PCCERT_CONTEXT pContext)
  178. {
  179. CRYPT_DATA_BLOB blob_name;
  180. WCHAR szName[200];
  181. wcscpy(szName,L"TestingName\0\0");
  182. blob_name.pbData = (LPBYTE)(LPCWSTR) szName;
  183. blob_name.cbData = (wcslen(szName)+1) * sizeof(WCHAR);
  184. if (!CertSetCertificateContextProperty(pContext,CERT_FRIENDLY_NAME_PROP_ID, 0, &blob_name))
  185. {
  186. _tprintf(_T("AttachFriendlyName: FAILED\n"));
  187. return HRESULT_FROM_WIN32(GetLastError());
  188. }
  189. else
  190. {
  191. _tprintf(_T("AttachFriendlyName: SUCCEEDED!!!\n"));
  192. }
  193. return ERROR_SUCCESS;
  194. }