subj_alt_name.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. """NDG HTTPS Client package
  2. Use pyasn1 to provide support for parsing ASN.1 formatted subjectAltName
  3. content for SSL peer verification. Code based on:
  4. http://stackoverflow.com/questions/5519958/how-do-i-parse-subjectaltname-extension-data-using-pyasn1
  5. """
  6. __author__ = "P J Kershaw"
  7. __date__ = "01/02/12"
  8. __copyright__ = "(C) 2012 Science and Technology Facilities Council"
  9. __license__ = "BSD - see LICENSE file in top-level directory"
  10. __contact__ = "Philip.Kershaw@stfc.ac.uk"
  11. __revision__ = '$Id$'
  12. try:
  13. from pyasn1.type import univ, constraint, char, namedtype, tag
  14. except ImportError, e:
  15. import_error_msg = ('Error importing pyasn1, subjectAltName check for SSL '
  16. 'peer verification will be disabled. Import error '
  17. 'is: %s' % e)
  18. import warnings
  19. warnings.warn(import_error_msg)
  20. class Pyasn1ImportError(ImportError):
  21. "Raise for pyasn1 import error"
  22. raise Pyasn1ImportError(import_error_msg)
  23. MAX = 64
  24. class DirectoryString(univ.Choice):
  25. """ASN.1 Directory string class"""
  26. componentType = namedtype.NamedTypes(
  27. namedtype.NamedType(
  28. 'teletexString', char.TeletexString().subtype(
  29. subtypeSpec=constraint.ValueSizeConstraint(1, MAX))),
  30. namedtype.NamedType(
  31. 'printableString', char.PrintableString().subtype(
  32. subtypeSpec=constraint.ValueSizeConstraint(1, MAX))),
  33. namedtype.NamedType(
  34. 'universalString', char.UniversalString().subtype(
  35. subtypeSpec=constraint.ValueSizeConstraint(1, MAX))),
  36. namedtype.NamedType(
  37. 'utf8String', char.UTF8String().subtype(
  38. subtypeSpec=constraint.ValueSizeConstraint(1, MAX))),
  39. namedtype.NamedType(
  40. 'bmpString', char.BMPString().subtype(
  41. subtypeSpec=constraint.ValueSizeConstraint(1, MAX))),
  42. namedtype.NamedType(
  43. 'ia5String', char.IA5String().subtype(
  44. subtypeSpec=constraint.ValueSizeConstraint(1, MAX))),
  45. )
  46. class AttributeValue(DirectoryString):
  47. """ASN.1 Attribute value"""
  48. class AttributeType(univ.ObjectIdentifier):
  49. """ASN.1 Attribute type"""
  50. class AttributeTypeAndValue(univ.Sequence):
  51. """ASN.1 Attribute type and value class"""
  52. componentType = namedtype.NamedTypes(
  53. namedtype.NamedType('type', AttributeType()),
  54. namedtype.NamedType('value', AttributeValue()),
  55. )
  56. class RelativeDistinguishedName(univ.SetOf):
  57. '''ASN.1 Realtive distinguished name'''
  58. componentType = AttributeTypeAndValue()
  59. class RDNSequence(univ.SequenceOf):
  60. '''ASN.1 RDN sequence class'''
  61. componentType = RelativeDistinguishedName()
  62. class Name(univ.Choice):
  63. '''ASN.1 name class'''
  64. componentType = namedtype.NamedTypes(
  65. namedtype.NamedType('', RDNSequence()),
  66. )
  67. class Extension(univ.Sequence):
  68. '''ASN.1 extension class'''
  69. componentType = namedtype.NamedTypes(
  70. namedtype.NamedType('extnID', univ.ObjectIdentifier()),
  71. namedtype.DefaultedNamedType('critical', univ.Boolean('False')),
  72. namedtype.NamedType('extnValue', univ.OctetString()),
  73. )
  74. class Extensions(univ.SequenceOf):
  75. '''ASN.1 extensions class'''
  76. componentType = Extension()
  77. sizeSpec = univ.SequenceOf.sizeSpec + constraint.ValueSizeConstraint(1, MAX)
  78. class GeneralName(univ.Choice):
  79. '''ASN.1 configuration for X.509 certificate subjectAltNames fields'''
  80. componentType = namedtype.NamedTypes(
  81. # namedtype.NamedType('otherName', AnotherName().subtype(
  82. # implicitTag=tag.Tag(tag.tagClassContext,
  83. # tag.tagFormatSimple, 0))),
  84. namedtype.NamedType('rfc822Name', char.IA5String().subtype(
  85. implicitTag=tag.Tag(tag.tagClassContext,
  86. tag.tagFormatSimple, 1))),
  87. namedtype.NamedType('dNSName', char.IA5String().subtype(
  88. implicitTag=tag.Tag(tag.tagClassContext,
  89. tag.tagFormatSimple, 2))),
  90. # namedtype.NamedType('x400Address', ORAddress().subtype(
  91. # implicitTag=tag.Tag(tag.tagClassContext,
  92. # tag.tagFormatSimple, 3))),
  93. namedtype.NamedType('directoryName', Name().subtype(
  94. implicitTag=tag.Tag(tag.tagClassContext,
  95. tag.tagFormatSimple, 4))),
  96. # namedtype.NamedType('ediPartyName', EDIPartyName().subtype(
  97. # implicitTag=tag.Tag(tag.tagClassContext,
  98. # tag.tagFormatSimple, 5))),
  99. namedtype.NamedType('uniformResourceIdentifier', char.IA5String().subtype(
  100. implicitTag=tag.Tag(tag.tagClassContext,
  101. tag.tagFormatSimple, 6))),
  102. namedtype.NamedType('iPAddress', univ.OctetString().subtype(
  103. implicitTag=tag.Tag(tag.tagClassContext,
  104. tag.tagFormatSimple, 7))),
  105. namedtype.NamedType('registeredID', univ.ObjectIdentifier().subtype(
  106. implicitTag=tag.Tag(tag.tagClassContext,
  107. tag.tagFormatSimple, 8))),
  108. )
  109. class GeneralNames(univ.SequenceOf):
  110. '''Sequence of names for ASN.1 subjectAltNames settings'''
  111. componentType = GeneralName()
  112. sizeSpec = univ.SequenceOf.sizeSpec + constraint.ValueSizeConstraint(1, MAX)
  113. class SubjectAltName(GeneralNames):
  114. '''ASN.1 implementation for subjectAltNames support'''