Package ndg :: Package httpsclient :: Module subj_alt_name
[hide private]

Source Code for Module ndg.httpsclient.subj_alt_name

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