c++ Visual Studio:从可执行文件获取所有证书颁发者详细信息

mrphzbgm  于 2024-01-09  发布在  其他
关注(0)|答案(2)|浏览(162)

我希望能够从Windows资源管理器中获取所有发行商数据,如下所示:

我已经能够从https://learn.microsoft.com/en-us/windows/win32/api/wincrypt/nf-wincrypt-certgetnamestringw获得CN,但我不知道它是关于那个针对CN的调用的(如果我知道,我可能能够弄清楚如何获得其他所有内容)。我试过用谷歌搜索“API wintrust issuer CommonName Organization”和“wincrypt organization commonname”之类的东西,但就像API对所有其他发行者数据洗手不干一样。

iyfamqjs

iyfamqjs1#

您只需要使用CertNameToStr并设置&(pCertContext->pCertInfo->Issuer)参数:

  1. CertNameToStr(
  2. pCertContext->dwCertEncodingType,
  3. &(pCertContext->pCertInfo->Issuer),
  4. CERT_X500_NAME_STR,
  5. pszString,
  6. cbSize);

字符串
我修改了official sample供您参考:

  1. #pragma comment(lib, "crypt32.lib")
  2. #include <stdio.h>
  3. #include <tchar.h>
  4. #include <windows.h>
  5. #include <Wincrypt.h>
  6. #define MY_ENCODING_TYPE (PKCS_7_ASN_ENCODING | X509_ASN_ENCODING)
  7. #define MY_STRING_TYPE (CERT_OID_NAME_STR)
  8. void MyHandleError(LPTSTR);
  9. void main(void)
  10. {
  11. HCERTSTORE hCertStore;
  12. PCCERT_CONTEXT pCertContext;
  13. if (!(hCertStore = CertOpenStore(
  14. CERT_STORE_PROV_SYSTEM,
  15. MY_ENCODING_TYPE,
  16. NULL,
  17. CERT_SYSTEM_STORE_CURRENT_USER,
  18. L"MY")))
  19. {
  20. MyHandleError(TEXT("The MY system store did not open."));
  21. }
  22. pCertContext = NULL;
  23. while (pCertContext = CertEnumCertificatesInStore(
  24. hCertStore,
  25. pCertContext))
  26. {
  27. LPTSTR pszString;
  28. LPTSTR pszName;
  29. DWORD cbSize;
  30. CERT_BLOB blobEncodedName;
  31. if (!(cbSize = CertGetNameString(
  32. pCertContext,
  33. CERT_NAME_SIMPLE_DISPLAY_TYPE,
  34. 0,
  35. NULL,
  36. NULL,
  37. 0)))
  38. {
  39. MyHandleError(TEXT("CertGetName 1 failed."));
  40. }
  41. if (!(pszName = (LPTSTR)malloc(cbSize * sizeof(TCHAR))))
  42. {
  43. MyHandleError(TEXT("Memory allocation failed."));
  44. }
  45. if (CertGetNameString(
  46. pCertContext,
  47. CERT_NAME_SIMPLE_DISPLAY_TYPE,
  48. 0,
  49. NULL,
  50. pszName,
  51. cbSize))
  52. {
  53. _tprintf(TEXT("\nSubject -> %s.\n"), pszName);
  54. free(pszName);
  55. }
  56. else
  57. {
  58. MyHandleError(TEXT("CertGetName failed."));
  59. }
  60. if (!(cbSize = CertGetNameString(
  61. pCertContext,
  62. CERT_NAME_SIMPLE_DISPLAY_TYPE,
  63. CERT_NAME_ISSUER_FLAG,
  64. NULL,
  65. NULL,
  66. 0)))
  67. {
  68. MyHandleError(TEXT("CertGetName 1 failed."));
  69. }
  70. if (!(pszName = (LPTSTR)malloc(cbSize * sizeof(TCHAR))))
  71. {
  72. MyHandleError(TEXT("Memory allocation failed."));
  73. }
  74. if (CertGetNameString(
  75. pCertContext,
  76. CERT_NAME_SIMPLE_DISPLAY_TYPE,
  77. CERT_NAME_ISSUER_FLAG,
  78. NULL,
  79. pszName,
  80. cbSize))
  81. {
  82. _tprintf(TEXT("Issuer -> %s.\n"), pszName);
  83. free(pszName);
  84. }
  85. else
  86. {
  87. MyHandleError(TEXT("CertGetName failed."));
  88. }
  89. cbSize = CertNameToStr(
  90. pCertContext->dwCertEncodingType,
  91. &(pCertContext->pCertInfo->Subject),
  92. MY_STRING_TYPE,
  93. NULL,
  94. 0);
  95. if (1 == cbSize)
  96. {
  97. MyHandleError(TEXT("Subject name is an empty string."));
  98. }
  99. if (!(pszString = (LPTSTR)malloc(cbSize * sizeof(TCHAR))))
  100. {
  101. MyHandleError(TEXT("Memory allocation failed."));
  102. }
  103. cbSize = CertNameToStr(
  104. pCertContext->dwCertEncodingType,
  105. &(pCertContext->pCertInfo->Issuer),
  106. CERT_X500_NAME_STR,
  107. pszString,
  108. cbSize);
  109. if (1 == cbSize)
  110. {
  111. MyHandleError(TEXT("Issuer name is an empty string."));
  112. }
  113. else
  114. {
  115. printf("Issuer String = %ls\n", pszString); //what you want
  116. }
  117. if (!(CertStrToName(
  118. MY_ENCODING_TYPE,
  119. pszString,
  120. MY_STRING_TYPE,
  121. NULL,
  122. NULL, // NULL to get the number of bytes
  123. // needed for the buffer.
  124. &cbSize, // Pointer to a DWORD to hold the
  125. // number of bytes needed for the
  126. // buffer
  127. NULL))) // Optional address of a pointer to
  128. // old the location for an error in the
  129. // input string.
  130. {
  131. MyHandleError(
  132. TEXT("Could not get the length of the BLOB."));
  133. }
  134. if (!(blobEncodedName.pbData = (LPBYTE)malloc(cbSize)))
  135. {
  136. MyHandleError(
  137. TEXT("Memory Allocation for the BLOB failed."));
  138. }
  139. blobEncodedName.cbData = cbSize;
  140. if (CertStrToName(
  141. MY_ENCODING_TYPE,
  142. pszString,
  143. MY_STRING_TYPE,
  144. NULL,
  145. blobEncodedName.pbData,
  146. &blobEncodedName.cbData,
  147. NULL))
  148. {
  149. _tprintf(TEXT("CertStrToName created the BLOB.\n"));
  150. }
  151. else
  152. {
  153. MyHandleError(TEXT("Could not create the BLOB."));
  154. }
  155. free(blobEncodedName.pbData);
  156. free(pszString);
  157. }
  158. _tprintf(
  159. TEXT("\nThere are no more certificates in the store. \n"));
  160. if (CertCloseStore(
  161. hCertStore,
  162. CERT_CLOSE_STORE_CHECK_FLAG))
  163. {
  164. _tprintf(TEXT("The store is closed. ")
  165. TEXT("All certificates are released.\n"));
  166. }
  167. else
  168. {
  169. _tprintf(TEXT("The store was closed, ")
  170. TEXT("but certificates still in use.\n"));
  171. }
  172. _tprintf(TEXT("This demonstration program ran to completion ")
  173. TEXT("without error.\n"));
  174. }
  175. void MyHandleError(LPTSTR psz)
  176. {
  177. _ftprintf(stderr,
  178. TEXT("An error occurred in running the program. \n"));
  179. _ftprintf(stderr, TEXT("%s\n"), psz);
  180. _ftprintf(stderr, TEXT("Error number %x.\n"), GetLastError());
  181. _ftprintf(stderr, TEXT("Program terminating. \n"));
  182. exit(1);
  183. }

展开查看全部
lrpiutwd

lrpiutwd2#

我使用 CertGetNameStringW

  1. std::wstring subjectString(PCCERT_CONTEXT pCertContext) const
  2. {
  3. DWORD dwStrType = CERT_X500_NAME_STR;
  4. // Ensure we have a valid certificate context
  5. if (!pCertContext) {
  6. return L"";
  7. }
  8. // Get the size needed for the subject's name
  9. DWORD dwSubjectNameStrSize = CertGetNameStringW(
  10. pCertContext,
  11. CERT_NAME_RDN_TYPE,
  12. 0,
  13. &dwStrType,
  14. NULL,
  15. 0
  16. );
  17. if (dwSubjectNameStrSize > 0) {
  18. // Allocate memory for the subject's name
  19. std::wstring subjectName(dwSubjectNameStrSize-1, L'\0');
  20. // Get the subject's name
  21. if (CertGetNameStringW(
  22. pCertContext,
  23. CERT_NAME_RDN_TYPE,
  24. 0,
  25. &dwStrType,
  26. subjectName.data(),
  27. dwSubjectNameStrSize
  28. ) <= 0) {
  29. throw win32error("Extracting subject from certificate");
  30. }
  31. return subjectName;
  32. }
  33. return L"";
  34. }

字符串

展开查看全部

相关问题