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.

217 lines
6.3 KiB

  1. // COnlineAuthority
  2. // copyright 1997 Microsoft Corp
  3. // created 5/19/97 by boydm
  4. #include "stdafx.h"
  5. #define INITGUID
  6. #include "keyring.h"
  7. #include "certcli.h"
  8. #include "OnlnAuth.h"
  9. DEFINE_GUID(IID_ICertGetConfig,
  10. 0xc7ea09c0, 0xce17, 0x11d0, 0x88, 0x33, 0x00, 0xa0, 0xc9, 0x03, 0xb8, 0x3c);
  11. //DEFINE_GUID(IID_ICertConfig,
  12. // 0x372fce34, 0x4324, 0x11d0, 0x88, 0x10, 0x00, 0xa0, 0xc9, 0x03, 0xb8, 0x3c);
  13. DEFINE_GUID(IID_ICertRequest,
  14. 0x014e4840, 0x5523, 0x11d0, 0x88, 0x12, 0x00, 0xa0, 0xc9, 0x03, 0xb8, 0x3c);
  15. // defines to get the class factory ids out of the registry
  16. #define SZREG_CERTGETCONFIG _T("CertGetConfig")
  17. #define SZREG_CERTREQUEST _T("CertRequest")
  18. //---------------------------------------------------------------------------
  19. COnlineAuthority::COnlineAuthority():
  20. pIConfig(NULL),
  21. pIRequest(NULL)
  22. {
  23. }
  24. //---------------------------------------------------------------------------
  25. COnlineAuthority::~COnlineAuthority()
  26. {
  27. // if we already have interfaces - release them
  28. if ( pIConfig )
  29. pIConfig->Release();
  30. if ( pIRequest )
  31. pIRequest->Release();
  32. }
  33. //---------------------------------------------------------------------------
  34. // initialize the class with an interface string
  35. BOOL COnlineAuthority::FInitSZ( CString szCA )
  36. {
  37. HRESULT hresError;
  38. IID iidConfig, iidRequest;
  39. OLECHAR* poch = NULL;
  40. CString szConfig, szRequest;
  41. CString szRegKeyName;
  42. DWORD dwType;
  43. DWORD cbBuff;
  44. DWORD err;
  45. HKEY hKey;
  46. // load the base registry key name
  47. szRegKeyName.LoadString( IDS_CA_LOCATION );
  48. // add the CA
  49. szRegKeyName += _T("\\");
  50. szRegKeyName += szCA;
  51. // open the registry key, if it exists
  52. err = RegOpenKeyEx(
  53. HKEY_LOCAL_MACHINE, // handle of open key
  54. szRegKeyName, // address of name of subkey to open
  55. 0, // reserved
  56. KEY_READ, // security access mask
  57. &hKey // address of handle of open key
  58. );
  59. // if we did not open the key for any reason (say... it doesn't exist)
  60. // then leave right away
  61. if ( err != ERROR_SUCCESS )
  62. return FALSE;
  63. // get the config value
  64. cbBuff = MAX_PATH;
  65. err = RegQueryValueEx(
  66. hKey, // handle of key to query
  67. SZREG_CERTGETCONFIG,// address of name of value to query
  68. NULL, // reserved
  69. &dwType, // address of buffer for value type
  70. (PUCHAR)szConfig.GetBuffer(cbBuff+1), // address of data buffer
  71. &cbBuff // address of data buffer size
  72. );
  73. szConfig.ReleaseBuffer();
  74. if ( err != ERROR_SUCCESS )
  75. {
  76. RegCloseKey( hKey );
  77. return FALSE;
  78. }
  79. // get the request value
  80. cbBuff = MAX_PATH;
  81. err = RegQueryValueEx(
  82. hKey, // handle of key to query
  83. SZREG_CERTREQUEST, // address of name of value to query
  84. NULL, // reserved
  85. &dwType, // address of buffer for value type
  86. (PUCHAR)szRequest.GetBuffer(cbBuff+1), // address of data buffer
  87. &cbBuff // address of data buffer size
  88. );
  89. szRequest.ReleaseBuffer();
  90. if ( err != ERROR_SUCCESS )
  91. {
  92. RegCloseKey( hKey );
  93. return FALSE;
  94. }
  95. // all done, close the key before leaving
  96. RegCloseKey( hKey );
  97. //======= first, convert the szGuid to a real binary GUID
  98. // allocate the name buffer
  99. poch = (OLECHAR*)GlobalAlloc( GPTR, MAX_PATH * 2 );
  100. // unicodize the name into the buffer
  101. if ( poch )
  102. MultiByteToWideChar( CP_ACP, MB_PRECOMPOSED, szConfig, -1,
  103. poch, MAX_PATH * 2 );
  104. // convert the string to an IID that we can use
  105. hresError = CLSIDFromString( poch, &iidConfig );
  106. // unicodize the name into the buffer
  107. if ( poch )
  108. MultiByteToWideChar( CP_ACP, MB_PRECOMPOSED, szRequest, -1,
  109. poch, MAX_PATH * 2 );
  110. // convert the string to an IID that we can use
  111. hresError = CLSIDFromString( poch, &iidRequest );
  112. // cleanup
  113. GlobalFree( poch );
  114. if ( FAILED(hresError) )
  115. {
  116. AfxMessageBox( IDS_CA_INVALID );
  117. return FALSE;
  118. }
  119. //======= if we already have interfaces - release them
  120. if ( pIConfig )
  121. pIConfig->Release();
  122. pIConfig = NULL;
  123. if ( pIRequest )
  124. pIRequest->Release();
  125. pIRequest = NULL;
  126. //======= start by obtaining the class factory pointer
  127. IClassFactory* pcsfFactory = NULL;
  128. COSERVERINFO csiMachineName;
  129. //fill the structure for CoGetClassObject
  130. ZeroMemory( &csiMachineName, sizeof(csiMachineName) );
  131. // csiMachineName.pAuthInfo = NULL;
  132. // csiMachineName.dwFlags = 0;
  133. // csiMachineName.pServerInfoExt = NULL;
  134. csiMachineName.pwszName = NULL;
  135. // get the class factory
  136. hresError = CoGetClassObject( iidConfig, CLSCTX_INPROC, NULL,
  137. IID_IClassFactory, (void**) &pcsfFactory);
  138. if (FAILED(hresError))
  139. return FALSE;
  140. //======= now we get the interfaces themselves
  141. hresError = pcsfFactory->CreateInstance(NULL, IID_ICertGetConfig, (void **)&pIConfig);
  142. if (FAILED(hresError))
  143. {
  144. pcsfFactory->Release();
  145. return FALSE;
  146. }
  147. // release the factory
  148. pcsfFactory->Release();
  149. // get the class factory
  150. hresError = CoGetClassObject( iidRequest, CLSCTX_INPROC, NULL,
  151. IID_IClassFactory, (void**) &pcsfFactory);
  152. if (FAILED(hresError))
  153. return FALSE;
  154. hresError = pcsfFactory->CreateInstance(NULL, IID_ICertRequest, (void **)&pIRequest);
  155. if (FAILED(hresError))
  156. {
  157. pIConfig->Release();
  158. pcsfFactory->Release();
  159. return FALSE;
  160. }
  161. // release the factory
  162. pcsfFactory->Release();
  163. // success
  164. return TRUE;
  165. }
  166. //---------------------------------------------------------------------------
  167. // stored property strings
  168. BOOL COnlineAuthority::FSetPropertyString( BSTR bstr )
  169. {
  170. return FALSE;
  171. }
  172. //---------------------------------------------------------------------------
  173. BOOL COnlineAuthority::FGetPropertyString( BSTR* pBstr )
  174. {
  175. HRESULT hErr;
  176. // make the call
  177. hErr = pIConfig->GetConfig( 0, pBstr);
  178. return SUCCEEDED(hErr);
  179. }