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.

168 lines
3.6 KiB

  1. /******************************************************************************
  2. Copyright (c) 2000 Microsoft Corporation
  3. Module Name:
  4. sign.cpp
  5. Abstract:
  6. Revision History:
  7. Vijay Baliga (VBaliga) 08/10/2000
  8. created
  9. ******************************************************************************/
  10. #include <module.h>
  11. #include <MPC_main.h>
  12. #include <MPC_utils.h>
  13. #include <MPC_streams.h>
  14. #include <KeysLib.h>
  15. #include <HCP_trace.h>
  16. #include <TrustedScripts.h>
  17. HRESULT WINAPI
  18. GetSignature(
  19. CComBSTR bstrPrivateKey,
  20. CComBSTR bstrCode
  21. )
  22. {
  23. __HCP_FUNC_ENTRY( "GetSignature" );
  24. HRESULT hr;
  25. CPCHCryptKeys key;
  26. CComBSTR bstrSignature;
  27. if (NULL != NULL)
  28. {
  29. __MPC_SET_ERROR_AND_EXIT(hr, E_INVALIDARG);
  30. }
  31. __MPC_EXIT_IF_METHOD_FAILS(hr, key.ImportPrivate(bstrPrivateKey));
  32. __MPC_EXIT_IF_METHOD_FAILS
  33. (
  34. hr,
  35. key.SignData
  36. (
  37. bstrSignature,
  38. (BYTE*) (BSTR(bstrCode)),
  39. SysStringByteLen(bstrCode)
  40. )
  41. );
  42. hr = S_OK;
  43. wprintf(L"%s", bstrSignature);
  44. __HCP_FUNC_CLEANUP;
  45. __HCP_FUNC_EXIT(hr);
  46. }
  47. static HRESULT LoadFile( /*[in ]*/ LPCWSTR szFile ,
  48. /*[out]*/ HGLOBAL& hg )
  49. {
  50. __HCP_FUNC_ENTRY( "LoadFile" );
  51. HRESULT hr;
  52. CComPtr<IStream> streamMem;
  53. CComPtr<MPC::FileStream> streamFile;
  54. //
  55. // Create a stream for a file.
  56. //
  57. __MPC_EXIT_IF_METHOD_FAILS(hr, MPC::CreateInstance( &streamFile ));
  58. __MPC_EXIT_IF_METHOD_FAILS(hr, streamFile->InitForRead( szFile ));
  59. //
  60. // Create a memory stream.
  61. //
  62. __MPC_EXIT_IF_METHOD_FAILS(hr, ::CreateStreamOnHGlobal( NULL, FALSE, &streamMem ));
  63. //
  64. // Load the contents in memory.
  65. //
  66. __MPC_EXIT_IF_METHOD_FAILS(hr, MPC::BaseStream::TransferData( streamFile, streamMem ));
  67. __MPC_EXIT_IF_METHOD_FAILS(hr, ::GetHGlobalFromStream( streamMem, &hg ));
  68. hr = S_OK;
  69. __HCP_FUNC_CLEANUP;
  70. __HCP_FUNC_EXIT(hr);
  71. }
  72. static HRESULT LoadFileAsString( /*[in ]*/ LPCWSTR szFile ,
  73. /*[out]*/ CComBSTR& bstrData )
  74. {
  75. __HCP_FUNC_ENTRY( "LoadFileAsString" );
  76. HRESULT hr;
  77. HGLOBAL hg = NULL;
  78. DWORD dwLen;
  79. LPWSTR str;
  80. __MPC_EXIT_IF_METHOD_FAILS(hr, LoadFile( szFile, hg ));
  81. dwLen = ::GlobalSize( hg );
  82. bstrData.Attach( ::SysAllocStringLen( NULL, dwLen ) );
  83. ::MultiByteToWideChar( CP_ACP, 0, (LPCSTR)::GlobalLock( hg ), dwLen, bstrData, (dwLen+1)*sizeof(WCHAR) ); bstrData[dwLen] = 0;
  84. hr = S_OK;
  85. __HCP_FUNC_CLEANUP;
  86. if(hg) ::GlobalFree( hg );
  87. __HCP_FUNC_EXIT(hr);
  88. }
  89. int __cdecl wmain(
  90. int argc,
  91. LPCWSTR argv[]
  92. )
  93. {
  94. HRESULT hr;
  95. MPC::wstring szFileKey;
  96. MPC::wstring szFileCode;
  97. CComBSTR bstrPrivateKey;
  98. CComBSTR bstrCode;
  99. if (argc != 3)
  100. {
  101. wprintf(L"Usage: %s <private key file> <code file>\n", argv[0]);
  102. exit(1);
  103. }
  104. if (FAILED(hr = ::CoInitializeEx(NULL, COINIT_MULTITHREADED)))
  105. {
  106. wprintf(L"No COM!!\n");
  107. exit(2);
  108. }
  109. MPC::SubstituteEnvVariables(szFileKey = argv[1]);
  110. MPC::SubstituteEnvVariables(szFileCode = argv[2]);
  111. LoadFileAsString(szFileKey.c_str(), bstrPrivateKey);
  112. LoadFileAsString(szFileCode.c_str(), bstrCode);
  113. if(FAILED(hr = GetSignature(bstrPrivateKey, bstrCode)))
  114. {
  115. wprintf(L"Failed to process %s: %08x\n", argv[1], hr);
  116. exit(3);
  117. }
  118. ::CoUninitialize();
  119. return 0;
  120. }