constraints.html 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. <html>
  2. <title>
  3. PyASN1 subtype constraints
  4. </title>
  5. <head>
  6. </head>
  7. <body>
  8. <center>
  9. <table width=60%>
  10. <tr>
  11. <td>
  12. <h4>
  13. 1.4 PyASN1 subtype constraints
  14. </h4>
  15. <p>
  16. Most ASN.1 types can correspond to an infinite set of values. To adapt to
  17. particular application's data model and needs, ASN.1 provides a mechanism
  18. for limiting the infinite set to values, that make sense in particular case.
  19. </p>
  20. <p>
  21. Imposing value constraints on an ASN.1 type can also be seen as creating
  22. a subtype from its base type.
  23. </p>
  24. <p>
  25. In pyasn1, constraints take shape of immutable objects capable
  26. of evaluating given value against constraint-specific requirements.
  27. Constraint object is a property of pyasn1 type. Like TagSet property,
  28. associated with every pyasn1 type, constraints can never be modified
  29. in place. The only way to modify pyasn1 type constraint is to associate
  30. new constraint object to a new pyasn1 type object.
  31. </p>
  32. <p>
  33. A handful of different flavors of <i>constraints</i> are defined in ASN.1.
  34. We will discuss them one by one in the following chapters and also explain
  35. how to combine and apply them to types.
  36. </p>
  37. <a name="1.4.1"></a>
  38. <h4>
  39. 1.4.1 Single value constraint
  40. </h4>
  41. <p>
  42. This kind of constraint allows for limiting type to a finite, specified set
  43. of values.
  44. </p>
  45. <table bgcolor="lightgray" border=0 width=100%><TR><TD>
  46. <pre>
  47. DialButton ::= OCTET STRING (
  48. "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"
  49. )
  50. </pre>
  51. </td></tr></table>
  52. <p>
  53. Its pyasn1 implementation would look like:
  54. </p>
  55. <table bgcolor="lightgray" border=0 width=100%><TR><TD>
  56. <pre>
  57. >>> from pyasn1.type import constraint
  58. >>> c = constraint.SingleValueConstraint(
  59. '0','1','2','3','4','5','6','7','8','9'
  60. )
  61. >>> c
  62. SingleValueConstraint(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
  63. >>> c('0')
  64. >>> c('A')
  65. Traceback (most recent call last):
  66. ...
  67. pyasn1.type.error.ValueConstraintError:
  68. SingleValueConstraint(0, 1, 2, 3, 4, 5, 6, 7, 8, 9) failed at: A
  69. >>>
  70. </pre>
  71. </td></tr></table>
  72. <p>
  73. As can be seen in the snippet above, if a value violates the constraint, an
  74. exception will be thrown. A constrainted pyasn1 type object holds a
  75. reference to a constraint object (or their combination, as will be explained
  76. later) and calls it for value verification.
  77. </p>
  78. <table bgcolor="lightgray" border=0 width=100%><TR><TD>
  79. <pre>
  80. >>> from pyasn1.type import univ, constraint
  81. >>> class DialButton(univ.OctetString):
  82. ... subtypeSpec = constraint.SingleValueConstraint(
  83. ... '0','1','2','3','4','5','6','7','8','9'
  84. ... )
  85. >>> DialButton('0')
  86. DialButton(b'0')
  87. >>> DialButton('A')
  88. Traceback (most recent call last):
  89. ...
  90. pyasn1.type.error.ValueConstraintError:
  91. SingleValueConstraint(0, 1, 2, 3, 4, 5, 6, 7, 8, 9) failed at: A
  92. >>>
  93. </pre>
  94. </td></tr></table>
  95. <p>
  96. Constrained pyasn1 value object can never hold a violating value.
  97. </p>
  98. <a name="1.4.2"></a>
  99. <h4>
  100. 1.4.2 Value range constraint
  101. </h4>
  102. <p>
  103. A pair of values, compliant to a type to be constrained, denote low and upper
  104. bounds of allowed range of values of a type.
  105. </p>
  106. <table bgcolor="lightgray" border=0 width=100%><TR><TD>
  107. <pre>
  108. Teenagers ::= INTEGER (13..19)
  109. </pre>
  110. </td></tr></table>
  111. <p>
  112. And in pyasn1 terms:
  113. </p>
  114. <table bgcolor="lightgray" border=0 width=100%><TR><TD>
  115. <pre>
  116. >>> from pyasn1.type import univ, constraint
  117. >>> class Teenagers(univ.Integer):
  118. ... subtypeSpec = constraint.ValueRangeConstraint(13, 19)
  119. >>> Teenagers(14)
  120. Teenagers(14)
  121. >>> Teenagers(20)
  122. Traceback (most recent call last):
  123. ...
  124. pyasn1.type.error.ValueConstraintError:
  125. ValueRangeConstraint(13, 19) failed at: 20
  126. >>>
  127. </pre>
  128. </td></tr></table>
  129. <p>
  130. Value range constraint usually applies numeric types.
  131. </p>
  132. <a name="1.4.3"></a>
  133. <h4>
  134. 1.4.3 Size constraint
  135. </h4>
  136. <p>
  137. It is sometimes convenient to set or limit the allowed size of a data item
  138. to be sent from one application to another to manage bandwidth and memory
  139. consumption issues. Size constraint specifies the lower and upper bounds
  140. of the size of a valid value.
  141. </p>
  142. <table bgcolor="lightgray" border=0 width=100%><TR><TD>
  143. <pre>
  144. TwoBits ::= BIT STRING (SIZE (2))
  145. </pre>
  146. </td></tr></table>
  147. <p>
  148. Express the same grammar in pyasn1:
  149. </p>
  150. <table bgcolor="lightgray" border=0 width=100%><TR><TD>
  151. <pre>
  152. >>> from pyasn1.type import univ, constraint
  153. >>> class TwoBits(univ.BitString):
  154. ... subtypeSpec = constraint.ValueSizeConstraint(2, 2)
  155. >>> TwoBits((1,1))
  156. TwoBits("'11'B")
  157. >>> TwoBits((1,1,0))
  158. Traceback (most recent call last):
  159. ...
  160. pyasn1.type.error.ValueConstraintError:
  161. ValueSizeConstraint(2, 2) failed at: (1, 1, 0)
  162. >>>
  163. </pre>
  164. </td></tr></table>
  165. <p>
  166. Size constraint can be applied to potentially massive values - bit or octet
  167. strings, SEQUENCE OF/SET OF values.
  168. </p>
  169. <a name="1.4.4"></a>
  170. <h4>
  171. 1.4.4 Alphabet constraint
  172. </h4>
  173. <p>
  174. The permitted alphabet constraint is similar to Single value constraint
  175. but constraint applies to individual characters of a value.
  176. </p>
  177. <table bgcolor="lightgray" border=0 width=100%><TR><TD>
  178. <pre>
  179. MorseCode ::= PrintableString (FROM ("."|"-"|" "))
  180. </pre>
  181. </td></tr></table>
  182. <p>
  183. And in pyasn1:
  184. </p>
  185. <table bgcolor="lightgray" border=0 width=100%><TR><TD>
  186. <pre>
  187. >>> from pyasn1.type import char, constraint
  188. >>> class MorseCode(char.PrintableString):
  189. ... subtypeSpec = constraint.PermittedAlphabetConstraint(".", "-", " ")
  190. >>> MorseCode("...---...")
  191. MorseCode('...---...')
  192. >>> MorseCode("?")
  193. Traceback (most recent call last):
  194. ...
  195. pyasn1.type.error.ValueConstraintError:
  196. PermittedAlphabetConstraint(".", "-", " ") failed at: "?"
  197. >>>
  198. </pre>
  199. </td></tr></table>
  200. <p>
  201. Current implementation does not handle ranges of characters in constraint
  202. (FROM "A".."Z" syntax), one has to list the whole set in a range.
  203. </p>
  204. <a name="1.4.5"></a>
  205. <h4>
  206. 1.4.5 Constraint combinations
  207. </h4>
  208. <p>
  209. Up to this moment, we used a single constraint per ASN.1 type. The standard,
  210. however, allows for combining multiple individual constraints into
  211. intersections, unions and exclusions.
  212. </p>
  213. <p>
  214. In pyasn1 data model, all of these methods of constraint combinations are
  215. implemented as constraint-like objects holding individual constraint (or
  216. combination) objects. Like terminal constraint objects, combination objects
  217. are capable to perform value verification at its set of enclosed constraints
  218. according to the logic of particular combination.
  219. </p>
  220. <p>
  221. Constraints intersection verification succeeds only if a value is
  222. compliant to each constraint in a set. To begin with, the following
  223. specification will constitute a valid telephone number:
  224. </p>
  225. <table bgcolor="lightgray" border=0 width=100%><TR><TD>
  226. <pre>
  227. PhoneNumber ::= NumericString (FROM ("0".."9")) (SIZE 11)
  228. </pre>
  229. </td></tr></table>
  230. <p>
  231. Constraint intersection object serves the logic above:
  232. </p>
  233. <table bgcolor="lightgray" border=0 width=100%><TR><TD>
  234. <pre>
  235. >>> from pyasn1.type import char, constraint
  236. >>> class PhoneNumber(char.NumericString):
  237. ... subtypeSpec = constraint.ConstraintsIntersection(
  238. ... constraint.PermittedAlphabetConstraint('0','1','2','3','4','5','6','7','8','9'),
  239. ... constraint.ValueSizeConstraint(11, 11)
  240. ... )
  241. >>> PhoneNumber('79039343212')
  242. PhoneNumber('79039343212')
  243. >>> PhoneNumber('?9039343212')
  244. Traceback (most recent call last):
  245. ...
  246. pyasn1.type.error.ValueConstraintError:
  247. ConstraintsIntersection(
  248. PermittedAlphabetConstraint('0','1','2','3','4','5','6','7','8','9'),
  249. ValueSizeConstraint(11, 11)) failed at:
  250. PermittedAlphabetConstraint('0','1','2','3','4','5','6','7','8','9') failed at: "?039343212"
  251. >>> PhoneNumber('9343212')
  252. Traceback (most recent call last):
  253. ...
  254. pyasn1.type.error.ValueConstraintError:
  255. ConstraintsIntersection(
  256. PermittedAlphabetConstraint('0','1','2','3','4','5','6','7','8','9'),
  257. ValueSizeConstraint(11, 11)) failed at:
  258. ValueSizeConstraint(10, 10) failed at: "9343212"
  259. >>>
  260. </pre>
  261. </td></tr></table>
  262. <p>
  263. Union of constraints works by making sure that a value is compliant
  264. to any of the constraint in a set. For instance:
  265. </p>
  266. <table bgcolor="lightgray" border=0 width=100%><TR><TD>
  267. <pre>
  268. CapitalOrSmall ::= IA5String (FROM ('A','B','C') | FROM ('a','b','c'))
  269. </pre>
  270. </td></tr></table>
  271. <p>
  272. It's important to note, that a value must fully comply to any single
  273. constraint in a set. In the specification above, a value of all small or
  274. all capital letters is compliant, but a mix of small&capitals is not.
  275. Here's its pyasn1 analogue:
  276. </p>
  277. <table bgcolor="lightgray" border=0 width=100%><TR><TD>
  278. <pre>
  279. >>> from pyasn1.type import char, constraint
  280. >>> class CapitalOrSmall(char.IA5String):
  281. ... subtypeSpec = constraint.ConstraintsUnion(
  282. ... constraint.PermittedAlphabetConstraint('A','B','C'),
  283. ... constraint.PermittedAlphabetConstraint('a','b','c')
  284. ... )
  285. >>> CapitalOrSmall('ABBA')
  286. CapitalOrSmall('ABBA')
  287. >>> CapitalOrSmall('abba')
  288. CapitalOrSmall('abba')
  289. >>> CapitalOrSmall('Abba')
  290. Traceback (most recent call last):
  291. ...
  292. pyasn1.type.error.ValueConstraintError:
  293. ConstraintsUnion(PermittedAlphabetConstraint('A', 'B', 'C'),
  294. PermittedAlphabetConstraint('a', 'b', 'c')) failed at: failed for "Abba"
  295. >>>
  296. </pre>
  297. </td></tr></table>
  298. <p>
  299. Finally, the exclusion constraint simply negates the logic of value
  300. verification at a constraint. In the following example, any integer value
  301. is allowed in a type but not zero.
  302. </p>
  303. <table bgcolor="lightgray" border=0 width=100%><TR><TD>
  304. <pre>
  305. NoZero ::= INTEGER (ALL EXCEPT 0)
  306. </pre>
  307. </td></tr></table>
  308. <p>
  309. In pyasn1 the above definition would read:
  310. </p>
  311. <table bgcolor="lightgray" border=0 width=100%><TR><TD>
  312. <pre>
  313. >>> from pyasn1.type import univ, constraint
  314. >>> class NoZero(univ.Integer):
  315. ... subtypeSpec = constraint.ConstraintsExclusion(
  316. ... constraint.SingleValueConstraint(0)
  317. ... )
  318. >>> NoZero(1)
  319. NoZero(1)
  320. >>> NoZero(0)
  321. Traceback (most recent call last):
  322. ...
  323. pyasn1.type.error.ValueConstraintError:
  324. ConstraintsExclusion(SingleValueConstraint(0)) failed at: 0
  325. >>>
  326. </pre>
  327. </td></tr></table>
  328. <p>
  329. The depth of such a constraints tree, built with constraint combination objects
  330. at its nodes, has not explicit limit. Value verification is performed in a
  331. recursive manner till a definite solution is found.
  332. </p>
  333. <a name="1.5"></a>
  334. <h4>
  335. 1.5 Types relationships
  336. </h4>
  337. <p>
  338. In the course of data processing in an application, it is sometimes
  339. convenient to figure out the type relationships between pyasn1 type or
  340. value objects. Formally, two things influence pyasn1 types relationship:
  341. <i>tag set</i> and <i>subtype constraints</i>. One pyasn1 type is considered
  342. to be a derivative of another if their TagSet and Constraint objects are
  343. a derivation of one another.
  344. </p>
  345. <p>
  346. The following example illustrates the concept (we use the same tagset but
  347. different constraints for simplicity):
  348. </p>
  349. <table bgcolor="lightgray" border=0 width=100%><TR><TD>
  350. <pre>
  351. >>> from pyasn1.type import univ, constraint
  352. >>> i1 = univ.Integer(subtypeSpec=constraint.ValueRangeConstraint(3,8))
  353. >>> i2 = univ.Integer(subtypeSpec=constraint.ConstraintsIntersection(
  354. ... constraint.ValueRangeConstraint(3,8),
  355. ... constraint.ValueRangeConstraint(4,7)
  356. ... ) )
  357. >>> i1.isSameTypeWith(i2)
  358. False
  359. >>> i1.isSuperTypeOf(i2)
  360. True
  361. >>> i1.isSuperTypeOf(i1)
  362. True
  363. >>> i2.isSuperTypeOf(i1)
  364. False
  365. >>>
  366. </pre>
  367. </td></tr></table>
  368. <p>
  369. As can be seen in the above code snippet, there are two methods of any pyasn1
  370. type/value object that test types for their relationship:
  371. <b>isSameTypeWith</b>() and <b>isSuperTypeOf</b>(). The former is
  372. self-descriptive while the latter yields true if the argument appears
  373. to be a pyasn1 object which has tagset and constraints derived from those
  374. of the object being called.
  375. </p>
  376. <hr>
  377. </td>
  378. </tr>
  379. </table>
  380. </center>
  381. </body>
  382. </html>