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