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.

438 lines
12 KiB

  1. #include "windows.h"
  2. #include "stdio.h"
  3. #include "wchar.h"
  4. #include "wincrypt.h"
  5. #include "stddef.h"
  6. #ifndef NUMBER_OF
  7. #define NUMBER_OF(x) (sizeof(x)/sizeof(*(x)))
  8. #endif
  9. static const WCHAR wchMicrosoftLogo[] =
  10. L"Microsoft (R) Side-By-Side Public Key Token Extractor 1.1.3.0\n"
  11. L"Copyright (C) Microsoft Corporation 2000-2002. All Rights Reserved\n\n";
  12. #define STRONG_NAME_BYTE_LENGTH ( 8 )
  13. typedef struct _SXS_PUBLIC_KEY_INFO
  14. {
  15. unsigned int SigAlgID;
  16. unsigned int HashAlgID;
  17. ULONG KeyLength;
  18. BYTE pbKeyInfo[1];
  19. } SXS_PUBLIC_KEY_INFO, *PSXS_PUBLIC_KEY_INFO;
  20. #define BUFFER_SIZE ( 8192 )
  21. BOOL
  22. ParseArgs( WCHAR **argv, int argc, PCWSTR* ppcwszFilename, BOOL *fQuiet )
  23. {
  24. if ( fQuiet )
  25. *fQuiet = FALSE;
  26. if ( ppcwszFilename )
  27. *ppcwszFilename = NULL;
  28. if (argv == NULL)
  29. return FALSE;
  30. if ( !fQuiet || !ppcwszFilename )
  31. {
  32. return FALSE;
  33. }
  34. for ( int i = 1; i < argc; i++ )
  35. {
  36. if (argv[i] == NULL)
  37. {
  38. ::fwprintf(stderr, L"Bad parameter in argument list\n");
  39. return FALSE;
  40. }
  41. if ( ( argv[i][0] == L'-' ) || ( argv[i][0] == L'/' ) )
  42. {
  43. PCWSTR pval = argv[i] + 1;
  44. if (::_wcsicmp(pval, L"nologo") == 0)
  45. {
  46. }
  47. else if (::_wcsicmp(pval, L"quiet") == 0)
  48. {
  49. if ( fQuiet ) *fQuiet = TRUE;
  50. }
  51. else if (::_wcsicmp(pval, L"?") == 0 )
  52. {
  53. return FALSE;
  54. }
  55. else
  56. {
  57. ::fwprintf(stderr, L"Unrecognized parameter %ls\n", argv[i]);
  58. return FALSE;
  59. }
  60. }
  61. else
  62. {
  63. if ( *ppcwszFilename == NULL )
  64. {
  65. *ppcwszFilename = argv[i];
  66. }
  67. else
  68. {
  69. ::fwprintf(stderr, L"Only one filename parameter at a time.\n");
  70. return FALSE;
  71. }
  72. }
  73. }
  74. return TRUE;
  75. }
  76. void DispUsage( PCWSTR pcwszExeName )
  77. {
  78. const static WCHAR wchUsage[] =
  79. L"Extracts public key tokens from certificate files, in a format\n"
  80. L"usable in Side-By-Side assembly identities.\n"
  81. L"\n"
  82. L"Usage:\n"
  83. L"\n"
  84. L"%ls <filename.cer> [-quiet]\n";
  85. ::wprintf(wchUsage, pcwszExeName);
  86. }
  87. BOOL
  88. HashAndSwizzleKey(
  89. HCRYPTPROV hProvider,
  90. BYTE *pbPublicKeyBlob,
  91. SIZE_T cbPublicKeyBlob,
  92. BYTE *pbKeyToken,
  93. SIZE_T &cbKeyToken
  94. )
  95. {
  96. BOOL fResult = FALSE;
  97. HCRYPTHASH hHash = NULL;
  98. DWORD dwHashSize, dwHashSizeSize;
  99. ULONG top = STRONG_NAME_BYTE_LENGTH - 1;
  100. ULONG bottom = 0;
  101. if (cbKeyToken < STRONG_NAME_BYTE_LENGTH) {
  102. return FALSE;
  103. }
  104. if ( !::CryptCreateHash( hProvider, CALG_SHA1, NULL, 0, &hHash ) )
  105. {
  106. ::fwprintf(stderr, L"Unable to create cryptological hash object, error %ld\n", ::GetLastError());
  107. goto Exit;
  108. }
  109. if ( !::CryptHashData( hHash, pbPublicKeyBlob, static_cast<DWORD>(cbPublicKeyBlob), 0 ) )
  110. {
  111. ::fwprintf(stderr, L"Unable to hash public key information, error %ld\n", ::GetLastError());
  112. goto Exit;
  113. }
  114. if ( !::CryptGetHashParam( hHash, HP_HASHSIZE, (PBYTE)&dwHashSize, &(dwHashSizeSize = sizeof(dwHashSize)), 0))
  115. {
  116. ::fwprintf(stderr, L"Unable to determine size of hashed public key bits, error %ld\n", ::GetLastError());
  117. goto Exit;
  118. }
  119. if ( dwHashSize > cbKeyToken )
  120. {
  121. ::fwprintf(stderr, L"Hashed data is too large - space for %ld bytes, got %ld.\n",
  122. cbKeyToken, dwHashSize);
  123. goto Exit;
  124. }
  125. if ( !::CryptGetHashParam( hHash, HP_HASHVAL, pbKeyToken, &(dwHashSize = (DWORD)cbKeyToken), 0))
  126. {
  127. ::fwprintf(stderr, L"Unable to get hash of public key bits, error %ld\n", ::GetLastError());
  128. goto Exit;
  129. }
  130. cbKeyToken = dwHashSize;
  131. if (cbKeyToken < STRONG_NAME_BYTE_LENGTH)
  132. {
  133. ::fwprintf(stderr, L"Internal error - length of hash object (%d) is less than strong name length (%d)\n",
  134. cbKeyToken,
  135. STRONG_NAME_BYTE_LENGTH);
  136. goto Exit;
  137. }
  138. //
  139. // Now, move down the last eight bytes, then reverse them.
  140. //
  141. ::memmove(pbKeyToken,
  142. pbKeyToken + (cbKeyToken - STRONG_NAME_BYTE_LENGTH),
  143. STRONG_NAME_BYTE_LENGTH);
  144. while ( bottom < top )
  145. {
  146. const BYTE b = pbKeyToken[top];
  147. pbKeyToken[top] = pbKeyToken[bottom];
  148. pbKeyToken[bottom] = b;
  149. bottom++;
  150. top--;
  151. }
  152. //
  153. // The tokens are always this long.
  154. //
  155. cbKeyToken = STRONG_NAME_BYTE_LENGTH;
  156. fResult = TRUE;
  157. Exit:
  158. if ( hHash != NULL )
  159. {
  160. ::CryptDestroyHash(hHash);
  161. hHash = NULL;
  162. }
  163. return fResult;
  164. }
  165. BOOL
  166. GetTokenOfKey(
  167. PCERT_PUBLIC_KEY_INFO pKeyInfo,
  168. PBYTE prgbBuffer,
  169. SIZE_T &cbPublicKeyTokenLength
  170. )
  171. {
  172. PBYTE rgbWorkingSpace = NULL;
  173. DWORD dwRequiredSpace = 0;
  174. PSXS_PUBLIC_KEY_INFO pKeyBlobWorkspace = NULL;
  175. HCRYPTPROV hContext = NULL;
  176. HCRYPTKEY hCryptKey = NULL;
  177. BOOL fResult = FALSE;
  178. if ( !CryptAcquireContext(&hContext, NULL, NULL, PROV_RSA_FULL, CRYPT_SILENT | CRYPT_VERIFYCONTEXT))
  179. {
  180. ::fwprintf(stderr, L"Unable to aquire cryptological context, error %ld.\n", ::GetLastError());
  181. goto Exit;
  182. }
  183. ::ZeroMemory(prgbBuffer, cbPublicKeyTokenLength);
  184. //
  185. // Set up the public key info blob for hashing. Import the key to a real
  186. // HCRYPTKEY, then export the bits back out to a buffer. Set up the various
  187. // other settings in the blob as well, the type of key and the alg. used to
  188. // sign it.
  189. //
  190. if ( !::CryptImportPublicKeyInfoEx(
  191. hContext,
  192. X509_ASN_ENCODING | PKCS_7_ASN_ENCODING,
  193. pKeyInfo,
  194. CALG_RSA_SIGN,
  195. 0,
  196. NULL,
  197. &hCryptKey) )
  198. {
  199. ::fwprintf(stderr, L"Unable to import the public key from this certificate. Error %ld.\n", ::GetLastError());
  200. goto Exit;
  201. }
  202. if (!::CryptExportKey(hCryptKey, NULL, PUBLICKEYBLOB, 0, NULL, &dwRequiredSpace))
  203. {
  204. ::fwprintf(stderr, L"Unable to get required space for exporting public key data\n");
  205. goto Exit;
  206. }
  207. dwRequiredSpace += sizeof(SXS_PUBLIC_KEY_INFO);
  208. rgbWorkingSpace = (PBYTE)HeapAlloc(GetProcessHeap(), 0, dwRequiredSpace);
  209. if (rgbWorkingSpace == NULL)
  210. {
  211. ::fwprintf(stderr, L"Not enough memory to export public key data\n");
  212. goto Exit;
  213. }
  214. pKeyBlobWorkspace = reinterpret_cast<PSXS_PUBLIC_KEY_INFO>(rgbWorkingSpace);
  215. pKeyBlobWorkspace->KeyLength = dwRequiredSpace - offsetof(SXS_PUBLIC_KEY_INFO, pbKeyInfo);
  216. if ( !::CryptExportKey(
  217. hCryptKey,
  218. NULL,
  219. PUBLICKEYBLOB,
  220. 0,
  221. pKeyBlobWorkspace->pbKeyInfo,
  222. &pKeyBlobWorkspace->KeyLength) )
  223. {
  224. ::fwprintf(stderr, L"Unable to extract public key bits from this certificate. Error %ld.\n", ::GetLastError());
  225. goto Exit;
  226. }
  227. pKeyBlobWorkspace->SigAlgID = CALG_RSA_SIGN;
  228. pKeyBlobWorkspace->HashAlgID = CALG_SHA1;
  229. //
  230. // We now need to hash the public key bytes with SHA1.
  231. //
  232. dwRequiredSpace = pKeyBlobWorkspace->KeyLength + offsetof(SXS_PUBLIC_KEY_INFO, pbKeyInfo);
  233. if (!::HashAndSwizzleKey(
  234. hContext,
  235. (PBYTE)pKeyBlobWorkspace,
  236. dwRequiredSpace,
  237. prgbBuffer,
  238. cbPublicKeyTokenLength))
  239. {
  240. goto Exit;
  241. }
  242. fResult = TRUE;
  243. Exit:
  244. if ( hCryptKey != NULL )
  245. {
  246. ::CryptDestroyKey(hCryptKey);
  247. hCryptKey = NULL;
  248. }
  249. if (rgbWorkingSpace != NULL)
  250. {
  251. ::HeapFree(GetProcessHeap(), 0, rgbWorkingSpace);
  252. rgbWorkingSpace = NULL;
  253. }
  254. if ( hContext != NULL )
  255. {
  256. ::CryptReleaseContext(hContext, 0);
  257. hContext = NULL;
  258. }
  259. return fResult;
  260. }
  261. int __cdecl wmain( int argc, WCHAR *argv[] )
  262. {
  263. HCERTSTORE hCertStore = NULL;
  264. PCCERT_CONTEXT pCertContext = NULL;
  265. BOOL fNoLogoDisplay = FALSE;
  266. BOOL fQuiet = FALSE;
  267. DWORD STRONG_NAME_LENGTH = 8;
  268. PCWSTR pcwszFilename = NULL;
  269. DWORD dwRetVal = ERROR_SUCCESS;
  270. //
  271. // Quick check - are we to display the logo?
  272. for ( int j = 0; j < argc; j++ )
  273. {
  274. if (::_wcsicmp(argv[j], L"-nologo") == 0)
  275. fNoLogoDisplay = TRUE;
  276. }
  277. if ( !fNoLogoDisplay )
  278. {
  279. ::fputws(wchMicrosoftLogo, stdout);
  280. }
  281. //
  282. // Now go look for the arguments.
  283. //
  284. if ((argc < 2) || !ParseArgs( argv, argc, &pcwszFilename, &fQuiet ))
  285. {
  286. ::DispUsage( argv[0] );
  287. dwRetVal = ERROR_INVALID_PARAMETER;
  288. goto Exit;
  289. }
  290. else if ( !pcwszFilename )
  291. {
  292. ::DispUsage( argv[0] );
  293. dwRetVal = ERROR_INVALID_PARAMETER;
  294. goto Exit;
  295. }
  296. hCertStore = ::CertOpenStore(
  297. CERT_STORE_PROV_FILENAME,
  298. X509_ASN_ENCODING | PKCS_7_ASN_ENCODING,
  299. NULL,
  300. CERT_STORE_OPEN_EXISTING_FLAG,
  301. (void*)pcwszFilename);
  302. if ( !hCertStore )
  303. {
  304. ::fwprintf(
  305. stderr,
  306. L"Unable to open the input file %ls, error %ld\n",
  307. pcwszFilename,
  308. dwRetVal = ::GetLastError());
  309. goto Exit;
  310. }
  311. while ( pCertContext = ::CertEnumCertificatesInStore( hCertStore, pCertContext ) )
  312. {
  313. if ( !pCertContext->pCertInfo )
  314. {
  315. ::fwprintf( stderr, L"Oddity with file %ls - Certificate information not decodable\n", pcwszFilename );
  316. continue;
  317. }
  318. // NTRAID#NTBUG9 - 536275 - jonwis - 2002/04/25 - Stack buffers are bad, replace with heap allocated blobs
  319. WCHAR wsNiceName[BUFFER_SIZE] = { L'\0' };
  320. BYTE bBuffer[BUFFER_SIZE];
  321. SIZE_T cbBuffer = BUFFER_SIZE;
  322. DWORD dwKeyLength = 0;
  323. PCERT_PUBLIC_KEY_INFO pKeyInfo = &(pCertContext->pCertInfo->SubjectPublicKeyInfo);
  324. DWORD dwDump = 0;
  325. dwDump = ::CertGetNameStringW(
  326. pCertContext,
  327. CERT_NAME_FRIENDLY_DISPLAY_TYPE,
  328. CERT_NAME_ISSUER_FLAG,
  329. NULL,
  330. wsNiceName,
  331. BUFFER_SIZE
  332. );
  333. if ( dwDump == 0 )
  334. {
  335. ::fwprintf(stderr, L"Unable to get certificate name string! Error %ld.", GetLastError());
  336. ::wcsncpy(wsNiceName, L"(Unknown)", NUMBER_OF(wsNiceName));
  337. wsNiceName[NUMBER_OF(wsNiceName) - 1] = 0;
  338. }
  339. if ( !fQuiet )
  340. {
  341. dwKeyLength = CertGetPublicKeyLength( X509_ASN_ENCODING | PKCS_7_ASN_ENCODING, pKeyInfo );
  342. ::wprintf(L"\nCertificate: \"%ls\" - %ld bits long\n", wsNiceName, dwKeyLength);
  343. if ( dwKeyLength < 2048 )
  344. {
  345. ::wprintf(L"\tWarning! This key is too short to sign SxS assemblies with.\n\tSigning keys need to be 2048 bits or more.\n");
  346. }
  347. }
  348. if (!::GetTokenOfKey( pKeyInfo, bBuffer, cbBuffer ))
  349. {
  350. ::fwprintf(stderr, L"Unable to generate public key token for this certificate.\n");
  351. }
  352. else
  353. {
  354. if ( !fQuiet ) ::wprintf(L"\tpublicKeyToken=\"");
  355. for ( SIZE_T i = 0; i < cbBuffer; i++ )
  356. {
  357. ::wprintf(L"%02x", bBuffer[i] );
  358. }
  359. if ( !fQuiet )
  360. ::wprintf(L"\"\n");
  361. else
  362. ::wprintf(L"\n");
  363. }
  364. }
  365. Exit:
  366. if ( hCertStore != NULL )
  367. {
  368. ::CertCloseStore(hCertStore, CERT_CLOSE_STORE_FORCE_FLAG);
  369. hCertStore = NULL;
  370. }
  371. return dwRetVal;
  372. }