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.

166 lines
3.0 KiB

  1. /*++
  2. Copyright (c) 1994-1998 Microsoft Corporation
  3. Module Name:
  4. tssec.c
  5. Abstract:
  6. Contains code that generates random keys.
  7. Author:
  8. Madan Appiah (madana) 1-Jan-1998
  9. Modified by Nadim Abdo 31-Aug-2001 to use system RNG
  10. Environment:
  11. User Mode - Win32
  12. Revision History:
  13. --*/
  14. #include <seccom.h>
  15. #include <stdlib.h>
  16. #ifdef OS_WINCE
  17. #include <rng.h>
  18. #endif
  19. #ifndef OS_WINCE
  20. #include <randlib.h>
  21. #endif
  22. VOID
  23. TSRNG_Initialize(
  24. )
  25. {
  26. #ifndef OS_WINCE
  27. InitializeRNG(NULL);
  28. #else
  29. TSInitializeRNG();
  30. #endif
  31. }
  32. VOID
  33. TSRNG_Shutdown(
  34. )
  35. {
  36. #ifndef OS_WINCE
  37. ShutdownRNG(NULL);
  38. #endif
  39. }
  40. //
  41. // function definitions
  42. //
  43. BOOL
  44. TSRNG_GenerateRandomBits(
  45. LPBYTE pbRandomBits,
  46. DWORD cbLen
  47. )
  48. /*++
  49. Routine Description:
  50. This function returns random bits
  51. Arguments:
  52. pbRandomBits - pointer to a buffer where a random key is returned.
  53. cbLen - length of the random key required.
  54. Return Value:
  55. TRUE - if a random key is generated successfully.
  56. FALSE - otherwise.
  57. --*/
  58. {
  59. #ifndef OS_WINCE
  60. BOOL fRet;
  61. fRet = NewGenRandom(NULL, NULL, pbRandomBits, cbLen);
  62. return fRet;
  63. #else
  64. GenerateRandomBits(pbRandomBits, cbLen);
  65. return( TRUE );
  66. #endif
  67. }
  68. BOOL
  69. TSCAPI_GenerateRandomBits(
  70. LPBYTE pbRandomBits,
  71. DWORD cbLen
  72. )
  73. /*++
  74. Routine Description:
  75. This function generates random number using CAPI in user mode
  76. Arguments:
  77. pbRandomBits - pointer to a buffer where a random key is returned.
  78. cbLen - length of the random key required.
  79. Return Value:
  80. TRUE - if a random number is generated successfully.
  81. FALSE - otherwise.
  82. --*/
  83. {
  84. HCRYPTPROV hProv;
  85. BOOL rc = FALSE;
  86. DWORD dwExtraFlags = CRYPT_VERIFYCONTEXT;
  87. DWORD dwError;
  88. // Get handle to the default provider.
  89. if(!CryptAcquireContext(&hProv, NULL, 0, PROV_RSA_FULL, dwExtraFlags)) {
  90. // Could not acquire a crypt context, get the reason of failure
  91. dwError = GetLastError();
  92. // If we get this error, it means the caller is impersonating a user (in Remote Assistance)
  93. // we revert back to the old way of generating random bits
  94. if (dwError == ERROR_FILE_NOT_FOUND) {
  95. rc = TSRNG_GenerateRandomBits(pbRandomBits, cbLen);
  96. goto done;
  97. }
  98. // Since default keyset should always exist, we can't hit this code path
  99. if (dwError == NTE_BAD_KEYSET) {
  100. //
  101. //create a new keyset
  102. //
  103. if(!CryptAcquireContext(&hProv, NULL, 0, PROV_RSA_FULL, dwExtraFlags | CRYPT_NEWKEYSET)) {
  104. //printf("Error %x during CryptAcquireContext!\n", GetLastError());
  105. goto done;
  106. }
  107. }
  108. else {
  109. goto done;
  110. }
  111. }
  112. if (CryptGenRandom(hProv, cbLen, pbRandomBits)) {
  113. rc = TRUE;
  114. }
  115. CryptReleaseContext(hProv, 0);
  116. done:
  117. return rc;
  118. }