Source code of Windows XP (NT5)
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.

387 lines
8.8 KiB

  1. /*++
  2. Copyright (c) 1999 Microsoft Corporation
  3. Module Name:
  4. template.c
  5. Abstract:
  6. Contains common template matching code
  7. Author:
  8. BrianSw 10-19-200
  9. Environment:
  10. User Level: Win32/kernel
  11. NOTE: Since this is used by user and kernel mode, code accordingly
  12. Revision History:
  13. --*/
  14. #include "precomp.h"
  15. BOOL
  16. WINAPI IsAllZero(BYTE *c, DWORD dwSize)
  17. {
  18. DWORD i;
  19. for (i=0;i<dwSize;i++) {
  20. if (c[i] != 0) {
  21. return FALSE;
  22. }
  23. }
  24. return TRUE;
  25. }
  26. BOOL
  27. WINAPI CmpBlob(IPSEC_BYTE_BLOB* c1, IPSEC_BYTE_BLOB *c2)
  28. {
  29. if (c1->dwSize == 0) {
  30. return TRUE;
  31. }
  32. if (c1->dwSize != c2->dwSize) {
  33. return FALSE;
  34. }
  35. if (memcmp(c1->pBlob,c2->pBlob,c1->dwSize) == 0) {
  36. return TRUE;
  37. }
  38. return FALSE;
  39. }
  40. BOOL
  41. WINAPI CmpData(BYTE* c1, BYTE *c2, DWORD size)
  42. {
  43. if ((!IsAllZero(c1,size)) &&
  44. (memcmp(c1,c2,size) != 0)) {
  45. return FALSE;
  46. }
  47. return TRUE;
  48. }
  49. /*
  50. For comparing structs like:
  51. typedef struct _PROTOCOL {
  52. PROTOCOL_TYPE ProtocolType;
  53. DWORD dwProtocol;
  54. } PROTOCOL, * PPROTOCOL;
  55. dwTypeSize is sizeof PROTOCOL_TYPE, dwStructSize is sizeof(PROTOCOL)
  56. Assumes type info is first in struct
  57. Template symantics:
  58. Template is:
  59. All 0, everything matches
  60. Type 0, rest non-0, exact match of rest of data
  61. Type non-0, rest 0, all entries of given type
  62. Type non-0, rest non-0, exact match
  63. */
  64. BOOL
  65. WINAPI CmpTypeStruct(BYTE *Template, BYTE *comp,
  66. DWORD dwTypeSize, DWORD dwStructSize)
  67. {
  68. if (IsAllZero(Template,dwStructSize)) {
  69. return TRUE;
  70. }
  71. if (IsAllZero(Template,dwTypeSize)) {
  72. if (memcmp(Template+dwTypeSize,comp+dwTypeSize,
  73. dwStructSize-dwTypeSize) == 0) {
  74. return TRUE;
  75. }
  76. return FALSE;
  77. }
  78. // Know here that Template.TypeInfo is non-0
  79. if (memcmp(Template,comp,dwTypeSize) != 0) {
  80. return FALSE;
  81. }
  82. if (IsAllZero(Template+dwTypeSize,dwStructSize-dwTypeSize)) {
  83. return TRUE;
  84. }
  85. if (memcmp(Template+dwTypeSize,comp+dwTypeSize,dwStructSize-dwTypeSize) == 0) {
  86. return TRUE;
  87. }
  88. return FALSE;
  89. }
  90. BOOL
  91. WINAPI CmpAddr(ADDR *Template, ADDR *a2)
  92. {
  93. if (Template->AddrType == IP_ADDR_UNIQUE && Template->uIpAddr) {
  94. if (Template->uIpAddr != (a2->uIpAddr)) {
  95. return FALSE;
  96. }
  97. if (a2->AddrType != IP_ADDR_UNIQUE) {
  98. return FALSE;
  99. }
  100. }
  101. if (Template->AddrType == IP_ADDR_SUBNET && Template->uIpAddr) {
  102. if ((Template->uIpAddr & Template->uSubNetMask)
  103. != (a2->uIpAddr & Template->uSubNetMask)) {
  104. return FALSE;
  105. }
  106. // Make sure template subnet contains a2's subnet (if a2 is unique, any subnet is superset of unique filter
  107. if (a2->AddrType == IP_ADDR_SUBNET &&
  108. ((Template->uSubNetMask & a2->uSubNetMask) != Template->uSubNetMask)) {
  109. return FALSE;
  110. }
  111. }
  112. return TRUE;
  113. }
  114. BOOL
  115. WINAPI CmpFilter(IPSEC_QM_FILTER *Template, IPSEC_QM_FILTER* f2)
  116. {
  117. if (!CmpTypeStruct((BYTE*)&Template->Protocol,
  118. (BYTE*)&f2->Protocol,
  119. sizeof(PROTOCOL_TYPE),
  120. sizeof(PROTOCOL))) {
  121. return FALSE;
  122. }
  123. if (!CmpTypeStruct((BYTE*)&Template->SrcPort,
  124. (BYTE*)&f2->SrcPort,
  125. sizeof(PORT_TYPE),
  126. sizeof(PORT))) {
  127. return FALSE;
  128. }
  129. if (!CmpTypeStruct((BYTE*)&Template->DesPort,
  130. (BYTE*)&f2->DesPort,
  131. sizeof(PORT_TYPE),
  132. sizeof(PORT))) {
  133. return FALSE;
  134. }
  135. if (Template->QMFilterType) {
  136. if (Template->QMFilterType != f2->QMFilterType) {
  137. return FALSE;
  138. }
  139. }
  140. if (!CmpData((BYTE*)&Template->MyTunnelEndpt,
  141. (BYTE*)&f2->MyTunnelEndpt,
  142. sizeof(ADDR))) {
  143. return FALSE;
  144. }
  145. if (!CmpData((BYTE*)&Template->PeerTunnelEndpt,
  146. (BYTE*)&f2->PeerTunnelEndpt,
  147. sizeof(ADDR))) {
  148. return FALSE;
  149. }
  150. if (!CmpAddr(&Template->SrcAddr,&f2->SrcAddr)) {
  151. return FALSE;
  152. }
  153. if (!CmpAddr(&Template->DesAddr,&f2->DesAddr)) {
  154. return FALSE;
  155. }
  156. return TRUE;
  157. }
  158. BOOL
  159. WINAPI CmpQMAlgo(PIPSEC_QM_ALGO Template, PIPSEC_QM_ALGO a2)
  160. {
  161. if (!CmpData((BYTE*)&Template->Operation,
  162. (BYTE*)&a2->Operation,
  163. sizeof(IPSEC_OPERATION))) {
  164. return FALSE;
  165. }
  166. if (!CmpData((BYTE*)&Template->uAlgoIdentifier,
  167. (BYTE*)&a2->uAlgoIdentifier,
  168. sizeof(ULONG))) {
  169. return FALSE;
  170. }
  171. if (!CmpData((BYTE*)&Template->uSecAlgoIdentifier,
  172. (BYTE*)&a2->uSecAlgoIdentifier,
  173. sizeof(HMAC_AH_ALGO))) {
  174. return FALSE;
  175. }
  176. if (!CmpData((BYTE*)&Template->MySpi,
  177. (BYTE*)&a2->MySpi,
  178. sizeof(IPSEC_QM_SPI))) {
  179. return FALSE;
  180. }
  181. if (!CmpData((BYTE*)&Template->PeerSpi,
  182. (BYTE*)&a2->PeerSpi,
  183. sizeof(IPSEC_QM_SPI))) {
  184. return FALSE;
  185. }
  186. return TRUE;
  187. }
  188. BOOL
  189. WINAPI CmpQMOffer(PIPSEC_QM_OFFER Template, PIPSEC_QM_OFFER o2)
  190. {
  191. DWORD i;
  192. if (!CmpData((BYTE*)&Template->Lifetime,
  193. (BYTE*)&o2->Lifetime,
  194. sizeof(KEY_LIFETIME))) {
  195. return FALSE;
  196. }
  197. if (Template->bPFSRequired) {
  198. if (Template->bPFSRequired != o2->bPFSRequired) {
  199. return FALSE;
  200. }
  201. }
  202. if (Template->dwPFSGroup) {
  203. if (Template->dwPFSGroup != o2->dwPFSGroup) {
  204. return FALSE;
  205. }
  206. }
  207. if (Template->dwNumAlgos) {
  208. if (Template->dwNumAlgos != o2->dwNumAlgos) {
  209. return FALSE;
  210. }
  211. for (i=0; i < Template->dwNumAlgos; i++) {
  212. if (!CmpQMAlgo(&Template->Algos[i],
  213. &o2->Algos[i])) {
  214. return FALSE;
  215. }
  216. }
  217. }
  218. return TRUE;
  219. }
  220. /*
  221. True if this NotifyListEntry Template matches the CurInfo
  222. */
  223. BOOL
  224. WINAPI MatchQMSATemplate(IPSEC_QM_SA *Template,IPSEC_QM_SA *CurInfo)
  225. {
  226. if (Template == NULL) {
  227. return TRUE;
  228. }
  229. if (!CmpFilter(&Template->IpsecQMFilter,
  230. &CurInfo->IpsecQMFilter)) {
  231. return FALSE;
  232. }
  233. if (!CmpData((BYTE*)&Template->MMSpi.Initiator,
  234. (BYTE*)&CurInfo->MMSpi.Initiator,sizeof(IKE_COOKIE))) {
  235. return FALSE;
  236. }
  237. if (!CmpData((BYTE*)&Template->MMSpi.Responder,
  238. (BYTE*)&CurInfo->MMSpi.Responder,sizeof(IKE_COOKIE))) {
  239. return FALSE;
  240. }
  241. if (!CmpData((BYTE*)&Template->gQMPolicyID,
  242. (BYTE*)&CurInfo->gQMPolicyID,sizeof(GUID))) {
  243. return FALSE;
  244. }
  245. if (!CmpQMOffer(&Template->SelectedQMOffer,
  246. &CurInfo->SelectedQMOffer)) {
  247. return FALSE;
  248. }
  249. return TRUE;
  250. }
  251. BOOL
  252. WINAPI MatchMMSATemplate(IPSEC_MM_SA *MMTemplate, IPSEC_MM_SA *SaData)
  253. {
  254. if (MMTemplate == NULL) {
  255. return TRUE;
  256. }
  257. if (!CmpData((BYTE*)&MMTemplate->gMMPolicyID,
  258. (BYTE*)&SaData->gMMPolicyID,sizeof(GUID))) {
  259. return FALSE;
  260. }
  261. if (!CmpData((BYTE*)&MMTemplate->MMSpi.Initiator,
  262. (BYTE*)&SaData->MMSpi.Initiator,sizeof(COOKIE))) {
  263. return FALSE;
  264. }
  265. if (!CmpData((BYTE*)&MMTemplate->MMSpi.Responder,
  266. (BYTE*)&SaData->MMSpi.Responder,sizeof(COOKIE))) {
  267. return FALSE;
  268. }
  269. if (!CmpAddr(&MMTemplate->Me,&SaData->Me)) {
  270. return FALSE;
  271. }
  272. if (!CmpAddr(&MMTemplate->Peer,&SaData->Peer)) {
  273. return FALSE;
  274. }
  275. if (!CmpData((BYTE*)&MMTemplate->SelectedMMOffer.EncryptionAlgorithm,(BYTE*)&SaData->SelectedMMOffer.EncryptionAlgorithm,sizeof(IPSEC_MM_ALGO))) {
  276. return FALSE;
  277. }
  278. if (!CmpData((BYTE*)&MMTemplate->SelectedMMOffer.HashingAlgorithm,(BYTE*)&SaData->SelectedMMOffer.HashingAlgorithm,sizeof(IPSEC_MM_ALGO))) {
  279. return FALSE;
  280. }
  281. if (!CmpData((BYTE*)&MMTemplate->SelectedMMOffer.dwDHGroup,(BYTE*)&SaData->SelectedMMOffer.dwDHGroup,sizeof(DWORD))) {
  282. return FALSE;
  283. }
  284. if (!CmpData((BYTE*)&MMTemplate->SelectedMMOffer.Lifetime,(BYTE*)&SaData->SelectedMMOffer.Lifetime,sizeof(KEY_LIFETIME))) {
  285. return FALSE;
  286. }
  287. if (!CmpData((BYTE*)&MMTemplate->SelectedMMOffer.dwQuickModeLimit,(BYTE*)&SaData->SelectedMMOffer.dwQuickModeLimit,sizeof(DWORD))) {
  288. return FALSE;
  289. }
  290. if (!CmpBlob(&MMTemplate->MyId,&SaData->MyId)) {
  291. return FALSE;
  292. }
  293. if (!CmpBlob(&MMTemplate->PeerId,&SaData->PeerId)) {
  294. return FALSE;
  295. }
  296. if (!CmpBlob(&MMTemplate->MyCertificateChain,&SaData->MyCertificateChain)) {
  297. return FALSE;
  298. }
  299. if (!CmpBlob(&MMTemplate->PeerCertificateChain,&SaData->PeerCertificateChain)) {
  300. return FALSE;
  301. }
  302. return TRUE;
  303. }