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.

163 lines
5.0 KiB

  1. #include "stdafx.h"
  2. static void GetDNInfo( PCCERT_CONTEXT pCC ,
  3. LPCSTR field ,
  4. LPCWSTR fieldName ,
  5. MPC::wstring& szBuf ,
  6. MPC::wstring* szPlainName = NULL)
  7. {
  8. WCHAR szTmp[MAX_NAME];
  9. if(szBuf.length()) szBuf += L",";
  10. szBuf += fieldName;
  11. ::CertGetNameStringW( pCC,
  12. CERT_NAME_ATTR_TYPE,
  13. 0,
  14. (void*)field,
  15. szTmp,
  16. MAXSTRLEN(szTmp) );
  17. if(szPlainName) *szPlainName = szTmp;
  18. szBuf += szTmp;
  19. }
  20. HRESULT GetInfoFromCert(LPWSTR wszCABName, MPC::wstring &wszDN, MPC::wstring &wszOwner)
  21. {
  22. __HCP_FUNC_ENTRY( "GetInfoFromCert" );
  23. HRESULT hr;
  24. DWORD dwEncoding;
  25. DWORD dwContentType;
  26. DWORD dwFormatType;
  27. HCERTSTORE hCertStore = NULL;
  28. PCCERT_CONTEXT pCC = NULL;
  29. DWORD dwNameBytes = MAX_NAME;
  30. // Start querying the cert object
  31. if(!::CryptQueryObject( CERT_QUERY_OBJECT_FILE, // dwObjectType
  32. wszCABName, // pvObject
  33. CERT_QUERY_CONTENT_FLAG_PKCS7_SIGNED_EMBED, // dwExpectedContentTypeFlags
  34. CERT_QUERY_FORMAT_FLAG_ALL, // dwExpectedFormatTypeFlags
  35. 0, // dwFlags
  36. &dwEncoding, // pdwMsgAndCertEncodingType
  37. &dwContentType, // pdwContentType
  38. &dwFormatType, // pdwFormatType
  39. &hCertStore, // phCertStore
  40. NULL, // phMsg
  41. NULL)) // ppvContext
  42. {
  43. printf("Error - unable to perform CryptQueryObject.\n"); __MPC_FUNC_LEAVE;
  44. }
  45. // get the first cert
  46. pCC = ::CertEnumCertificatesInStore( hCertStore, NULL );
  47. if(!pCC)
  48. {
  49. printf("Error - unable to perform CertEnumCertificatesInStore.\n"); __MPC_FUNC_LEAVE;
  50. }
  51. {
  52. wszDN.erase();
  53. GetDNInfo( pCC, szOID_COMMON_NAME , L"CN=", wszDN, &wszOwner );
  54. GetDNInfo( pCC, szOID_LOCALITY_NAME , L"L=" , wszDN );
  55. GetDNInfo( pCC, szOID_STATE_OR_PROVINCE_NAME, L"S=" , wszDN );
  56. GetDNInfo( pCC, szOID_COUNTRY_NAME , L"C=" , wszDN );
  57. }
  58. if(wszDN.size() == 0 || wszOwner.size() == 0)
  59. {
  60. printf("Error - unable to obtain DN or name in certificate.\n"); __MPC_FUNC_LEAVE;
  61. }
  62. //
  63. // Escape unsafe character in the CertID.
  64. //
  65. {
  66. LPWSTR szCertID = (LPWSTR)wszDN.c_str();
  67. while(szCertID[0])
  68. {
  69. switch(szCertID[0])
  70. {
  71. case L'\\':
  72. case L'/':
  73. case L':':
  74. case L'*':
  75. case L'?':
  76. case L'"':
  77. case L'<':
  78. case L'>':
  79. case L'|':
  80. szCertID[0] = L'_';
  81. }
  82. szCertID++;
  83. }
  84. }
  85. hr = S_OK;
  86. __HCP_FUNC_CLEANUP;
  87. if(pCC ) ::CertFreeCertificateContext( pCC );
  88. if(hCertStore) ::CertCloseStore ( hCertStore, 0 );
  89. __HCP_FUNC_EXIT(hr);
  90. }
  91. ////////////////////////////////////////////////////////////////////////////////
  92. int __cdecl wmain( int argc, WCHAR **argv, WCHAR **envp)
  93. {
  94. HRESULT hr;
  95. MPC::wstring wszDN;
  96. MPC::wstring wszOwnerName;
  97. FILE *fPD;
  98. FILE *fLT;
  99. USES_CONVERSION;
  100. if(argc != 3)
  101. {
  102. printf("\nUsage : regoem <cab filename> <regcab filename>\n");
  103. return 0;
  104. }
  105. hr = GetInfoFromCert(argv[1], wszDN, wszOwnerName);
  106. //
  107. // Create package_description.xml file
  108. //
  109. fPD = fopen("package_description.xml", "w");
  110. if (!fPD)
  111. {
  112. printf("Unable to create package_description.xml\n");
  113. return 0;
  114. }
  115. fprintf(fPD, "<?xml version=\"1.0\" ?>\n<HELPCENTERPACKAGE>\n\t<VERSION VALUE=\"0.0.0.0\" />");
  116. fprintf(fPD, "\n\t<PRODUCT ID=\"%s\" />", W2A(wszOwnerName.c_str()));
  117. fprintf(fPD, "\n\t<SKU VALUE=\"ALL\"/>");
  118. fprintf(fPD, "\n\t<LANGUAGE VALUE=\"ALL\"/>");
  119. fprintf(fPD, "\n\t<NODEOWNERS>\n\t\t<OWNER DN=\"%s\"/>\n\t</NODEOWNERS>\n</HELPCENTERPACKAGE>", W2A(wszDN.c_str()));
  120. fclose(fPD);
  121. //
  122. // Create list.txt file
  123. //
  124. fLT = fopen("list.txt", "w");
  125. if (!fLT)
  126. {
  127. printf("Unable to create list.txt\n");
  128. return 0;
  129. }
  130. fprintf(fLT, "%s,OEM registration CAB for %s,http://www.microsoft.com\n", W2A(argv[2]), W2A(wszOwnerName.c_str()));
  131. fclose(fLT);
  132. return FAILED(hr) ? 10 : 0;
  133. }