fix starting Gajim when pyasn1 is not installed. see #7066

This commit is contained in:
Yann Leboulanger 2012-01-02 17:01:31 +01:00
parent aa94671708
commit 9804572772

View file

@ -1,14 +1,20 @@
from pyasn1.type import univ, constraint, char, namedtype, tag import logging
from pyasn1.codec.der.decoder import decode log = logging.getLogger('gajim.c.check_X509')
from common.helpers import prep, InvalidFormat
MAX = 64 try:
oid_xmppaddr = '(1, 3, 6, 1, 5, 5, 7, 8, 5)' import OpenSSL.SSL
oid_dnssrv = '(1, 3, 6, 1, 5, 5, 7, 8, 7)' import OpenSSL.crypto
from pyasn1.type import univ, constraint, char, namedtype, tag
from pyasn1.codec.der.decoder import decode
from common.helpers import prep, InvalidFormat
MAX = 64
oid_xmppaddr = '(1, 3, 6, 1, 5, 5, 7, 8, 5)'
oid_dnssrv = '(1, 3, 6, 1, 5, 5, 7, 8, 7)'
class DirectoryString(univ.Choice): class DirectoryString(univ.Choice):
componentType = namedtype.NamedTypes( componentType = namedtype.NamedTypes(
namedtype.NamedType( namedtype.NamedType(
'teletexString', char.TeletexString().subtype( 'teletexString', char.TeletexString().subtype(
@ -33,30 +39,30 @@ class DirectoryString(univ.Choice):
subtypeSpec=constraint.ValueSizeConstraint(1, MAX))), subtypeSpec=constraint.ValueSizeConstraint(1, MAX))),
) )
class AttributeValue(DirectoryString): class AttributeValue(DirectoryString):
pass pass
class AttributeType(univ.ObjectIdentifier): class AttributeType(univ.ObjectIdentifier):
pass pass
class AttributeTypeAndValue(univ.Sequence): class AttributeTypeAndValue(univ.Sequence):
componentType = namedtype.NamedTypes( componentType = namedtype.NamedTypes(
namedtype.NamedType('type', AttributeType()), namedtype.NamedType('type', AttributeType()),
namedtype.NamedType('value', AttributeValue()), namedtype.NamedType('value', AttributeValue()),
) )
class RelativeDistinguishedName(univ.SetOf): class RelativeDistinguishedName(univ.SetOf):
componentType = AttributeTypeAndValue() componentType = AttributeTypeAndValue()
class RDNSequence(univ.SequenceOf): class RDNSequence(univ.SequenceOf):
componentType = RelativeDistinguishedName() componentType = RelativeDistinguishedName()
class Name(univ.Choice): class Name(univ.Choice):
componentType = namedtype.NamedTypes( componentType = namedtype.NamedTypes(
namedtype.NamedType('', RDNSequence()), namedtype.NamedType('', RDNSequence()),
) )
class GeneralName(univ.Choice): class GeneralName(univ.Choice):
componentType = namedtype.NamedTypes( componentType = namedtype.NamedTypes(
namedtype.NamedType('otherName', univ.Sequence().subtype( namedtype.NamedType('otherName', univ.Sequence().subtype(
implicitTag=tag.Tag(tag.tagClassContext, implicitTag=tag.Tag(tag.tagClassContext,
@ -88,15 +94,11 @@ class GeneralName(univ.Choice):
tag.tagFormatSimple, 8))), tag.tagFormatSimple, 8))),
) )
class GeneralNames(univ.SequenceOf): class GeneralNames(univ.SequenceOf):
componentType = GeneralName() componentType = GeneralName()
sizeSpec = univ.SequenceOf.sizeSpec + constraint.ValueSizeConstraint(1, MAX) sizeSpec = univ.SequenceOf.sizeSpec + constraint.ValueSizeConstraint(1, MAX)
def _parse_asn1(asn1):
#s = '0\x1a\x82\rwww.gajim.org\x82\tgajim.org'
s = '0\x81\x86\x82\x0c*.jabber.org\x82\njabber.org\xa0\x1a\x06\x08+\x06\x01\x05\x05\x07\x08\x05\xa0\x0e\x0c\x0c*.jabber.org\xa0\x1a\x06\x08+\x06\x01\x05\x05\x07\x08\x07\xa0\x0e\x16\x0c*.jabber.org\xa0\x18\x06\x08+\x06\x01\x05\x05\x07\x08\x05\xa0\x0c\x0c\njabber.org\xa0\x18\x06\x08+\x06\x01\x05\x05\x07\x08\x07\xa0\x0c\x16\njabber.org'
def _parse_asn1(asn1):
obj = decode(asn1, asn1Spec=GeneralNames())[0] obj = decode(asn1, asn1Spec=GeneralNames())[0]
r = {} r = {}
for o in obj: for o in obj:
@ -117,7 +119,7 @@ def _parse_asn1(asn1):
r['uniformResourceIdentifier'] = True r['uniformResourceIdentifier'] = True
return r return r
def check_certificate(cert, domain): def check_certificate(cert, domain):
cnt = cert.get_extension_count() cnt = cert.get_extension_count()
if '.' in domain: if '.' in domain:
compared_domain = domain.split('.', 1)[1] compared_domain = domain.split('.', 1)[1]
@ -162,3 +164,12 @@ def check_certificate(cert, domain):
if subject.commonName == domain: if subject.commonName == domain:
return True return True
return False return False
except ImportError:
log.warn('Import of PyOpenSSL or pyasn1 failed. Cannot correctly check '
'SSL certificate')
def check_certificate(cert, domain):
subject = cert.get_subject()
if subject.commonName == domain:
return True
return False