PkixNameConstraintValidator.cs 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.Collections;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1;
  6. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.X509;
  7. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  8. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Collections;
  9. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Pkix
  10. {
  11. public class PkixNameConstraintValidator
  12. {
  13. private ISet excludedSubtreesDN = new HashSet();
  14. private ISet excludedSubtreesDNS = new HashSet();
  15. private ISet excludedSubtreesEmail = new HashSet();
  16. private ISet excludedSubtreesURI = new HashSet();
  17. private ISet excludedSubtreesIP = new HashSet();
  18. private ISet permittedSubtreesDN;
  19. private ISet permittedSubtreesDNS;
  20. private ISet permittedSubtreesEmail;
  21. private ISet permittedSubtreesURI;
  22. private ISet permittedSubtreesIP;
  23. public PkixNameConstraintValidator()
  24. {
  25. }
  26. private static bool WithinDNSubtree(
  27. Asn1Sequence dns,
  28. Asn1Sequence subtree)
  29. {
  30. if (subtree.Count < 1)
  31. {
  32. return false;
  33. }
  34. if (subtree.Count > dns.Count)
  35. {
  36. return false;
  37. }
  38. for (int j = subtree.Count - 1; j >= 0; j--)
  39. {
  40. if (!(subtree[j].Equals(dns[j])))
  41. {
  42. return false;
  43. }
  44. }
  45. return true;
  46. }
  47. public void CheckPermittedDN(Asn1Sequence dns)
  48. //throws PkixNameConstraintValidatorException
  49. {
  50. CheckPermittedDN(permittedSubtreesDN, dns);
  51. }
  52. public void CheckExcludedDN(Asn1Sequence dns)
  53. //throws PkixNameConstraintValidatorException
  54. {
  55. CheckExcludedDN(excludedSubtreesDN, dns);
  56. }
  57. private void CheckPermittedDN(ISet permitted, Asn1Sequence dns)
  58. //throws PkixNameConstraintValidatorException
  59. {
  60. if (permitted == null)
  61. {
  62. return;
  63. }
  64. if ((permitted.Count == 0) && dns.Count == 0)
  65. {
  66. return;
  67. }
  68. IEnumerator it = permitted.GetEnumerator();
  69. while (it.MoveNext())
  70. {
  71. Asn1Sequence subtree = (Asn1Sequence)it.Current;
  72. if (WithinDNSubtree(dns, subtree))
  73. {
  74. return;
  75. }
  76. }
  77. throw new PkixNameConstraintValidatorException(
  78. "Subject distinguished name is not from a permitted subtree");
  79. }
  80. private void CheckExcludedDN(ISet excluded, Asn1Sequence dns)
  81. //throws PkixNameConstraintValidatorException
  82. {
  83. if (excluded.IsEmpty)
  84. {
  85. return;
  86. }
  87. IEnumerator it = excluded.GetEnumerator();
  88. while (it.MoveNext())
  89. {
  90. Asn1Sequence subtree = (Asn1Sequence)it.Current;
  91. if (WithinDNSubtree(dns, subtree))
  92. {
  93. throw new PkixNameConstraintValidatorException(
  94. "Subject distinguished name is from an excluded subtree");
  95. }
  96. }
  97. }
  98. private ISet IntersectDN(ISet permitted, ISet dns)
  99. {
  100. ISet intersect = new HashSet();
  101. for (IEnumerator it = dns.GetEnumerator(); it.MoveNext(); )
  102. {
  103. Asn1Sequence dn = Asn1Sequence.GetInstance(((GeneralSubtree)it
  104. .Current).Base.Name.ToAsn1Object());
  105. if (permitted == null)
  106. {
  107. if (dn != null)
  108. {
  109. intersect.Add(dn);
  110. }
  111. }
  112. else
  113. {
  114. IEnumerator _iter = permitted.GetEnumerator();
  115. while (_iter.MoveNext())
  116. {
  117. Asn1Sequence subtree = (Asn1Sequence)_iter.Current;
  118. if (WithinDNSubtree(dn, subtree))
  119. {
  120. intersect.Add(dn);
  121. }
  122. else if (WithinDNSubtree(subtree, dn))
  123. {
  124. intersect.Add(subtree);
  125. }
  126. }
  127. }
  128. }
  129. return intersect;
  130. }
  131. private ISet UnionDN(ISet excluded, Asn1Sequence dn)
  132. {
  133. if (excluded.IsEmpty)
  134. {
  135. if (dn == null)
  136. {
  137. return excluded;
  138. }
  139. excluded.Add(dn);
  140. return excluded;
  141. }
  142. else
  143. {
  144. ISet intersect = new HashSet();
  145. IEnumerator it = excluded.GetEnumerator();
  146. while (it.MoveNext())
  147. {
  148. Asn1Sequence subtree = (Asn1Sequence)it.Current;
  149. if (WithinDNSubtree(dn, subtree))
  150. {
  151. intersect.Add(subtree);
  152. }
  153. else if (WithinDNSubtree(subtree, dn))
  154. {
  155. intersect.Add(dn);
  156. }
  157. else
  158. {
  159. intersect.Add(subtree);
  160. intersect.Add(dn);
  161. }
  162. }
  163. return intersect;
  164. }
  165. }
  166. private ISet IntersectEmail(ISet permitted, ISet emails)
  167. {
  168. ISet intersect = new HashSet();
  169. for (IEnumerator it = emails.GetEnumerator(); it.MoveNext(); )
  170. {
  171. String email = ExtractNameAsString(((GeneralSubtree)it.Current)
  172. .Base);
  173. if (permitted == null)
  174. {
  175. if (email != null)
  176. {
  177. intersect.Add(email);
  178. }
  179. }
  180. else
  181. {
  182. IEnumerator it2 = permitted.GetEnumerator();
  183. while (it2.MoveNext())
  184. {
  185. String _permitted = (String)it2.Current;
  186. intersectEmail(email, _permitted, intersect);
  187. }
  188. }
  189. }
  190. return intersect;
  191. }
  192. private ISet UnionEmail(ISet excluded, String email)
  193. {
  194. if (excluded.IsEmpty)
  195. {
  196. if (email == null)
  197. {
  198. return excluded;
  199. }
  200. excluded.Add(email);
  201. return excluded;
  202. }
  203. else
  204. {
  205. ISet union = new HashSet();
  206. IEnumerator it = excluded.GetEnumerator();
  207. while (it.MoveNext())
  208. {
  209. String _excluded = (String)it.Current;
  210. unionEmail(_excluded, email, union);
  211. }
  212. return union;
  213. }
  214. }
  215. /**
  216. * Returns the intersection of the permitted IP ranges in
  217. * <code>permitted</code> with <code>ip</code>.
  218. *
  219. * @param permitted A <code>Set</code> of permitted IP addresses with
  220. * their subnet mask as byte arrays.
  221. * @param ips The IP address with its subnet mask.
  222. * @return The <code>Set</code> of permitted IP ranges intersected with
  223. * <code>ip</code>.
  224. */
  225. private ISet IntersectIP(ISet permitted, ISet ips)
  226. {
  227. ISet intersect = new HashSet();
  228. for (IEnumerator it = ips.GetEnumerator(); it.MoveNext(); )
  229. {
  230. byte[] ip = Asn1OctetString.GetInstance(
  231. ((GeneralSubtree)it.Current).Base.Name).GetOctets();
  232. if (permitted == null)
  233. {
  234. if (ip != null)
  235. {
  236. intersect.Add(ip);
  237. }
  238. }
  239. else
  240. {
  241. IEnumerator it2 = permitted.GetEnumerator();
  242. while (it2.MoveNext())
  243. {
  244. byte[] _permitted = (byte[])it2.Current;
  245. intersect.AddAll(IntersectIPRange(_permitted, ip));
  246. }
  247. }
  248. }
  249. return intersect;
  250. }
  251. /**
  252. * Returns the union of the excluded IP ranges in <code>excluded</code>
  253. * with <code>ip</code>.
  254. *
  255. * @param excluded A <code>Set</code> of excluded IP addresses with their
  256. * subnet mask as byte arrays.
  257. * @param ip The IP address with its subnet mask.
  258. * @return The <code>Set</code> of excluded IP ranges unified with
  259. * <code>ip</code> as byte arrays.
  260. */
  261. private ISet UnionIP(ISet excluded, byte[] ip)
  262. {
  263. if (excluded.IsEmpty)
  264. {
  265. if (ip == null)
  266. {
  267. return excluded;
  268. }
  269. excluded.Add(ip);
  270. return excluded;
  271. }
  272. else
  273. {
  274. ISet union = new HashSet();
  275. IEnumerator it = excluded.GetEnumerator();
  276. while (it.MoveNext())
  277. {
  278. byte[] _excluded = (byte[])it.Current;
  279. union.AddAll(UnionIPRange(_excluded, ip));
  280. }
  281. return union;
  282. }
  283. }
  284. /**
  285. * Calculates the union if two IP ranges.
  286. *
  287. * @param ipWithSubmask1 The first IP address with its subnet mask.
  288. * @param ipWithSubmask2 The second IP address with its subnet mask.
  289. * @return A <code>Set</code> with the union of both addresses.
  290. */
  291. private ISet UnionIPRange(byte[] ipWithSubmask1, byte[] ipWithSubmask2)
  292. {
  293. ISet set = new HashSet();
  294. // difficult, adding always all IPs is not wrong
  295. if (BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Arrays.AreEqual(ipWithSubmask1, ipWithSubmask2))
  296. {
  297. set.Add(ipWithSubmask1);
  298. }
  299. else
  300. {
  301. set.Add(ipWithSubmask1);
  302. set.Add(ipWithSubmask2);
  303. }
  304. return set;
  305. }
  306. /**
  307. * Calculates the interesction if two IP ranges.
  308. *
  309. * @param ipWithSubmask1 The first IP address with its subnet mask.
  310. * @param ipWithSubmask2 The second IP address with its subnet mask.
  311. * @return A <code>Set</code> with the single IP address with its subnet
  312. * mask as a byte array or an empty <code>Set</code>.
  313. */
  314. private ISet IntersectIPRange(byte[] ipWithSubmask1, byte[] ipWithSubmask2)
  315. {
  316. if (ipWithSubmask1.Length != ipWithSubmask2.Length)
  317. {
  318. //Collections.EMPTY_SET;
  319. return new HashSet();
  320. }
  321. byte[][] temp = ExtractIPsAndSubnetMasks(ipWithSubmask1, ipWithSubmask2);
  322. byte[] ip1 = temp[0];
  323. byte[] subnetmask1 = temp[1];
  324. byte[] ip2 = temp[2];
  325. byte[] subnetmask2 = temp[3];
  326. byte[][] minMax = MinMaxIPs(ip1, subnetmask1, ip2, subnetmask2);
  327. byte[] min;
  328. byte[] max;
  329. max = Min(minMax[1], minMax[3]);
  330. min = Max(minMax[0], minMax[2]);
  331. // minimum IP address must be bigger than max
  332. if (CompareTo(min, max) == 1)
  333. {
  334. //return Collections.EMPTY_SET;
  335. return new HashSet();
  336. }
  337. // OR keeps all significant bits
  338. byte[] ip = Or(minMax[0], minMax[2]);
  339. byte[] subnetmask = Or(subnetmask1, subnetmask2);
  340. //return new HashSet( ICollectionsingleton(IpWithSubnetMask(ip, subnetmask));
  341. ISet hs = new HashSet();
  342. hs.Add(IpWithSubnetMask(ip, subnetmask));
  343. return hs;
  344. }
  345. /**
  346. * Concatenates the IP address with its subnet mask.
  347. *
  348. * @param ip The IP address.
  349. * @param subnetMask Its subnet mask.
  350. * @return The concatenated IP address with its subnet mask.
  351. */
  352. private byte[] IpWithSubnetMask(byte[] ip, byte[] subnetMask)
  353. {
  354. int ipLength = ip.Length;
  355. byte[] temp = new byte[ipLength * 2];
  356. Array.Copy(ip, 0, temp, 0, ipLength);
  357. Array.Copy(subnetMask, 0, temp, ipLength, ipLength);
  358. return temp;
  359. }
  360. /**
  361. * Splits the IP addresses and their subnet mask.
  362. *
  363. * @param ipWithSubmask1 The first IP address with the subnet mask.
  364. * @param ipWithSubmask2 The second IP address with the subnet mask.
  365. * @return An array with two elements. Each element contains the IP address
  366. * and the subnet mask in this order.
  367. */
  368. private byte[][] ExtractIPsAndSubnetMasks(
  369. byte[] ipWithSubmask1,
  370. byte[] ipWithSubmask2)
  371. {
  372. int ipLength = ipWithSubmask1.Length / 2;
  373. byte[] ip1 = new byte[ipLength];
  374. byte[] subnetmask1 = new byte[ipLength];
  375. Array.Copy(ipWithSubmask1, 0, ip1, 0, ipLength);
  376. Array.Copy(ipWithSubmask1, ipLength, subnetmask1, 0, ipLength);
  377. byte[] ip2 = new byte[ipLength];
  378. byte[] subnetmask2 = new byte[ipLength];
  379. Array.Copy(ipWithSubmask2, 0, ip2, 0, ipLength);
  380. Array.Copy(ipWithSubmask2, ipLength, subnetmask2, 0, ipLength);
  381. return new byte[][]
  382. {ip1, subnetmask1, ip2, subnetmask2};
  383. }
  384. /**
  385. * Based on the two IP addresses and their subnet masks the IP range is
  386. * computed for each IP address - subnet mask pair and returned as the
  387. * minimum IP address and the maximum address of the range.
  388. *
  389. * @param ip1 The first IP address.
  390. * @param subnetmask1 The subnet mask of the first IP address.
  391. * @param ip2 The second IP address.
  392. * @param subnetmask2 The subnet mask of the second IP address.
  393. * @return A array with two elements. The first/second element contains the
  394. * min and max IP address of the first/second IP address and its
  395. * subnet mask.
  396. */
  397. private byte[][] MinMaxIPs(
  398. byte[] ip1,
  399. byte[] subnetmask1,
  400. byte[] ip2,
  401. byte[] subnetmask2)
  402. {
  403. int ipLength = ip1.Length;
  404. byte[] min1 = new byte[ipLength];
  405. byte[] max1 = new byte[ipLength];
  406. byte[] min2 = new byte[ipLength];
  407. byte[] max2 = new byte[ipLength];
  408. for (int i = 0; i < ipLength; i++)
  409. {
  410. min1[i] = (byte)(ip1[i] & subnetmask1[i]);
  411. max1[i] = (byte)(ip1[i] & subnetmask1[i] | ~subnetmask1[i]);
  412. min2[i] = (byte)(ip2[i] & subnetmask2[i]);
  413. max2[i] = (byte)(ip2[i] & subnetmask2[i] | ~subnetmask2[i]);
  414. }
  415. return new byte[][] { min1, max1, min2, max2 };
  416. }
  417. private void CheckPermittedEmail(ISet permitted, String email)
  418. //throws PkixNameConstraintValidatorException
  419. {
  420. if (permitted == null)
  421. {
  422. return;
  423. }
  424. IEnumerator it = permitted.GetEnumerator();
  425. while (it.MoveNext())
  426. {
  427. String str = ((String)it.Current);
  428. if (EmailIsConstrained(email, str))
  429. {
  430. return;
  431. }
  432. }
  433. if (email.Length == 0 && permitted.Count == 0)
  434. {
  435. return;
  436. }
  437. throw new PkixNameConstraintValidatorException(
  438. "Subject email address is not from a permitted subtree.");
  439. }
  440. private void CheckExcludedEmail(ISet excluded, String email)
  441. //throws PkixNameConstraintValidatorException
  442. {
  443. if (excluded.IsEmpty)
  444. {
  445. return;
  446. }
  447. IEnumerator it = excluded.GetEnumerator();
  448. while (it.MoveNext())
  449. {
  450. String str = (String)it.Current;
  451. if (EmailIsConstrained(email, str))
  452. {
  453. throw new PkixNameConstraintValidatorException(
  454. "Email address is from an excluded subtree.");
  455. }
  456. }
  457. }
  458. /**
  459. * Checks if the IP <code>ip</code> is included in the permitted ISet
  460. * <code>permitted</code>.
  461. *
  462. * @param permitted A <code>Set</code> of permitted IP addresses with
  463. * their subnet mask as byte arrays.
  464. * @param ip The IP address.
  465. * @throws PkixNameConstraintValidatorException
  466. * if the IP is not permitted.
  467. */
  468. private void CheckPermittedIP(ISet permitted, byte[] ip)
  469. //throws PkixNameConstraintValidatorException
  470. {
  471. if (permitted == null)
  472. {
  473. return;
  474. }
  475. IEnumerator it = permitted.GetEnumerator();
  476. while (it.MoveNext())
  477. {
  478. byte[] ipWithSubnet = (byte[])it.Current;
  479. if (IsIPConstrained(ip, ipWithSubnet))
  480. {
  481. return;
  482. }
  483. }
  484. if (ip.Length == 0 && permitted.Count == 0)
  485. {
  486. return;
  487. }
  488. throw new PkixNameConstraintValidatorException(
  489. "IP is not from a permitted subtree.");
  490. }
  491. /**
  492. * Checks if the IP <code>ip</code> is included in the excluded ISet
  493. * <code>excluded</code>.
  494. *
  495. * @param excluded A <code>Set</code> of excluded IP addresses with their
  496. * subnet mask as byte arrays.
  497. * @param ip The IP address.
  498. * @throws PkixNameConstraintValidatorException
  499. * if the IP is excluded.
  500. */
  501. private void checkExcludedIP(ISet excluded, byte[] ip)
  502. //throws PkixNameConstraintValidatorException
  503. {
  504. if (excluded.IsEmpty)
  505. {
  506. return;
  507. }
  508. IEnumerator it = excluded.GetEnumerator();
  509. while (it.MoveNext())
  510. {
  511. byte[] ipWithSubnet = (byte[])it.Current;
  512. if (IsIPConstrained(ip, ipWithSubnet))
  513. {
  514. throw new PkixNameConstraintValidatorException(
  515. "IP is from an excluded subtree.");
  516. }
  517. }
  518. }
  519. /**
  520. * Checks if the IP address <code>ip</code> is constrained by
  521. * <code>constraint</code>.
  522. *
  523. * @param ip The IP address.
  524. * @param constraint The constraint. This is an IP address concatenated with
  525. * its subnetmask.
  526. * @return <code>true</code> if constrained, <code>false</code>
  527. * otherwise.
  528. */
  529. private bool IsIPConstrained(byte[] ip, byte[] constraint)
  530. {
  531. int ipLength = ip.Length;
  532. if (ipLength != (constraint.Length / 2))
  533. {
  534. return false;
  535. }
  536. byte[] subnetMask = new byte[ipLength];
  537. Array.Copy(constraint, ipLength, subnetMask, 0, ipLength);
  538. byte[] permittedSubnetAddress = new byte[ipLength];
  539. byte[] ipSubnetAddress = new byte[ipLength];
  540. // the resulting IP address by applying the subnet mask
  541. for (int i = 0; i < ipLength; i++)
  542. {
  543. permittedSubnetAddress[i] = (byte)(constraint[i] & subnetMask[i]);
  544. ipSubnetAddress[i] = (byte)(ip[i] & subnetMask[i]);
  545. }
  546. return BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Arrays.AreEqual(permittedSubnetAddress, ipSubnetAddress);
  547. }
  548. private bool EmailIsConstrained(String email, String constraint)
  549. {
  550. String sub = email.Substring(email.IndexOf('@') + 1);
  551. // a particular mailbox
  552. if (constraint.IndexOf('@') != -1)
  553. {
  554. if (BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.ToUpperInvariant(email).Equals(BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.ToUpperInvariant(constraint)))
  555. {
  556. return true;
  557. }
  558. }
  559. // on particular host
  560. else if (!(constraint[0].Equals('.')))
  561. {
  562. if (BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.ToUpperInvariant(sub).Equals(BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.ToUpperInvariant(constraint)))
  563. {
  564. return true;
  565. }
  566. }
  567. // address in sub domain
  568. else if (WithinDomain(sub, constraint))
  569. {
  570. return true;
  571. }
  572. return false;
  573. }
  574. private bool WithinDomain(String testDomain, String domain)
  575. {
  576. String tempDomain = domain;
  577. if (BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.StartsWith(tempDomain, "."))
  578. {
  579. tempDomain = tempDomain.Substring(1);
  580. }
  581. String[] domainParts = tempDomain.Split('.'); // Strings.split(tempDomain, '.');
  582. String[] testDomainParts = testDomain.Split('.'); // Strings.split(testDomain, '.');
  583. // must have at least one subdomain
  584. if (testDomainParts.Length <= domainParts.Length)
  585. {
  586. return false;
  587. }
  588. int d = testDomainParts.Length - domainParts.Length;
  589. for (int i = -1; i < domainParts.Length; i++)
  590. {
  591. if (i == -1)
  592. {
  593. if (testDomainParts[i + d].Equals(""))
  594. {
  595. return false;
  596. }
  597. }
  598. else if (!BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.EqualsIgnoreCase(testDomainParts[i + d], domainParts[i]))
  599. {
  600. return false;
  601. }
  602. }
  603. return true;
  604. }
  605. private void CheckPermittedDNS(ISet permitted, String dns)
  606. //throws PkixNameConstraintValidatorException
  607. {
  608. if (permitted == null)
  609. {
  610. return;
  611. }
  612. IEnumerator it = permitted.GetEnumerator();
  613. while (it.MoveNext())
  614. {
  615. String str = ((String)it.Current);
  616. // is sub domain
  617. if (WithinDomain(dns, str)
  618. || BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.ToUpperInvariant(dns).Equals(BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.ToUpperInvariant(str)))
  619. {
  620. return;
  621. }
  622. }
  623. if (dns.Length == 0 && permitted.Count == 0)
  624. {
  625. return;
  626. }
  627. throw new PkixNameConstraintValidatorException(
  628. "DNS is not from a permitted subtree.");
  629. }
  630. private void checkExcludedDNS(ISet excluded, String dns)
  631. // throws PkixNameConstraintValidatorException
  632. {
  633. if (excluded.IsEmpty)
  634. {
  635. return;
  636. }
  637. IEnumerator it = excluded.GetEnumerator();
  638. while (it.MoveNext())
  639. {
  640. String str = ((String)it.Current);
  641. // is sub domain or the same
  642. if (WithinDomain(dns, str) || BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.EqualsIgnoreCase(dns, str))
  643. {
  644. throw new PkixNameConstraintValidatorException(
  645. "DNS is from an excluded subtree.");
  646. }
  647. }
  648. }
  649. /**
  650. * The common part of <code>email1</code> and <code>email2</code> is
  651. * added to the union <code>union</code>. If <code>email1</code> and
  652. * <code>email2</code> have nothing in common they are added both.
  653. *
  654. * @param email1 Email address constraint 1.
  655. * @param email2 Email address constraint 2.
  656. * @param union The union.
  657. */
  658. private void unionEmail(String email1, String email2, ISet union)
  659. {
  660. // email1 is a particular address
  661. if (email1.IndexOf('@') != -1)
  662. {
  663. String _sub = email1.Substring(email1.IndexOf('@') + 1);
  664. // both are a particular mailbox
  665. if (email2.IndexOf('@') != -1)
  666. {
  667. if (BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.EqualsIgnoreCase(email1, email2))
  668. {
  669. union.Add(email1);
  670. }
  671. else
  672. {
  673. union.Add(email1);
  674. union.Add(email2);
  675. }
  676. }
  677. // email2 specifies a domain
  678. else if (BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.StartsWith(email2, "."))
  679. {
  680. if (WithinDomain(_sub, email2))
  681. {
  682. union.Add(email2);
  683. }
  684. else
  685. {
  686. union.Add(email1);
  687. union.Add(email2);
  688. }
  689. }
  690. // email2 specifies a particular host
  691. else
  692. {
  693. if (BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.EqualsIgnoreCase(_sub, email2))
  694. {
  695. union.Add(email2);
  696. }
  697. else
  698. {
  699. union.Add(email1);
  700. union.Add(email2);
  701. }
  702. }
  703. }
  704. // email1 specifies a domain
  705. else if (BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.StartsWith(email1, "."))
  706. {
  707. if (email2.IndexOf('@') != -1)
  708. {
  709. String _sub = email2.Substring(email1.IndexOf('@') + 1);
  710. if (WithinDomain(_sub, email1))
  711. {
  712. union.Add(email1);
  713. }
  714. else
  715. {
  716. union.Add(email1);
  717. union.Add(email2);
  718. }
  719. }
  720. // email2 specifies a domain
  721. else if (BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.StartsWith(email2, "."))
  722. {
  723. if (WithinDomain(email1, email2) || BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.EqualsIgnoreCase(email1, email2))
  724. {
  725. union.Add(email2);
  726. }
  727. else if (WithinDomain(email2, email1))
  728. {
  729. union.Add(email1);
  730. }
  731. else
  732. {
  733. union.Add(email1);
  734. union.Add(email2);
  735. }
  736. }
  737. else
  738. {
  739. if (WithinDomain(email2, email1))
  740. {
  741. union.Add(email1);
  742. }
  743. else
  744. {
  745. union.Add(email1);
  746. union.Add(email2);
  747. }
  748. }
  749. }
  750. // email specifies a host
  751. else
  752. {
  753. if (email2.IndexOf('@') != -1)
  754. {
  755. String _sub = email2.Substring(email1.IndexOf('@') + 1);
  756. if (BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.EqualsIgnoreCase(_sub, email1))
  757. {
  758. union.Add(email1);
  759. }
  760. else
  761. {
  762. union.Add(email1);
  763. union.Add(email2);
  764. }
  765. }
  766. // email2 specifies a domain
  767. else if (BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.StartsWith(email2, "."))
  768. {
  769. if (WithinDomain(email1, email2))
  770. {
  771. union.Add(email2);
  772. }
  773. else
  774. {
  775. union.Add(email1);
  776. union.Add(email2);
  777. }
  778. }
  779. // email2 specifies a particular host
  780. else
  781. {
  782. if (BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.EqualsIgnoreCase(email1, email2))
  783. {
  784. union.Add(email1);
  785. }
  786. else
  787. {
  788. union.Add(email1);
  789. union.Add(email2);
  790. }
  791. }
  792. }
  793. }
  794. private void unionURI(String email1, String email2, ISet union)
  795. {
  796. // email1 is a particular address
  797. if (email1.IndexOf('@') != -1)
  798. {
  799. String _sub = email1.Substring(email1.IndexOf('@') + 1);
  800. // both are a particular mailbox
  801. if (email2.IndexOf('@') != -1)
  802. {
  803. if (BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.EqualsIgnoreCase(email1, email2))
  804. {
  805. union.Add(email1);
  806. }
  807. else
  808. {
  809. union.Add(email1);
  810. union.Add(email2);
  811. }
  812. }
  813. // email2 specifies a domain
  814. else if (BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.StartsWith(email2, "."))
  815. {
  816. if (WithinDomain(_sub, email2))
  817. {
  818. union.Add(email2);
  819. }
  820. else
  821. {
  822. union.Add(email1);
  823. union.Add(email2);
  824. }
  825. }
  826. // email2 specifies a particular host
  827. else
  828. {
  829. if (BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.EqualsIgnoreCase(_sub, email2))
  830. {
  831. union.Add(email2);
  832. }
  833. else
  834. {
  835. union.Add(email1);
  836. union.Add(email2);
  837. }
  838. }
  839. }
  840. // email1 specifies a domain
  841. else if (BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.StartsWith(email1, "."))
  842. {
  843. if (email2.IndexOf('@') != -1)
  844. {
  845. String _sub = email2.Substring(email1.IndexOf('@') + 1);
  846. if (WithinDomain(_sub, email1))
  847. {
  848. union.Add(email1);
  849. }
  850. else
  851. {
  852. union.Add(email1);
  853. union.Add(email2);
  854. }
  855. }
  856. // email2 specifies a domain
  857. else if (BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.StartsWith(email2, "."))
  858. {
  859. if (WithinDomain(email1, email2) || BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.EqualsIgnoreCase(email1, email2))
  860. {
  861. union.Add(email2);
  862. }
  863. else if (WithinDomain(email2, email1))
  864. {
  865. union.Add(email1);
  866. }
  867. else
  868. {
  869. union.Add(email1);
  870. union.Add(email2);
  871. }
  872. }
  873. else
  874. {
  875. if (WithinDomain(email2, email1))
  876. {
  877. union.Add(email1);
  878. }
  879. else
  880. {
  881. union.Add(email1);
  882. union.Add(email2);
  883. }
  884. }
  885. }
  886. // email specifies a host
  887. else
  888. {
  889. if (email2.IndexOf('@') != -1)
  890. {
  891. String _sub = email2.Substring(email1.IndexOf('@') + 1);
  892. if (BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.EqualsIgnoreCase(_sub, email1))
  893. {
  894. union.Add(email1);
  895. }
  896. else
  897. {
  898. union.Add(email1);
  899. union.Add(email2);
  900. }
  901. }
  902. // email2 specifies a domain
  903. else if (BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.StartsWith(email2, "."))
  904. {
  905. if (WithinDomain(email1, email2))
  906. {
  907. union.Add(email2);
  908. }
  909. else
  910. {
  911. union.Add(email1);
  912. union.Add(email2);
  913. }
  914. }
  915. // email2 specifies a particular host
  916. else
  917. {
  918. if (BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.EqualsIgnoreCase(email1, email2))
  919. {
  920. union.Add(email1);
  921. }
  922. else
  923. {
  924. union.Add(email1);
  925. union.Add(email2);
  926. }
  927. }
  928. }
  929. }
  930. private ISet intersectDNS(ISet permitted, ISet dnss)
  931. {
  932. ISet intersect = new HashSet();
  933. for (IEnumerator it = dnss.GetEnumerator(); it.MoveNext(); )
  934. {
  935. String dns = ExtractNameAsString(((GeneralSubtree)it.Current)
  936. .Base);
  937. if (permitted == null)
  938. {
  939. if (dns != null)
  940. {
  941. intersect.Add(dns);
  942. }
  943. }
  944. else
  945. {
  946. IEnumerator _iter = permitted.GetEnumerator();
  947. while (_iter.MoveNext())
  948. {
  949. String _permitted = (String)_iter.Current;
  950. if (WithinDomain(_permitted, dns))
  951. {
  952. intersect.Add(_permitted);
  953. }
  954. else if (WithinDomain(dns, _permitted))
  955. {
  956. intersect.Add(dns);
  957. }
  958. }
  959. }
  960. }
  961. return intersect;
  962. }
  963. protected ISet unionDNS(ISet excluded, String dns)
  964. {
  965. if (excluded.IsEmpty)
  966. {
  967. if (dns == null)
  968. {
  969. return excluded;
  970. }
  971. excluded.Add(dns);
  972. return excluded;
  973. }
  974. else
  975. {
  976. ISet union = new HashSet();
  977. IEnumerator _iter = excluded.GetEnumerator();
  978. while (_iter.MoveNext())
  979. {
  980. String _permitted = (String)_iter.Current;
  981. if (WithinDomain(_permitted, dns))
  982. {
  983. union.Add(dns);
  984. }
  985. else if (WithinDomain(dns, _permitted))
  986. {
  987. union.Add(_permitted);
  988. }
  989. else
  990. {
  991. union.Add(_permitted);
  992. union.Add(dns);
  993. }
  994. }
  995. return union;
  996. }
  997. }
  998. /**
  999. * The most restricting part from <code>email1</code> and
  1000. * <code>email2</code> is added to the intersection <code>intersect</code>.
  1001. *
  1002. * @param email1 Email address constraint 1.
  1003. * @param email2 Email address constraint 2.
  1004. * @param intersect The intersection.
  1005. */
  1006. private void intersectEmail(String email1, String email2, ISet intersect)
  1007. {
  1008. // email1 is a particular address
  1009. if (email1.IndexOf('@') != -1)
  1010. {
  1011. String _sub = email1.Substring(email1.IndexOf('@') + 1);
  1012. // both are a particular mailbox
  1013. if (email2.IndexOf('@') != -1)
  1014. {
  1015. if (BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.EqualsIgnoreCase(email1, email2))
  1016. {
  1017. intersect.Add(email1);
  1018. }
  1019. }
  1020. // email2 specifies a domain
  1021. else if (BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.StartsWith(email2, "."))
  1022. {
  1023. if (WithinDomain(_sub, email2))
  1024. {
  1025. intersect.Add(email1);
  1026. }
  1027. }
  1028. // email2 specifies a particular host
  1029. else
  1030. {
  1031. if (BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.EqualsIgnoreCase(_sub, email2))
  1032. {
  1033. intersect.Add(email1);
  1034. }
  1035. }
  1036. }
  1037. // email specifies a domain
  1038. else if (BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.StartsWith(email1, "."))
  1039. {
  1040. if (email2.IndexOf('@') != -1)
  1041. {
  1042. String _sub = email2.Substring(email1.IndexOf('@') + 1);
  1043. if (WithinDomain(_sub, email1))
  1044. {
  1045. intersect.Add(email2);
  1046. }
  1047. }
  1048. // email2 specifies a domain
  1049. else if (BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.StartsWith(email2, "."))
  1050. {
  1051. if (WithinDomain(email1, email2) || BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.EqualsIgnoreCase(email1, email2))
  1052. {
  1053. intersect.Add(email1);
  1054. }
  1055. else if (WithinDomain(email2, email1))
  1056. {
  1057. intersect.Add(email2);
  1058. }
  1059. }
  1060. else
  1061. {
  1062. if (WithinDomain(email2, email1))
  1063. {
  1064. intersect.Add(email2);
  1065. }
  1066. }
  1067. }
  1068. // email1 specifies a host
  1069. else
  1070. {
  1071. if (email2.IndexOf('@') != -1)
  1072. {
  1073. String _sub = email2.Substring(email2.IndexOf('@') + 1);
  1074. if (BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.EqualsIgnoreCase(_sub, email1))
  1075. {
  1076. intersect.Add(email2);
  1077. }
  1078. }
  1079. // email2 specifies a domain
  1080. else if (BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.StartsWith(email2, "."))
  1081. {
  1082. if (WithinDomain(email1, email2))
  1083. {
  1084. intersect.Add(email1);
  1085. }
  1086. }
  1087. // email2 specifies a particular host
  1088. else
  1089. {
  1090. if (BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.EqualsIgnoreCase(email1, email2))
  1091. {
  1092. intersect.Add(email1);
  1093. }
  1094. }
  1095. }
  1096. }
  1097. private void checkExcludedURI(ISet excluded, String uri)
  1098. // throws PkixNameConstraintValidatorException
  1099. {
  1100. if (excluded.IsEmpty)
  1101. {
  1102. return;
  1103. }
  1104. IEnumerator it = excluded.GetEnumerator();
  1105. while (it.MoveNext())
  1106. {
  1107. String str = ((String)it.Current);
  1108. if (IsUriConstrained(uri, str))
  1109. {
  1110. throw new PkixNameConstraintValidatorException(
  1111. "URI is from an excluded subtree.");
  1112. }
  1113. }
  1114. }
  1115. private ISet intersectURI(ISet permitted, ISet uris)
  1116. {
  1117. ISet intersect = new HashSet();
  1118. for (IEnumerator it = uris.GetEnumerator(); it.MoveNext(); )
  1119. {
  1120. String uri = ExtractNameAsString(((GeneralSubtree)it.Current)
  1121. .Base);
  1122. if (permitted == null)
  1123. {
  1124. if (uri != null)
  1125. {
  1126. intersect.Add(uri);
  1127. }
  1128. }
  1129. else
  1130. {
  1131. IEnumerator _iter = permitted.GetEnumerator();
  1132. while (_iter.MoveNext())
  1133. {
  1134. String _permitted = (String)_iter.Current;
  1135. intersectURI(_permitted, uri, intersect);
  1136. }
  1137. }
  1138. }
  1139. return intersect;
  1140. }
  1141. private ISet unionURI(ISet excluded, String uri)
  1142. {
  1143. if (excluded.IsEmpty)
  1144. {
  1145. if (uri == null)
  1146. {
  1147. return excluded;
  1148. }
  1149. excluded.Add(uri);
  1150. return excluded;
  1151. }
  1152. else
  1153. {
  1154. ISet union = new HashSet();
  1155. IEnumerator _iter = excluded.GetEnumerator();
  1156. while (_iter.MoveNext())
  1157. {
  1158. String _excluded = (String)_iter.Current;
  1159. unionURI(_excluded, uri, union);
  1160. }
  1161. return union;
  1162. }
  1163. }
  1164. private void intersectURI(String email1, String email2, ISet intersect)
  1165. {
  1166. // email1 is a particular address
  1167. if (email1.IndexOf('@') != -1)
  1168. {
  1169. String _sub = email1.Substring(email1.IndexOf('@') + 1);
  1170. // both are a particular mailbox
  1171. if (email2.IndexOf('@') != -1)
  1172. {
  1173. if (BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.EqualsIgnoreCase(email1, email2))
  1174. {
  1175. intersect.Add(email1);
  1176. }
  1177. }
  1178. // email2 specifies a domain
  1179. else if (BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.StartsWith(email2, "."))
  1180. {
  1181. if (WithinDomain(_sub, email2))
  1182. {
  1183. intersect.Add(email1);
  1184. }
  1185. }
  1186. // email2 specifies a particular host
  1187. else
  1188. {
  1189. if (BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.EqualsIgnoreCase(_sub, email2))
  1190. {
  1191. intersect.Add(email1);
  1192. }
  1193. }
  1194. }
  1195. // email specifies a domain
  1196. else if (BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.StartsWith(email1, "."))
  1197. {
  1198. if (email2.IndexOf('@') != -1)
  1199. {
  1200. String _sub = email2.Substring(email1.IndexOf('@') + 1);
  1201. if (WithinDomain(_sub, email1))
  1202. {
  1203. intersect.Add(email2);
  1204. }
  1205. }
  1206. // email2 specifies a domain
  1207. else if (BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.StartsWith(email2, "."))
  1208. {
  1209. if (WithinDomain(email1, email2) || BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.EqualsIgnoreCase(email1, email2))
  1210. {
  1211. intersect.Add(email1);
  1212. }
  1213. else if (WithinDomain(email2, email1))
  1214. {
  1215. intersect.Add(email2);
  1216. }
  1217. }
  1218. else
  1219. {
  1220. if (WithinDomain(email2, email1))
  1221. {
  1222. intersect.Add(email2);
  1223. }
  1224. }
  1225. }
  1226. // email1 specifies a host
  1227. else
  1228. {
  1229. if (email2.IndexOf('@') != -1)
  1230. {
  1231. String _sub = email2.Substring(email2.IndexOf('@') + 1);
  1232. if (BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.EqualsIgnoreCase(_sub, email1))
  1233. {
  1234. intersect.Add(email2);
  1235. }
  1236. }
  1237. // email2 specifies a domain
  1238. else if (BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.StartsWith(email2, "."))
  1239. {
  1240. if (WithinDomain(email1, email2))
  1241. {
  1242. intersect.Add(email1);
  1243. }
  1244. }
  1245. // email2 specifies a particular host
  1246. else
  1247. {
  1248. if (BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.EqualsIgnoreCase(email1, email2))
  1249. {
  1250. intersect.Add(email1);
  1251. }
  1252. }
  1253. }
  1254. }
  1255. private void CheckPermittedURI(ISet permitted, String uri)
  1256. // throws PkixNameConstraintValidatorException
  1257. {
  1258. if (permitted == null)
  1259. {
  1260. return;
  1261. }
  1262. IEnumerator it = permitted.GetEnumerator();
  1263. while (it.MoveNext())
  1264. {
  1265. String str = ((String)it.Current);
  1266. if (IsUriConstrained(uri, str))
  1267. {
  1268. return;
  1269. }
  1270. }
  1271. if (uri.Length == 0 && permitted.Count == 0)
  1272. {
  1273. return;
  1274. }
  1275. throw new PkixNameConstraintValidatorException(
  1276. "URI is not from a permitted subtree.");
  1277. }
  1278. private bool IsUriConstrained(String uri, String constraint)
  1279. {
  1280. String host = ExtractHostFromURL(uri);
  1281. // a host
  1282. if (!BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.StartsWith(constraint, "."))
  1283. {
  1284. if (BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.EqualsIgnoreCase(host, constraint))
  1285. {
  1286. return true;
  1287. }
  1288. }
  1289. // in sub domain or domain
  1290. else if (WithinDomain(host, constraint))
  1291. {
  1292. return true;
  1293. }
  1294. return false;
  1295. }
  1296. private static String ExtractHostFromURL(String url)
  1297. {
  1298. // see RFC 1738
  1299. // remove ':' after protocol, e.g. http:
  1300. String sub = url.Substring(url.IndexOf(':') + 1);
  1301. // extract host from Common Internet Scheme Syntax, e.g. http://
  1302. int idxOfSlashes = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.IndexOf(sub, "//");
  1303. if (idxOfSlashes != -1)
  1304. {
  1305. sub = sub.Substring(idxOfSlashes + 2);
  1306. }
  1307. // first remove port, e.g. http://test.com:21
  1308. if (sub.LastIndexOf(':') != -1)
  1309. {
  1310. sub = sub.Substring(0, sub.LastIndexOf(':'));
  1311. }
  1312. // remove user and password, e.g. http://john:password@test.com
  1313. sub = sub.Substring(sub.IndexOf(':') + 1);
  1314. sub = sub.Substring(sub.IndexOf('@') + 1);
  1315. // remove local parts, e.g. http://test.com/bla
  1316. if (sub.IndexOf('/') != -1)
  1317. {
  1318. sub = sub.Substring(0, sub.IndexOf('/'));
  1319. }
  1320. return sub;
  1321. }
  1322. /**
  1323. * Checks if the given GeneralName is in the permitted ISet.
  1324. *
  1325. * @param name The GeneralName
  1326. * @throws PkixNameConstraintValidatorException
  1327. * If the <code>name</code>
  1328. */
  1329. public void checkPermitted(GeneralName name)
  1330. // throws PkixNameConstraintValidatorException
  1331. {
  1332. switch (name.TagNo)
  1333. {
  1334. case 1:
  1335. CheckPermittedEmail(permittedSubtreesEmail,
  1336. ExtractNameAsString(name));
  1337. break;
  1338. case 2:
  1339. CheckPermittedDNS(permittedSubtreesDNS, DerIA5String.GetInstance(
  1340. name.Name).GetString());
  1341. break;
  1342. case 4:
  1343. CheckPermittedDN(Asn1Sequence.GetInstance(name.Name.ToAsn1Object()));
  1344. break;
  1345. case 6:
  1346. CheckPermittedURI(permittedSubtreesURI, DerIA5String.GetInstance(
  1347. name.Name).GetString());
  1348. break;
  1349. case 7:
  1350. byte[] ip = Asn1OctetString.GetInstance(name.Name).GetOctets();
  1351. CheckPermittedIP(permittedSubtreesIP, ip);
  1352. break;
  1353. }
  1354. }
  1355. /**
  1356. * Check if the given GeneralName is contained in the excluded ISet.
  1357. *
  1358. * @param name The GeneralName.
  1359. * @throws PkixNameConstraintValidatorException
  1360. * If the <code>name</code> is
  1361. * excluded.
  1362. */
  1363. public void checkExcluded(GeneralName name)
  1364. // throws PkixNameConstraintValidatorException
  1365. {
  1366. switch (name.TagNo)
  1367. {
  1368. case 1:
  1369. CheckExcludedEmail(excludedSubtreesEmail, ExtractNameAsString(name));
  1370. break;
  1371. case 2:
  1372. checkExcludedDNS(excludedSubtreesDNS, DerIA5String.GetInstance(
  1373. name.Name).GetString());
  1374. break;
  1375. case 4:
  1376. CheckExcludedDN(Asn1Sequence.GetInstance(name.Name.ToAsn1Object()));
  1377. break;
  1378. case 6:
  1379. checkExcludedURI(excludedSubtreesURI, DerIA5String.GetInstance(
  1380. name.Name).GetString());
  1381. break;
  1382. case 7:
  1383. byte[] ip = Asn1OctetString.GetInstance(name.Name).GetOctets();
  1384. checkExcludedIP(excludedSubtreesIP, ip);
  1385. break;
  1386. }
  1387. }
  1388. /**
  1389. * Updates the permitted ISet of these name constraints with the intersection
  1390. * with the given subtree.
  1391. *
  1392. * @param permitted The permitted subtrees
  1393. */
  1394. public void IntersectPermittedSubtree(Asn1Sequence permitted)
  1395. {
  1396. IDictionary subtreesMap = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateHashtable();
  1397. // group in ISets in a map ordered by tag no.
  1398. for (IEnumerator e = permitted.GetEnumerator(); e.MoveNext(); )
  1399. {
  1400. GeneralSubtree subtree = GeneralSubtree.GetInstance(e.Current);
  1401. int tagNo = subtree.Base.TagNo;
  1402. if (subtreesMap[tagNo] == null)
  1403. {
  1404. subtreesMap[tagNo] = new HashSet();
  1405. }
  1406. ((ISet)subtreesMap[tagNo]).Add(subtree);
  1407. }
  1408. for (IEnumerator it = subtreesMap.GetEnumerator(); it.MoveNext(); )
  1409. {
  1410. DictionaryEntry entry = (DictionaryEntry)it.Current;
  1411. // go through all subtree groups
  1412. switch ((int)entry.Key )
  1413. {
  1414. case 1:
  1415. permittedSubtreesEmail = IntersectEmail(permittedSubtreesEmail,
  1416. (ISet)entry.Value);
  1417. break;
  1418. case 2:
  1419. permittedSubtreesDNS = intersectDNS(permittedSubtreesDNS,
  1420. (ISet)entry.Value);
  1421. break;
  1422. case 4:
  1423. permittedSubtreesDN = IntersectDN(permittedSubtreesDN,
  1424. (ISet)entry.Value);
  1425. break;
  1426. case 6:
  1427. permittedSubtreesURI = intersectURI(permittedSubtreesURI,
  1428. (ISet)entry.Value);
  1429. break;
  1430. case 7:
  1431. permittedSubtreesIP = IntersectIP(permittedSubtreesIP,
  1432. (ISet)entry.Value);
  1433. break;
  1434. }
  1435. }
  1436. }
  1437. private String ExtractNameAsString(GeneralName name)
  1438. {
  1439. return DerIA5String.GetInstance(name.Name).GetString();
  1440. }
  1441. public void IntersectEmptyPermittedSubtree(int nameType)
  1442. {
  1443. switch (nameType)
  1444. {
  1445. case 1:
  1446. permittedSubtreesEmail = new HashSet();
  1447. break;
  1448. case 2:
  1449. permittedSubtreesDNS = new HashSet();
  1450. break;
  1451. case 4:
  1452. permittedSubtreesDN = new HashSet();
  1453. break;
  1454. case 6:
  1455. permittedSubtreesURI = new HashSet();
  1456. break;
  1457. case 7:
  1458. permittedSubtreesIP = new HashSet();
  1459. break;
  1460. }
  1461. }
  1462. /**
  1463. * Adds a subtree to the excluded ISet of these name constraints.
  1464. *
  1465. * @param subtree A subtree with an excluded GeneralName.
  1466. */
  1467. public void AddExcludedSubtree(GeneralSubtree subtree)
  1468. {
  1469. GeneralName subTreeBase = subtree.Base;
  1470. switch (subTreeBase.TagNo)
  1471. {
  1472. case 1:
  1473. excludedSubtreesEmail = UnionEmail(excludedSubtreesEmail,
  1474. ExtractNameAsString(subTreeBase));
  1475. break;
  1476. case 2:
  1477. excludedSubtreesDNS = unionDNS(excludedSubtreesDNS,
  1478. ExtractNameAsString(subTreeBase));
  1479. break;
  1480. case 4:
  1481. excludedSubtreesDN = UnionDN(excludedSubtreesDN,
  1482. (Asn1Sequence)subTreeBase.Name.ToAsn1Object());
  1483. break;
  1484. case 6:
  1485. excludedSubtreesURI = unionURI(excludedSubtreesURI,
  1486. ExtractNameAsString(subTreeBase));
  1487. break;
  1488. case 7:
  1489. excludedSubtreesIP = UnionIP(excludedSubtreesIP, Asn1OctetString
  1490. .GetInstance(subTreeBase.Name).GetOctets());
  1491. break;
  1492. }
  1493. }
  1494. /**
  1495. * Returns the maximum IP address.
  1496. *
  1497. * @param ip1 The first IP address.
  1498. * @param ip2 The second IP address.
  1499. * @return The maximum IP address.
  1500. */
  1501. private static byte[] Max(byte[] ip1, byte[] ip2)
  1502. {
  1503. for (int i = 0; i < ip1.Length; i++)
  1504. {
  1505. if ((ip1[i] & 0xFFFF) > (ip2[i] & 0xFFFF))
  1506. {
  1507. return ip1;
  1508. }
  1509. }
  1510. return ip2;
  1511. }
  1512. /**
  1513. * Returns the minimum IP address.
  1514. *
  1515. * @param ip1 The first IP address.
  1516. * @param ip2 The second IP address.
  1517. * @return The minimum IP address.
  1518. */
  1519. private static byte[] Min(byte[] ip1, byte[] ip2)
  1520. {
  1521. for (int i = 0; i < ip1.Length; i++)
  1522. {
  1523. if ((ip1[i] & 0xFFFF) < (ip2[i] & 0xFFFF))
  1524. {
  1525. return ip1;
  1526. }
  1527. }
  1528. return ip2;
  1529. }
  1530. /**
  1531. * Compares IP address <code>ip1</code> with <code>ip2</code>. If ip1
  1532. * is equal to ip2 0 is returned. If ip1 is bigger 1 is returned, -1
  1533. * otherwise.
  1534. *
  1535. * @param ip1 The first IP address.
  1536. * @param ip2 The second IP address.
  1537. * @return 0 if ip1 is equal to ip2, 1 if ip1 is bigger, -1 otherwise.
  1538. */
  1539. private static int CompareTo(byte[] ip1, byte[] ip2)
  1540. {
  1541. if (BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Arrays.AreEqual(ip1, ip2))
  1542. {
  1543. return 0;
  1544. }
  1545. if (BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Arrays.AreEqual(Max(ip1, ip2), ip1))
  1546. {
  1547. return 1;
  1548. }
  1549. return -1;
  1550. }
  1551. /**
  1552. * Returns the logical OR of the IP addresses <code>ip1</code> and
  1553. * <code>ip2</code>.
  1554. *
  1555. * @param ip1 The first IP address.
  1556. * @param ip2 The second IP address.
  1557. * @return The OR of <code>ip1</code> and <code>ip2</code>.
  1558. */
  1559. private static byte[] Or(byte[] ip1, byte[] ip2)
  1560. {
  1561. byte[] temp = new byte[ip1.Length];
  1562. for (int i = 0; i < ip1.Length; i++)
  1563. {
  1564. temp[i] = (byte)(ip1[i] | ip2[i]);
  1565. }
  1566. return temp;
  1567. }
  1568. [Obsolete("Use GetHashCode instead")]
  1569. public int HashCode()
  1570. {
  1571. return GetHashCode();
  1572. }
  1573. public override int GetHashCode()
  1574. {
  1575. return HashCollection(excludedSubtreesDN)
  1576. + HashCollection(excludedSubtreesDNS)
  1577. + HashCollection(excludedSubtreesEmail)
  1578. + HashCollection(excludedSubtreesIP)
  1579. + HashCollection(excludedSubtreesURI)
  1580. + HashCollection(permittedSubtreesDN)
  1581. + HashCollection(permittedSubtreesDNS)
  1582. + HashCollection(permittedSubtreesEmail)
  1583. + HashCollection(permittedSubtreesIP)
  1584. + HashCollection(permittedSubtreesURI);
  1585. }
  1586. private int HashCollection(ICollection coll)
  1587. {
  1588. if (coll == null)
  1589. {
  1590. return 0;
  1591. }
  1592. int hash = 0;
  1593. IEnumerator it1 = coll.GetEnumerator();
  1594. while (it1.MoveNext())
  1595. {
  1596. Object o = it1.Current;
  1597. if (o is byte[])
  1598. {
  1599. hash += BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Arrays.GetHashCode((byte[])o);
  1600. }
  1601. else
  1602. {
  1603. hash += o.GetHashCode();
  1604. }
  1605. }
  1606. return hash;
  1607. }
  1608. public override bool Equals(Object o)
  1609. {
  1610. if (!(o is PkixNameConstraintValidator))
  1611. return false;
  1612. PkixNameConstraintValidator constraintValidator = (PkixNameConstraintValidator)o;
  1613. return CollectionsAreEqual(constraintValidator.excludedSubtreesDN, excludedSubtreesDN)
  1614. && CollectionsAreEqual(constraintValidator.excludedSubtreesDNS, excludedSubtreesDNS)
  1615. && CollectionsAreEqual(constraintValidator.excludedSubtreesEmail, excludedSubtreesEmail)
  1616. && CollectionsAreEqual(constraintValidator.excludedSubtreesIP, excludedSubtreesIP)
  1617. && CollectionsAreEqual(constraintValidator.excludedSubtreesURI, excludedSubtreesURI)
  1618. && CollectionsAreEqual(constraintValidator.permittedSubtreesDN, permittedSubtreesDN)
  1619. && CollectionsAreEqual(constraintValidator.permittedSubtreesDNS, permittedSubtreesDNS)
  1620. && CollectionsAreEqual(constraintValidator.permittedSubtreesEmail, permittedSubtreesEmail)
  1621. && CollectionsAreEqual(constraintValidator.permittedSubtreesIP, permittedSubtreesIP)
  1622. && CollectionsAreEqual(constraintValidator.permittedSubtreesURI, permittedSubtreesURI);
  1623. }
  1624. private bool CollectionsAreEqual(ICollection coll1, ICollection coll2)
  1625. {
  1626. if (coll1 == coll2)
  1627. {
  1628. return true;
  1629. }
  1630. if (coll1 == null || coll2 == null)
  1631. {
  1632. return false;
  1633. }
  1634. if (coll1.Count != coll2.Count)
  1635. {
  1636. return false;
  1637. }
  1638. IEnumerator it1 = coll1.GetEnumerator();
  1639. while (it1.MoveNext())
  1640. {
  1641. Object a = it1.Current;
  1642. IEnumerator it2 = coll2.GetEnumerator();
  1643. bool found = false;
  1644. while (it2.MoveNext())
  1645. {
  1646. Object b = it2.Current;
  1647. if (SpecialEquals(a, b))
  1648. {
  1649. found = true;
  1650. break;
  1651. }
  1652. }
  1653. if (!found)
  1654. {
  1655. return false;
  1656. }
  1657. }
  1658. return true;
  1659. }
  1660. private bool SpecialEquals(Object o1, Object o2)
  1661. {
  1662. if (o1 == o2)
  1663. {
  1664. return true;
  1665. }
  1666. if (o1 == null || o2 == null)
  1667. {
  1668. return false;
  1669. }
  1670. if ((o1 is byte[]) && (o2 is byte[]))
  1671. {
  1672. return BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Arrays.AreEqual((byte[])o1, (byte[])o2);
  1673. }
  1674. else
  1675. {
  1676. return o1.Equals(o2);
  1677. }
  1678. }
  1679. /**
  1680. * Stringifies an IPv4 or v6 address with subnet mask.
  1681. *
  1682. * @param ip The IP with subnet mask.
  1683. * @return The stringified IP address.
  1684. */
  1685. private String StringifyIP(byte[] ip)
  1686. {
  1687. String temp = "";
  1688. for (int i = 0; i < ip.Length / 2; i++)
  1689. {
  1690. //temp += Integer.toString(ip[i] & 0x00FF) + ".";
  1691. temp += (ip[i] & 0x00FF) + ".";
  1692. }
  1693. temp = temp.Substring(0, temp.Length - 1);
  1694. temp += "/";
  1695. for (int i = ip.Length / 2; i < ip.Length; i++)
  1696. {
  1697. //temp += Integer.toString(ip[i] & 0x00FF) + ".";
  1698. temp += (ip[i] & 0x00FF) + ".";
  1699. }
  1700. temp = temp.Substring(0, temp.Length - 1);
  1701. return temp;
  1702. }
  1703. private String StringifyIPCollection(ISet ips)
  1704. {
  1705. String temp = "";
  1706. temp += "[";
  1707. for (IEnumerator it = ips.GetEnumerator(); it.MoveNext(); )
  1708. {
  1709. temp += StringifyIP((byte[])it.Current) + ",";
  1710. }
  1711. if (temp.Length > 1)
  1712. {
  1713. temp = temp.Substring(0, temp.Length - 1);
  1714. }
  1715. temp += "]";
  1716. return temp;
  1717. }
  1718. public override String ToString()
  1719. {
  1720. String temp = "";
  1721. temp += "permitted:\n";
  1722. if (permittedSubtreesDN != null)
  1723. {
  1724. temp += "DN:\n";
  1725. temp += permittedSubtreesDN.ToString() + "\n";
  1726. }
  1727. if (permittedSubtreesDNS != null)
  1728. {
  1729. temp += "DNS:\n";
  1730. temp += permittedSubtreesDNS.ToString() + "\n";
  1731. }
  1732. if (permittedSubtreesEmail != null)
  1733. {
  1734. temp += "Email:\n";
  1735. temp += permittedSubtreesEmail.ToString() + "\n";
  1736. }
  1737. if (permittedSubtreesURI != null)
  1738. {
  1739. temp += "URI:\n";
  1740. temp += permittedSubtreesURI.ToString() + "\n";
  1741. }
  1742. if (permittedSubtreesIP != null)
  1743. {
  1744. temp += "IP:\n";
  1745. temp += StringifyIPCollection(permittedSubtreesIP) + "\n";
  1746. }
  1747. temp += "excluded:\n";
  1748. if (!(excludedSubtreesDN.IsEmpty))
  1749. {
  1750. temp += "DN:\n";
  1751. temp += excludedSubtreesDN.ToString() + "\n";
  1752. }
  1753. if (!excludedSubtreesDNS.IsEmpty)
  1754. {
  1755. temp += "DNS:\n";
  1756. temp += excludedSubtreesDNS.ToString() + "\n";
  1757. }
  1758. if (!excludedSubtreesEmail.IsEmpty)
  1759. {
  1760. temp += "Email:\n";
  1761. temp += excludedSubtreesEmail.ToString() + "\n";
  1762. }
  1763. if (!excludedSubtreesURI.IsEmpty)
  1764. {
  1765. temp += "URI:\n";
  1766. temp += excludedSubtreesURI.ToString() + "\n";
  1767. }
  1768. if (!excludedSubtreesIP.IsEmpty)
  1769. {
  1770. temp += "IP:\n";
  1771. temp += StringifyIPCollection(excludedSubtreesIP) + "\n";
  1772. }
  1773. return temp;
  1774. }
  1775. }
  1776. }
  1777. #pragma warning restore
  1778. #endif