2011-04-01 9 views
14

मेरे पास कुछ डेटा है जो pyOpenSSL ने मुझे दिया, '0\r\x82\x0bexample.com'। यह एक विषयAltName X509 एक्सटेंशन का मान होना चाहिए। मैं pyasn1 का उपयोग कर इस विस्तार के लिए ASN1 विनिर्देश के आवश्यक भागों सांकेतिक शब्दों में बदलना करने की कोशिश की (और pyasn1 उदाहरणों में से एक के आधार पर):मैं pyasn1 का उपयोग कर topicAltName एक्सटेंशन डेटा को कैसे पार्स करूं?

from pyasn1.type import univ, constraint, char, namedtype 

from pyasn1.codec.der.decoder import decode 

MAX = 64 

class DirectoryString(univ.Choice): 
    componentType = namedtype.NamedTypes(
     namedtype.NamedType(
      'teletexString', char.TeletexString().subtype(
       subtypeSpec=constraint.ValueSizeConstraint(1, MAX))), 
     namedtype.NamedType(
      'printableString', char.PrintableString().subtype(
       subtypeSpec=constraint.ValueSizeConstraint(1, MAX))), 
     namedtype.NamedType(
      'universalString', char.UniversalString().subtype(
       subtypeSpec=constraint.ValueSizeConstraint(1, MAX))), 
     namedtype.NamedType(
      'utf8String', char.UTF8String().subtype(
       subtypeSpec=constraint.ValueSizeConstraint(1, MAX))), 
     namedtype.NamedType(
      'bmpString', char.BMPString().subtype(
       subtypeSpec=constraint.ValueSizeConstraint(1, MAX))), 
     namedtype.NamedType(
      'ia5String', char.IA5String().subtype(
       subtypeSpec=constraint.ValueSizeConstraint(1, MAX))), 
     ) 


class AttributeValue(DirectoryString): 
    pass 


class AttributeType(univ.ObjectIdentifier): 
    pass 


class AttributeTypeAndValue(univ.Sequence): 
    componentType = namedtype.NamedTypes(
     namedtype.NamedType('type', AttributeType()), 
     namedtype.NamedType('value', AttributeValue()), 
     ) 


class RelativeDistinguishedName(univ.SetOf): 
    componentType = AttributeTypeAndValue() 

class RDNSequence(univ.SequenceOf): 
    componentType = RelativeDistinguishedName() 


class Name(univ.Choice): 
    componentType = namedtype.NamedTypes(
     namedtype.NamedType('', RDNSequence()), 
     ) 


class Extension(univ.Sequence): 
    componentType = namedtype.NamedTypes(
     namedtype.NamedType('extnID', univ.ObjectIdentifier()), 
     namedtype.DefaultedNamedType('critical', univ.Boolean('False')), 
     namedtype.NamedType('extnValue', univ.OctetString()), 
     ) 


class Extensions(univ.SequenceOf): 
    componentType = Extension() 
    sizeSpec = univ.SequenceOf.sizeSpec + constraint.ValueSizeConstraint(1, MAX) 


class GeneralName(univ.Choice): 
    componentType = namedtype.NamedTypes(
     # namedtype.NamedType('otherName', AnotherName()), 
     namedtype.NamedType('rfc822Name', char.IA5String()), 
     namedtype.NamedType('dNSName', char.IA5String()), 
     # namedtype.NamedType('x400Address', ORAddress()), 
     namedtype.NamedType('directoryName', Name()), 
     # namedtype.NamedType('ediPartyName', EDIPartyName()), 
     namedtype.NamedType('uniformResourceIdentifier', char.IA5String()), 
     namedtype.NamedType('iPAddress', univ.OctetString()), 
     namedtype.NamedType('registeredID', univ.ObjectIdentifier()), 
     ) 


class GeneralNames(univ.SequenceOf): 
    componentType = GeneralName() 
    sizeSpec = univ.SequenceOf.sizeSpec + constraint.ValueSizeConstraint(1, MAX) 


class SubjectAltName(GeneralNames): 
    pass 

print decode('0\r\x82\x0bexample.com', asn1Spec=GeneralNames()) 

जाहिर है मैं अंत के पास एक छोटे से ऊब गया और पूरी तरह से निर्दिष्ट नहीं किया है GeneralName प्रकार। हालांकि, परीक्षण स्ट्रिंग में dNSName होना चाहिए, जो छोड़े गए मानों में से एक नहीं है, इसलिए मुझे उम्मीद है कि इससे कोई फर्क नहीं पड़ता।

जब कार्यक्रम चलाया जाता है, यह एक त्रुटि के साथ विफल रहा व्याख्या करने में सक्षम नहीं कर रहा हूँ:

Traceback (most recent call last): 
    File "x509.py", line 94, in <module> 
    print decode('0\r\x82\x0bexample.com', asn1Spec=GeneralNames()) 
    File "/usr/lib/pymodules/python2.6/pyasn1/v1/codec/ber/decoder.py", line 493, in __call__ 
    length, stGetValueDecoder, decodeFun 
    File "/usr/lib/pymodules/python2.6/pyasn1/v1/codec/ber/decoder.py", line 202, in valueDecoder 
    substrate, asn1Spec 
    File "/usr/lib/pymodules/python2.6/pyasn1/v1/codec/ber/decoder.py", line 453, in __call__ 
    __chosenSpec.getTypeMap().has_key(tagSet): 
    File "/usr/lib/pymodules/python2.6/pyasn1/v1/type/univ.py", line 608, in getTypeMap 
    return Set.getComponentTypeMap(self) 
    File "/usr/lib/pymodules/python2.6/pyasn1/v1/type/univ.py", line 535, in getComponentTypeMap 
    def getComponentTypeMap(self): return self._componentType.getTypeMap(1) 
    File "/usr/lib/pymodules/python2.6/pyasn1/v1/type/namedtype.py", line 126, in getTypeMap 
    'Duplicate type %s in map %s'%(k,self.__typeMap) 
pyasn1.error.PyAsn1Error: Duplicate type TagSet(Tag(tagClass=0, tagFormat=0, tagId=22)) in map {TagSet(Tag(tagClass=0, tagFormat=0, tagId=22)): IA5String()} 

जहाँ मैं गलत हो गया था पर कोई सुझाव और कैसे सफलतापूर्वक पार्स करने के लिए pyasn1 के साथ इस प्रकार के एक्सटेंशन के बहुत होगा की सराहना की।

उत्तर

15

मैंने इस प्रश्न को pyasn1-user सूची और plyasn1 के लेखक (pyasn1 के लेखक) पर पोस्ट किया है, मेरी गलती की ओर इशारा किया। संक्षेप में, GeneralName.componentType में प्रत्येक NamedType टैग जानकारी दी जानी चाहिए। यह subtype विधि के साथ किया जाता है। उदाहरण के लिए, के बजाय के लिए:

namedtype.NamedType('rfc822Name', char.IA5String()), 

परिभाषा होनी चाहिए:

namedtype.NamedType('rfc822Name', char.IA5String().subtype(
     implicitTag=tag.Tag(tag.tagClassContext, 
          tag.tagFormatSimple, 1))), 

जहां 1GeneralName की ASN.1 परिभाषा से आता है:

GeneralName ::= CHOICE { 
    otherName      [0]  OtherName, 
    rfc822Name      [1]  IA5String, 
    dNSName       [2]  IA5String, 
    x400Address      [3]  ORAddress, 
    directoryName     [4]  Name, 
    ediPartyName     [5]  EDIPartyName, 
    uniformResourceIdentifier  [6]  IA5String, 
    iPAddress      [7]  OCTET STRING, 
    registeredID     [8]  OBJECT IDENTIFIER 
} 

एक टैग परिभाषित करने के बाद componentType के इन क्षेत्रों में से प्रत्येक के लिए, पार्सिंग सफल होती है:

(GeneralNames().setComponentByPosition(
    0, GeneralName().setComponentByPosition(1, IA5String('example.com'))), '') 
संबंधित मुद्दे