subj_alt_name.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 AnotherName(univ.Sequence):
  79. componentType = namedtype.NamedTypes(
  80. namedtype.NamedType('type-id', univ.ObjectIdentifier()),
  81. namedtype.NamedType('value', univ.Any().subtype(
  82. explicitTag=tag.Tag(tag.tagClassContext,
  83. tag.tagFormatSimple, 0)))
  84. )
  85. class GeneralName(univ.Choice):
  86. '''ASN.1 configuration for X.509 certificate subjectAltNames fields'''
  87. componentType = namedtype.NamedTypes(
  88. namedtype.NamedType('otherName', AnotherName().subtype(
  89. implicitTag=tag.Tag(tag.tagClassContext,
  90. tag.tagFormatSimple, 0))),
  91. namedtype.NamedType('rfc822Name', char.IA5String().subtype(
  92. implicitTag=tag.Tag(tag.tagClassContext,
  93. tag.tagFormatSimple, 1))),
  94. namedtype.NamedType('dNSName', char.IA5String().subtype(
  95. implicitTag=tag.Tag(tag.tagClassContext,
  96. tag.tagFormatSimple, 2))),
  97. # namedtype.NamedType('x400Address', ORAddress().subtype(
  98. # implicitTag=tag.Tag(tag.tagClassContext,
  99. # tag.tagFormatSimple, 3))),
  100. namedtype.NamedType('directoryName', Name().subtype(
  101. implicitTag=tag.Tag(tag.tagClassContext,
  102. tag.tagFormatSimple, 4))),
  103. # namedtype.NamedType('ediPartyName', EDIPartyName().subtype(
  104. # implicitTag=tag.Tag(tag.tagClassContext,
  105. # tag.tagFormatSimple, 5))),
  106. namedtype.NamedType('uniformResourceIdentifier', char.IA5String().subtype(
  107. implicitTag=tag.Tag(tag.tagClassContext,
  108. tag.tagFormatSimple, 6))),
  109. namedtype.NamedType('iPAddress', univ.OctetString().subtype(
  110. implicitTag=tag.Tag(tag.tagClassContext,
  111. tag.tagFormatSimple, 7))),
  112. namedtype.NamedType('registeredID', univ.ObjectIdentifier().subtype(
  113. implicitTag=tag.Tag(tag.tagClassContext,
  114. tag.tagFormatSimple, 8))),
  115. )
  116. class GeneralNames(univ.SequenceOf):
  117. '''Sequence of names for ASN.1 subjectAltNames settings'''
  118. componentType = GeneralName()
  119. sizeSpec = univ.SequenceOf.sizeSpec + constraint.ValueSizeConstraint(1, MAX)
  120. class SubjectAltName(GeneralNames):
  121. '''ASN.1 implementation for subjectAltNames support'''