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.

217 lines
5.5 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (C) Microsoft Corporation, 1996 - 1999
  6. //
  7. // File: sphelper.cpp
  8. //
  9. // Contents: Microsoft Internet Security Authenticode Policy Provider
  10. //
  11. // Functions: spGetAgencyNameOfCert
  12. // spGetAgencyNameOfCert
  13. // spGetPublisherNameOfCert
  14. // spGetCommonNameExtension
  15. // spGetAgencyName
  16. // spGetRDNAttrWStr
  17. //
  18. // History: 05-Oct-1997 pberkman created
  19. //
  20. //--------------------------------------------------------------------------
  21. #include "global.hxx"
  22. WCHAR *spGetAgencyNameOfCert(PCCERT_CONTEXT pCert)
  23. {
  24. LPWSTR pwsz;
  25. if (pwsz = spGetCommonNameExtension(pCert))
  26. {
  27. return(pwsz);
  28. }
  29. return(spGetAgencyName(&pCert->pCertInfo->Subject));
  30. }
  31. WCHAR *spGetPublisherNameOfCert(IN PCCERT_CONTEXT pCert)
  32. {
  33. LPWSTR pwsz;
  34. if (pwsz = spGetCommonNameExtension(pCert))
  35. {
  36. return(pwsz);
  37. }
  38. return(spGetRDNAttrWStr(szOID_COMMON_NAME, &pCert->pCertInfo->Subject));
  39. }
  40. WCHAR *spGetCommonNameExtension(PCCERT_CONTEXT pCert)
  41. {
  42. LPWSTR pwsz = NULL;
  43. PCERT_NAME_VALUE pNameValue = NULL;
  44. PCERT_EXTENSION pExt;
  45. pwsz = NULL;
  46. pNameValue = NULL;
  47. pExt = CertFindExtension(SPC_COMMON_NAME_OBJID,
  48. pCert->pCertInfo->cExtension,
  49. pCert->pCertInfo->rgExtension);
  50. if (pExt)
  51. {
  52. DWORD cbInfo;
  53. PCERT_RDN_VALUE_BLOB pValue;
  54. DWORD dwValueType;
  55. DWORD cwsz;
  56. cbInfo = 0;
  57. CryptDecodeObject( X509_ASN_ENCODING,
  58. X509_NAME_VALUE,
  59. pExt->Value.pbData,
  60. pExt->Value.cbData,
  61. 0,
  62. NULL,
  63. &cbInfo);
  64. if (cbInfo == 0)
  65. {
  66. return(NULL);
  67. }
  68. if (!(pNameValue = (PCERT_NAME_VALUE)new BYTE[cbInfo]))
  69. {
  70. return(NULL);
  71. }
  72. if (!(CryptDecodeObject(X509_ASN_ENCODING,
  73. X509_NAME_VALUE,
  74. pExt->Value.pbData,
  75. pExt->Value.cbData,
  76. 0,
  77. pNameValue,
  78. &cbInfo)))
  79. {
  80. delete pNameValue;
  81. return(NULL);
  82. }
  83. dwValueType = pNameValue->dwValueType;
  84. pValue = &pNameValue->Value;
  85. cwsz = CertRDNValueToStrW(dwValueType,
  86. pValue,
  87. NULL,
  88. 0);
  89. if (cwsz > 1)
  90. {
  91. pwsz = new WCHAR[cwsz];
  92. if (pwsz)
  93. {
  94. CertRDNValueToStrW(dwValueType,
  95. pValue,
  96. pwsz,
  97. cwsz);
  98. }
  99. }
  100. }
  101. delete pNameValue;
  102. return(pwsz);
  103. }
  104. WCHAR *spGetAgencyName(IN PCERT_NAME_BLOB pNameBlob)
  105. {
  106. LPWSTR pwsz;
  107. if (pwsz = spGetRDNAttrWStr(szOID_ORGANIZATIONAL_UNIT_NAME, pNameBlob))
  108. {
  109. return(pwsz);
  110. }
  111. if (pwsz = spGetRDNAttrWStr(szOID_ORGANIZATION_NAME, pNameBlob))
  112. {
  113. return(pwsz);
  114. }
  115. return(spGetRDNAttrWStr(szOID_COMMON_NAME, pNameBlob));
  116. }
  117. WCHAR *spGetRDNAttrWStr(IN LPCSTR pszObjId, IN PCERT_NAME_BLOB pNameBlob)
  118. {
  119. LPWSTR pwsz;
  120. PCERT_NAME_INFO pNameInfo;
  121. PCERT_RDN_ATTR pRDNAttr;
  122. DWORD cbInfo;
  123. pwsz = NULL;
  124. pNameInfo = NULL;
  125. cbInfo = 0;
  126. CryptDecodeObject( X509_ASN_ENCODING,
  127. X509_NAME,
  128. pNameBlob->pbData,
  129. pNameBlob->cbData,
  130. 0,
  131. NULL,
  132. &cbInfo);
  133. if (cbInfo == 0)
  134. {
  135. return(NULL);
  136. }
  137. if (!(pNameInfo = (PCERT_NAME_INFO)new BYTE[cbInfo]))
  138. {
  139. return(NULL);
  140. }
  141. if (!(CryptDecodeObject(X509_ASN_ENCODING,
  142. X509_NAME,
  143. pNameBlob->pbData,
  144. pNameBlob->cbData,
  145. 0,
  146. pNameInfo,
  147. &cbInfo)))
  148. {
  149. delete pNameInfo;
  150. return(NULL);
  151. }
  152. pRDNAttr = CertFindRDNAttr(pszObjId, pNameInfo);
  153. if (pRDNAttr)
  154. {
  155. PCERT_RDN_VALUE_BLOB pValue = &pRDNAttr->Value;
  156. DWORD dwValueType = pRDNAttr->dwValueType;
  157. DWORD cwsz;
  158. pValue = &pRDNAttr->Value;
  159. dwValueType = pRDNAttr->dwValueType;
  160. cwsz = CertRDNValueToStrW(dwValueType,
  161. pValue,
  162. NULL,
  163. 0);
  164. if (cwsz > 1)
  165. {
  166. pwsz = new WCHAR[cwsz];
  167. if (pwsz)
  168. {
  169. CertRDNValueToStrW(dwValueType,
  170. pValue,
  171. pwsz,
  172. cwsz);
  173. }
  174. }
  175. }
  176. delete pNameInfo;
  177. return(pwsz);
  178. }