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.

401 lines
7.7 KiB

  1. /*++
  2. Copyright (c) 1995 Microsoft Corporation
  3. Module Name:
  4. srclist.cpp
  5. Abstract:
  6. Certificate source list object implementation.
  7. Author:
  8. Jeff Parham (jeffparh) 15-Dec-1995
  9. Revision History:
  10. --*/
  11. #include "stdafx.h"
  12. #include "srclist.h"
  13. #include <strsafe.h> //include last
  14. // key name under which the individual source key names may be found
  15. #define KEY_CERT_SOURCE_LIST "Software\\LSAPI\\Microsoft\\CertificateSources"
  16. // value name for the path to the certificate source DLL (REG_EXPAND_SZ)
  17. #define VALUE_CERT_SOURCE_PATH "ImagePath"
  18. // value name for the display name of the certificate source
  19. #define VALUE_CERT_DISPLAY_NAME "DisplayName"
  20. CCertSourceList::CCertSourceList()
  21. /*++
  22. Routine Description:
  23. Constructor for object.
  24. Arguments:
  25. None.
  26. Return Values:
  27. None.
  28. --*/
  29. {
  30. m_dwNumSources = 0;
  31. m_ppcsiSourceList = NULL;
  32. RefreshSources();
  33. }
  34. CCertSourceList::~CCertSourceList()
  35. /*++
  36. Routine Description:
  37. Destructor for dialog.
  38. Arguments:
  39. None.
  40. Return Values:
  41. None.
  42. --*/
  43. {
  44. RemoveSources();
  45. }
  46. BOOL CCertSourceList::RefreshSources()
  47. /*++
  48. Routine Description:
  49. Refresh source list from configuration stored in the registry.
  50. Arguments:
  51. None.
  52. Return Values:
  53. BOOL.
  54. --*/
  55. {
  56. LONG lError;
  57. LONG lEnumError;
  58. HKEY hKeyCertSourceList;
  59. int iSubKey;
  60. HKEY hKeyCertSource;
  61. DWORD cb;
  62. BOOL ok;
  63. PCERT_SOURCE_INFO pcsiSourceInfo;
  64. DWORD cch;
  65. TCHAR szExpImagePath[ _MAX_PATH ];
  66. HRESULT hr;
  67. RemoveSources();
  68. lError = RegOpenKeyEx( HKEY_LOCAL_MACHINE, TEXT( KEY_CERT_SOURCE_LIST ), 0, KEY_READ, &hKeyCertSourceList );
  69. if ( ERROR_SUCCESS == lError )
  70. {
  71. iSubKey = 0;
  72. do
  73. {
  74. ok = FALSE;
  75. pcsiSourceInfo = (PCERT_SOURCE_INFO) LocalAlloc( LPTR, sizeof( *pcsiSourceInfo ) );
  76. if ( NULL != pcsiSourceInfo )
  77. {
  78. // determine next certificate source
  79. cch = sizeof( pcsiSourceInfo->szName ) / sizeof( pcsiSourceInfo->szName[0] );
  80. lEnumError = RegEnumKeyEx( hKeyCertSourceList, iSubKey, pcsiSourceInfo->szName, &cch, NULL, NULL, NULL, NULL );
  81. iSubKey++;
  82. if ( ERROR_SUCCESS == lError )
  83. {
  84. // open certificate source's key
  85. lError = RegOpenKeyEx( hKeyCertSourceList, pcsiSourceInfo->szName, 0, KEY_READ, &hKeyCertSource );
  86. if ( ERROR_SUCCESS == lError )
  87. {
  88. // certificate source key opened; get its REG_EXPAND_SZ image path
  89. cb = sizeof( szExpImagePath );
  90. lError = RegQueryValueEx( hKeyCertSource, TEXT( VALUE_CERT_SOURCE_PATH ), NULL, NULL, (LPBYTE) szExpImagePath, &cb );
  91. if ( ERROR_SUCCESS == lError )
  92. {
  93. // translate environment variables in path
  94. cch = ExpandEnvironmentStrings( szExpImagePath, pcsiSourceInfo->szImagePath, sizeof( pcsiSourceInfo->szImagePath ) / sizeof( pcsiSourceInfo->szImagePath[0] ) );
  95. if ( ( 0 != cch ) && ( cch < sizeof( pcsiSourceInfo->szImagePath ) / sizeof( pcsiSourceInfo->szImagePath[0] ) ) )
  96. {
  97. // get display name
  98. cb = sizeof( pcsiSourceInfo->szDisplayName );
  99. lError = RegQueryValueEx( hKeyCertSource, TEXT( VALUE_CERT_DISPLAY_NAME ), NULL, NULL, (LPBYTE) pcsiSourceInfo->szDisplayName, &cb );
  100. if ( ERROR_SUCCESS != lError )
  101. {
  102. // default display name is the key name
  103. hr = StringCbCopy( pcsiSourceInfo->szDisplayName, sizeof(pcsiSourceInfo->szDisplayName), pcsiSourceInfo->szName );
  104. ASSERT(SUCCEEDED(hr));
  105. }
  106. // add the certificate source to our list
  107. AddSource( pcsiSourceInfo );
  108. ok = TRUE;
  109. }
  110. }
  111. RegCloseKey( hKeyCertSource );
  112. }
  113. }
  114. if ( !ok )
  115. {
  116. // an error occurred before saving our pointer; don't leak!
  117. LocalFree( pcsiSourceInfo );
  118. }
  119. }
  120. } while ( ( NULL != pcsiSourceInfo ) && ( ERROR_SUCCESS == lEnumError ) );
  121. RegCloseKey( hKeyCertSourceList );
  122. }
  123. // 'salright
  124. return TRUE;
  125. }
  126. BOOL CCertSourceList::RemoveSources()
  127. /*++
  128. Routine Description:
  129. Free internal certificate source list.
  130. Arguments:
  131. None.
  132. Return Values:
  133. BOOL.
  134. --*/
  135. {
  136. if ( NULL != m_ppcsiSourceList )
  137. {
  138. for ( DWORD i=0; i < m_dwNumSources; i++ )
  139. {
  140. LocalFree( m_ppcsiSourceList[i] );
  141. }
  142. LocalFree( m_ppcsiSourceList );
  143. }
  144. m_ppcsiSourceList = NULL;
  145. m_dwNumSources = 0;
  146. return TRUE;
  147. }
  148. LPCTSTR CCertSourceList::GetSourceName( int nIndex )
  149. /*++
  150. Routine Description:
  151. Get the name (e.g., "Paper") of the source at the given index.
  152. Arguments:
  153. nIndex (int)
  154. Return Values:
  155. LPCTSTR.
  156. --*/
  157. {
  158. LPTSTR pszName;
  159. if ( ( nIndex < 0 ) || ( nIndex >= (int) m_dwNumSources ) )
  160. {
  161. pszName = NULL;
  162. }
  163. else
  164. {
  165. pszName = m_ppcsiSourceList[ nIndex ]->szName;
  166. }
  167. return pszName;
  168. }
  169. LPCTSTR CCertSourceList::GetSourceDisplayName( int nIndex )
  170. /*++
  171. Routine Description:
  172. Get the display name (e.g., "Paper Certificate") of the source
  173. at the given index.
  174. Arguments:
  175. nIndex (int)
  176. Return Values:
  177. LPCTSTR.
  178. --*/
  179. {
  180. LPTSTR pszDisplayName;
  181. if ( ( nIndex < 0 ) || ( nIndex >= (int) m_dwNumSources ) )
  182. {
  183. pszDisplayName = NULL;
  184. }
  185. else
  186. {
  187. pszDisplayName = m_ppcsiSourceList[ nIndex ]->szDisplayName;
  188. }
  189. return pszDisplayName;
  190. }
  191. LPCTSTR CCertSourceList::GetSourceImagePath( int nIndex )
  192. /*++
  193. Routine Description:
  194. Get the image path name (e.g., "C:\WINNT35\SYSTEM32\CCFAPI32.DLL") of
  195. the source at the given index.
  196. Arguments:
  197. nIndex (int)
  198. Return Values:
  199. LPCTSTR.
  200. --*/
  201. {
  202. LPTSTR pszImagePath;
  203. if ( ( nIndex < 0 ) || ( nIndex >= (int) m_dwNumSources ) )
  204. {
  205. pszImagePath = NULL;
  206. }
  207. else
  208. {
  209. pszImagePath = m_ppcsiSourceList[ nIndex ]->szImagePath;
  210. }
  211. return pszImagePath;
  212. }
  213. int CCertSourceList::GetNumSources()
  214. /*++
  215. Routine Description:
  216. Get the number of certificate sources available.
  217. Arguments:
  218. None.
  219. Return Values:
  220. int.
  221. --*/
  222. {
  223. return m_dwNumSources;
  224. }
  225. BOOL CCertSourceList::AddSource( PCERT_SOURCE_INFO pcsiNewSource )
  226. /*++
  227. Routine Description:
  228. Add a source to the internal list.
  229. Arguments:
  230. pcsiNewSource (PCERT_SOURCE_INFO)
  231. Return Values:
  232. BOOL.
  233. --*/
  234. {
  235. PCERT_SOURCE_INFO *l_ppcsiSourceList;
  236. if ( 0 == m_dwNumSources )
  237. {
  238. l_ppcsiSourceList = (PCERT_SOURCE_INFO *) LocalAlloc( LMEM_FIXED, sizeof( pcsiNewSource ) );
  239. }
  240. else
  241. {
  242. l_ppcsiSourceList = (PCERT_SOURCE_INFO *) LocalReAlloc( m_ppcsiSourceList, ( 1 + m_dwNumSources ) * sizeof( pcsiNewSource ), 0 );
  243. }
  244. if ( NULL != l_ppcsiSourceList )
  245. {
  246. m_ppcsiSourceList = l_ppcsiSourceList;
  247. m_ppcsiSourceList[ m_dwNumSources ] = pcsiNewSource;
  248. m_dwNumSources++;
  249. }
  250. else
  251. {
  252. // if any allocation failure, free all sources and reset
  253. RemoveSources();
  254. }
  255. return ( NULL != m_ppcsiSourceList );
  256. }