rfc8226.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. # This file is being contributed to pyasn1-modules software.
  2. #
  3. # Created by Russ Housley with assistance from the asn1ate tool, with manual
  4. # changes to implement appropriate constraints and added comments.
  5. # Modified by Russ Housley to add maps for use with opentypes.
  6. #
  7. # Copyright (c) 2019, Vigil Security, LLC
  8. # License: http://snmplabs.com/pyasn1/license.html
  9. #
  10. # JWT Claim Constraints and TN Authorization List for certificate extensions.
  11. #
  12. # ASN.1 source from:
  13. # https://www.rfc-editor.org/rfc/rfc8226.txt (with errata corrected)
  14. from pyasn1.type import char
  15. from pyasn1.type import constraint
  16. from pyasn1.type import namedtype
  17. from pyasn1.type import tag
  18. from pyasn1.type import univ
  19. from pyasn1_modules import rfc5280
  20. MAX = float('inf')
  21. def _OID(*components):
  22. output = []
  23. for x in tuple(components):
  24. if isinstance(x, univ.ObjectIdentifier):
  25. output.extend(list(x))
  26. else:
  27. output.append(int(x))
  28. return univ.ObjectIdentifier(output)
  29. class JWTClaimName(char.IA5String):
  30. pass
  31. class JWTClaimNames(univ.SequenceOf):
  32. pass
  33. JWTClaimNames.componentType = JWTClaimName()
  34. JWTClaimNames.sizeSpec = constraint.ValueSizeConstraint(1, MAX)
  35. class JWTClaimPermittedValues(univ.Sequence):
  36. pass
  37. JWTClaimPermittedValues.componentType = namedtype.NamedTypes(
  38. namedtype.NamedType('claim', JWTClaimName()),
  39. namedtype.NamedType('permitted', univ.SequenceOf(
  40. componentType=char.UTF8String()).subtype(
  41. sizeSpec=constraint.ValueSizeConstraint(1, MAX)))
  42. )
  43. class JWTClaimPermittedValuesList(univ.SequenceOf):
  44. pass
  45. JWTClaimPermittedValuesList.componentType = JWTClaimPermittedValues()
  46. JWTClaimPermittedValuesList.sizeSpec = constraint.ValueSizeConstraint(1, MAX)
  47. class JWTClaimConstraints(univ.Sequence):
  48. pass
  49. JWTClaimConstraints.componentType = namedtype.NamedTypes(
  50. namedtype.OptionalNamedType('mustInclude',
  51. JWTClaimNames().subtype(explicitTag=tag.Tag(tag.tagClassContext,
  52. tag.tagFormatSimple, 0))),
  53. namedtype.OptionalNamedType('permittedValues',
  54. JWTClaimPermittedValuesList().subtype(explicitTag=tag.Tag(tag.tagClassContext,
  55. tag.tagFormatSimple, 1)))
  56. )
  57. JWTClaimConstraints.subtypeSpec = constraint.ConstraintsUnion(
  58. constraint.WithComponentsConstraint(
  59. ('mustInclude', constraint.ComponentPresentConstraint())),
  60. constraint.WithComponentsConstraint(
  61. ('permittedValues', constraint.ComponentPresentConstraint()))
  62. )
  63. id_pe_JWTClaimConstraints = _OID(1, 3, 6, 1, 5, 5, 7, 1, 27)
  64. class ServiceProviderCode(char.IA5String):
  65. pass
  66. class TelephoneNumber(char.IA5String):
  67. pass
  68. TelephoneNumber.subtypeSpec = constraint.ConstraintsIntersection(
  69. constraint.ValueSizeConstraint(1, 15),
  70. constraint.PermittedAlphabetConstraint(
  71. '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '#', '*')
  72. )
  73. class TelephoneNumberRange(univ.Sequence):
  74. pass
  75. TelephoneNumberRange.componentType = namedtype.NamedTypes(
  76. namedtype.NamedType('start', TelephoneNumber()),
  77. namedtype.NamedType('count',
  78. univ.Integer().subtype(subtypeSpec=constraint.ValueRangeConstraint(2, MAX)))
  79. )
  80. class TNEntry(univ.Choice):
  81. pass
  82. TNEntry.componentType = namedtype.NamedTypes(
  83. namedtype.NamedType('spc',
  84. ServiceProviderCode().subtype(explicitTag=tag.Tag(tag.tagClassContext,
  85. tag.tagFormatSimple, 0))),
  86. namedtype.NamedType('range',
  87. TelephoneNumberRange().subtype(explicitTag=tag.Tag(tag.tagClassContext,
  88. tag.tagFormatConstructed, 1))),
  89. namedtype.NamedType('one',
  90. TelephoneNumber().subtype(explicitTag=tag.Tag(tag.tagClassContext,
  91. tag.tagFormatSimple, 2)))
  92. )
  93. class TNAuthorizationList(univ.SequenceOf):
  94. pass
  95. TNAuthorizationList.componentType = TNEntry()
  96. TNAuthorizationList.sizeSpec = constraint.ValueSizeConstraint(1, MAX)
  97. id_pe_TNAuthList = _OID(1, 3, 6, 1, 5, 5, 7, 1, 26)
  98. id_ad_stirTNList = _OID(1, 3, 6, 1, 5, 5, 7, 48, 14)
  99. # Map of Certificate Extension OIDs to Extensions added to the
  100. # ones that are in rfc5280.py
  101. _certificateExtensionsMapUpdate = {
  102. id_pe_TNAuthList: TNAuthorizationList(),
  103. id_pe_JWTClaimConstraints: JWTClaimConstraints(),
  104. }
  105. rfc5280.certificateExtensionsMap.update(_certificateExtensionsMapUpdate)