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)
24 "Raise for pyasn1 import error"
25 raise Pyasn1ImportError(import_error_msg)
26
27
28 MAX = 64
29
30
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
56 """ASN.1 Attribute value"""
57
58
60 """ASN.1 Attribute type"""
61
62
69
70
74
78
79
80 -class Name(univ.Choice):
85
86
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
100
101
103 '''ASN.1 configuration for X.509 certificate subjectAltNames fields'''
104 componentType = namedtype.NamedTypes(
105
106
107
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
115
116
117 namedtype.NamedType('directoryName', Name().subtype(
118 implicitTag=tag.Tag(tag.tagClassContext,
119 tag.tagFormatSimple, 4))),
120
121
122
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
139
140
142 '''ASN.1 implementation for subjectAltNames support'''
143