Пример #1
0
 def _verifyComponent(self, idx, value, **kwargs):
     componentType = self._componentType
     if componentType:
         if idx >= len(componentType):
             raise PyAsn1Error('Component type error out of range')
         t = componentType[idx].getType()
         if not t.getTagSet().isSuperTagSetOf(value.getTagSet()):
             raise PyAsn1Error('Component type error %r vs %r' % (t, value))
Пример #2
0
    def clone(self, value=univ.noValue, **kwargs):
        """Clone this instance.

        If *value* is specified, use its tag as the component type selector,
        and itself as the component value.

        :param value: (Optional) the component value.
        :type value: :py:obj:`pyasn1.type.base.Asn1ItemBase`
        :return: the cloned instance.
        :rtype: :py:obj:`pysnmp.proto.rfc1155.NetworkAddress`
        :raise: :py:obj:`pysnmp.smi.error.SmiError`:
            if the type of *value* is not allowed for this Choice instance.
        """
        cloned = univ.Choice.clone(self, **kwargs)
        if value is not univ.noValue:
            if isinstance(value, NetworkAddress):
                value = value.getComponent()
            elif not isinstance(value, IpAddress):
                # IpAddress is the only supported type, perhaps forever because
                # this is SNMPv1.
                value = IpAddress(value)
            try:
                tagSet = value.tagSet
            except AttributeError:
                raise PyAsn1Error('component value %r has no tag set' % (value,))
            cloned.setComponentByType(tagSet, value)
        return cloned
Пример #3
0
def _decodeASN1String(rdnNameAttrValue):
    """ Tries to decode a string encoded with the following type:
      * UTF8String
      * PrintableString
      * IA5String


      This utility function is needed for 2 reasons:
      * Not all the attributes are encoded the same way, and as we do not want to bother
        with zillions of `if` conditions, we may just as well try
      * It seems like the RFCs are not always respected, and the encoding is not always the correct one

      http://www.oid-info.com/ is a very good source of information for looking up the type of
      a specific OID

      :param rdnNameAttrValue: the value part of rfc3280.AttributeTypeAndValue

      :returns: the decoded value or raises PyAsn1Error if nothing worked
  """
    for decodeType in (asn1char.UTF8String, asn1char.PrintableString,
                       asn1char.IA5String):
        try:
            attrValStr, _rest = der_decode(rdnNameAttrValue, decodeType())
        # Decoding error, try the next type
        except PyAsn1Error:
            pass
        else:
            # If the decoding worked, return it
            return attrValStr
    raise PyAsn1Error("Could not find a correct decoding type")
Пример #4
0
def require_component(asn1_obj, component_ident, val_type=None):
    val = get_component(asn1_obj, component_ident)
    if val.isValue:
        return _cast_value(val, val_type)
    else:
        raise PyAsn1Error(
            f'Required component {component_ident} is not set to a value')
Пример #5
0
def _decodeASN1String(rdnNameAttrValue):
    """Tries to decode a string encoded with the following type:
    * BMPString
    * IA5String
    * PrintableString
    * TeletexString
    * UTF8String

    Most of these types come from the definition of the issuer field in RFC3280:
    * The basic attributes, defined as DirectoryString (4.1.2.4  Issuer)
    * the optional attributes (Appendix A.  Psuedo-ASN.1 Structures and OIDs)

    This utility function is needed for 2 reasons:
    * Not all the attributes are encoded the same way, and as we do not want to bother
      with zillions of `if` conditions, we may just as well try
    * It seems like the RFCs are not always respected, and the encoding is not always the correct one

    http://www.oid-info.com/ is a very good source of information for looking up the type of
    a specific OID

    :param rdnNameAttrValue: the value part of rfc3280.AttributeTypeAndValue

    :returns: the decoded value or raises PyAsn1Error if nothing worked
    """
    for decodeType in (
            asn1char.UTF8String,
            asn1char.PrintableString,
            asn1char.IA5String,
            asn1char.TeletexString,
            asn1char.BMPString,
    ):
        try:
            attrValStr, _rest = der_decode(rdnNameAttrValue, decodeType())
        # Decoding error, try the next type
        except PyAsn1Error:
            pass
        else:
            # If the decoding worked, return it
            return attrValStr
    raise PyAsn1Error("Could not find a correct decoding type")