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.

258 lines
7.4 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows NT Security
  4. // Copyright (C) Microsoft Corporation, 1997 - 1999
  5. //
  6. // File: origin.cpp
  7. //
  8. // Contents: Origin Identifier implementation
  9. //
  10. // History: 10-Sep-97 kirtd Created
  11. //
  12. //----------------------------------------------------------------------------
  13. #include <global.hxx>
  14. #include <dbgdef.h>
  15. //+---------------------------------------------------------------------------
  16. //
  17. // Function: CertGetOriginIdentifier
  18. //
  19. // Synopsis: get the origin identifier for a certificate
  20. //
  21. //----------------------------------------------------------------------------
  22. BOOL WINAPI CertGetOriginIdentifier (
  23. IN PCCERT_CONTEXT pCertContext,
  24. IN PCCERT_CONTEXT pIssuer,
  25. IN DWORD dwFlags,
  26. OUT CRYPT_ORIGIN_IDENTIFIER OriginIdentifier
  27. )
  28. {
  29. MD5_CTX md5ctx;
  30. PCERT_INFO pCertInfo = pCertContext->pCertInfo;
  31. PCERT_INFO pIssuerCertInfo = pIssuer->pCertInfo;
  32. MD5Init( &md5ctx );
  33. MD5Update( &md5ctx, pIssuerCertInfo->Subject.pbData, pIssuerCertInfo->Subject.cbData );
  34. MD5Update( &md5ctx, pCertInfo->Subject.pbData, pCertInfo->Subject.cbData );
  35. MD5Update(
  36. &md5ctx,
  37. (LPBYTE)pCertInfo->SubjectPublicKeyInfo.Algorithm.pszObjId,
  38. strlen( pCertInfo->SubjectPublicKeyInfo.Algorithm.pszObjId )
  39. );
  40. MD5Update(
  41. &md5ctx,
  42. pCertInfo->SubjectPublicKeyInfo.Algorithm.Parameters.pbData,
  43. pCertInfo->SubjectPublicKeyInfo.Algorithm.Parameters.cbData
  44. );
  45. // We assume that the unused public key bits are zero
  46. MD5Update(
  47. &md5ctx,
  48. pCertInfo->SubjectPublicKeyInfo.PublicKey.pbData,
  49. pCertInfo->SubjectPublicKeyInfo.PublicKey.cbData
  50. );
  51. MD5Update(
  52. &md5ctx,
  53. pIssuerCertInfo->SubjectPublicKeyInfo.PublicKey.pbData,
  54. pIssuerCertInfo->SubjectPublicKeyInfo.PublicKey.cbData
  55. );
  56. MD5Final( &md5ctx );
  57. memcpy( OriginIdentifier, md5ctx.digest, MD5DIGESTLEN );
  58. return( TRUE );
  59. }
  60. //+---------------------------------------------------------------------------
  61. //
  62. // Function: CtlGetOriginIdentifier
  63. //
  64. // Synopsis: get the origin identifier for a CTL
  65. //
  66. //----------------------------------------------------------------------------
  67. BOOL WINAPI CtlGetOriginIdentifier (
  68. IN PCCTL_CONTEXT pCtlContext,
  69. IN PCCERT_CONTEXT pIssuer,
  70. IN DWORD dwFlags,
  71. OUT CRYPT_ORIGIN_IDENTIFIER OriginIdentifier
  72. )
  73. {
  74. MD5_CTX md5ctx;
  75. DWORD cCount;
  76. PCTL_INFO pCtlInfo = pCtlContext->pCtlInfo;
  77. PCTL_USAGE pCtlUsage = &( pCtlContext->pCtlInfo->SubjectUsage );
  78. PCERT_INFO pIssuerCertInfo = pIssuer->pCertInfo;
  79. MD5Init( &md5ctx );
  80. MD5Update(
  81. &md5ctx,
  82. pIssuerCertInfo->Subject.pbData,
  83. pIssuerCertInfo->Subject.cbData
  84. );
  85. MD5Update(
  86. &md5ctx,
  87. pIssuerCertInfo->SerialNumber.pbData,
  88. pIssuerCertInfo->SerialNumber.cbData
  89. );
  90. for ( cCount = 0; cCount < pCtlUsage->cUsageIdentifier; cCount++ )
  91. {
  92. MD5Update(
  93. &md5ctx,
  94. (LPBYTE)pCtlUsage->rgpszUsageIdentifier[cCount],
  95. strlen( pCtlUsage->rgpszUsageIdentifier[cCount] )
  96. );
  97. }
  98. MD5Update(
  99. &md5ctx,
  100. pCtlInfo->ListIdentifier.pbData,
  101. pCtlInfo->ListIdentifier.cbData
  102. );
  103. MD5Update(
  104. &md5ctx,
  105. pIssuerCertInfo->SubjectPublicKeyInfo.PublicKey.pbData,
  106. pIssuerCertInfo->SubjectPublicKeyInfo.PublicKey.cbData
  107. );
  108. MD5Final( &md5ctx );
  109. memcpy( OriginIdentifier, md5ctx.digest, MD5DIGESTLEN );
  110. return( TRUE );
  111. }
  112. //+---------------------------------------------------------------------------
  113. //
  114. // Function: CrlGetOriginIdentifierFromCrlIssuer
  115. //
  116. // Synopsis: get origin identifier for a CRL given the CRL's issuer cert
  117. //
  118. // Comments: A freshest, delta CRL will have a different OriginIdentifier
  119. // from a base CRL having the same issuer.
  120. //
  121. //----------------------------------------------------------------------------
  122. BOOL WINAPI CrlGetOriginIdentifierFromCrlIssuer (
  123. IN PCCERT_CONTEXT pIssuerContext,
  124. IN PCERT_NAME_BLOB pIssuerName,
  125. IN BOOL fFreshest,
  126. OUT CRYPT_ORIGIN_IDENTIFIER OriginIdentifier
  127. )
  128. {
  129. MD5_CTX md5ctx;
  130. PCERT_INFO pIssuerCertInfo = pIssuerContext->pCertInfo;
  131. BYTE bFreshest;
  132. MD5Init( &md5ctx );
  133. if (fFreshest)
  134. {
  135. bFreshest = 1;
  136. }
  137. else
  138. {
  139. bFreshest = 0;
  140. }
  141. MD5Update(
  142. &md5ctx,
  143. &bFreshest,
  144. sizeof(bFreshest)
  145. );
  146. MD5Update(
  147. &md5ctx,
  148. pIssuerName->pbData,
  149. pIssuerName->cbData
  150. );
  151. MD5Update(
  152. &md5ctx,
  153. pIssuerCertInfo->SubjectPublicKeyInfo.PublicKey.pbData,
  154. pIssuerCertInfo->SubjectPublicKeyInfo.PublicKey.cbData
  155. );
  156. MD5Final( &md5ctx );
  157. memcpy( OriginIdentifier, md5ctx.digest, MD5DIGESTLEN );
  158. return( TRUE );
  159. }
  160. //+---------------------------------------------------------------------------
  161. //
  162. // Function: CrlGetOriginIdentifier
  163. //
  164. // Synopsis: get the origin identifier for a CRL
  165. //
  166. //----------------------------------------------------------------------------
  167. BOOL WINAPI CrlGetOriginIdentifier (
  168. IN PCCRL_CONTEXT pCrlContext,
  169. IN PCCERT_CONTEXT pIssuer,
  170. IN DWORD dwFlags,
  171. OUT CRYPT_ORIGIN_IDENTIFIER OriginIdentifier
  172. )
  173. {
  174. BOOL fFreshest;
  175. // See if this is a delta, freshest CRL.
  176. if (CertFindExtension(
  177. szOID_DELTA_CRL_INDICATOR,
  178. pCrlContext->pCrlInfo->cExtension,
  179. pCrlContext->pCrlInfo->rgExtension
  180. ))
  181. {
  182. fFreshest = TRUE;
  183. }
  184. else
  185. {
  186. fFreshest = FALSE;
  187. }
  188. return CrlGetOriginIdentifierFromCrlIssuer (
  189. pIssuer,
  190. &pCrlContext->pCrlInfo->Issuer,
  191. fFreshest,
  192. OriginIdentifier
  193. );
  194. }
  195. //+---------------------------------------------------------------------------
  196. //
  197. // Function: CrlGetOriginIdentifierFromSubjectCert
  198. //
  199. // Synopsis: get origin identifier for a CRL given the subject cert
  200. //
  201. // Comments: OBJECT_CONTEXT_FRESHEST_CRL_FLAG can be set in dwFlags.
  202. //
  203. // Assumption: Subject certificate and CRL's issuer are the same.
  204. //----------------------------------------------------------------------------
  205. BOOL WINAPI CrlGetOriginIdentifierFromSubjectCert (
  206. IN PCCERT_CONTEXT pSubjectCert,
  207. IN PCCERT_CONTEXT pIssuer,
  208. IN BOOL fFreshest,
  209. OUT CRYPT_ORIGIN_IDENTIFIER OriginIdentifier
  210. )
  211. {
  212. //
  213. // NOTENOTE: For the first version of this code we assume that the
  214. // issuer of the CRL and the issuer of a subject certificate
  215. // in the CRL are the same. Therefore, we can calculate
  216. // the CRL origin identifier by using the subject cert's
  217. // issuer name
  218. //
  219. return CrlGetOriginIdentifierFromCrlIssuer (
  220. pIssuer,
  221. &pSubjectCert->pCertInfo->Issuer,
  222. fFreshest,
  223. OriginIdentifier
  224. );
  225. }