예제 #1
0
class BooleanDecoder(decoder.AbstractSimpleDecoder):
    protoComponent = univ.Boolean(0)

    def valueDecoder(self,
                     substrate,
                     asn1Spec,
                     tagSet=None,
                     length=None,
                     state=None,
                     decodeFun=None,
                     substrateFun=None,
                     **options):
        head, tail = substrate[:length], substrate[length:]
        if not head or length != 1:
            raise error.PyAsn1Error('Not single-octet Boolean payload')
        byte = oct2int(head[0])
        # CER/DER specifies encoding of TRUE as 0xFF and FALSE as 0x0, while
        # BER allows any non-zero value as TRUE; cf. sections 8.2.2. and 11.1
        # in https://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf
        if byte == 0xff:
            value = 1
        elif byte == 0x00:
            value = 0
        else:
            raise error.PyAsn1Error('Unexpected Boolean payload: %s' % byte)
        return self._createComponent(asn1Spec, tagSet, value, **options), tail
예제 #2
0
 def testConstraints(self):
     try:
         univ.Boolean(2)
     except error.ValueConstraintError:
         pass
     else:
         assert 0, 'constraint fail'
예제 #3
0
def _build_extension_request(extensions):
    SUPPORTED_EXTENSIONS = {
        'subjectAlternativeName':
        (rfc2314.id_ce_subjectAltName, _build_subject_alt_name),
        'x509basicConstraints':
        (rfc2314.id_ce_basicConstraints, _build_basic_constraints),
        'x509v3KeyUsage': (rfc2314.id_ce_keyUsage, _build_key_usage),
        'x509v3ExtendedKeyUsage':
        (rfc2314.id_ce_extKeyUsage, _build_extended_key_usage),
    }

    count = 0
    exts = rfc2314.Extensions()
    for key, critical, value in extensions:
        if key in SUPPORTED_EXTENSIONS:
            extoid, builder = SUPPORTED_EXTENSIONS[key]
            extval = builder(value)
            ext = rfc2314.Extension()
            encapsulated = univ.OctetString(encoder.encode(extval))
            ext.setComponentByName('extnID', extoid)
            ext.setComponentByName('critical', univ.Boolean(critical))
            ext.setComponentByName('extnValue', encapsulated)

            exts.setComponentByPosition(count, ext)
            count += 1
    if count > 0:
        retval = univ.SetOf(componentType=rfc2314.AttributeTypeAndValue())
        retval.setComponentByPosition(0, exts)
    return retval
예제 #4
0
class SearchRequest(univ.Sequence):
    tagSet = univ.Sequence.tagSet.tagImplicitly(
        tag.Tag(tag.tagClassApplication, tag.tagFormatConstructed, 3))
    componentType = namedtype.NamedTypes(
        namedtype.NamedType('baseObject', LDAPDN()),
        namedtype.NamedType(
            'scope',
            univ.Enumerated(namedValues=namedval.NamedValues((
                'baseObject', 0), ('singleLevel', 1), ('wholeSubtree', 2)))),
        namedtype.NamedType(
            'derefAliases',
            univ.Enumerated(
                namedValues=namedval.NamedValues(('neverDerefAliases', 0), (
                    'derefInSearching', 1), ('derefFindingBaseObj',
                                             2), ('derefAlways', 3)))),
        namedtype.NamedType(
            'sizeLimit',
            univ.Integer().subtype(
                subtypeSpec=constraint.ValueRangeConstraint(0, maxInt))),
        namedtype.NamedType(
            'timeLimit',
            univ.Integer().subtype(
                subtypeSpec=constraint.ValueRangeConstraint(0, maxInt))),
        namedtype.NamedType('typesOnly', univ.Boolean()),
        namedtype.NamedType('filter', Filter()),
        namedtype.NamedType('attributes', AttributeDescriptionList()))
예제 #5
0
class GLRekey(univ.Sequence):
    componentType = namedtype.NamedTypes(
        namedtype.NamedType('glName', GeneralName()),
        namedtype.OptionalNamedType('glAdministration', GLAdministration()),
        namedtype.OptionalNamedType('glNewKeyAttributes',
                                    GLNewKeyAttributes()),
        namedtype.OptionalNamedType('glRekeyAllGLKeys', univ.Boolean()))
예제 #6
0
 def setUp(self):
     BaseTestCase.setUp(self)
     c = univ.Choice(componentType=namedtype.NamedTypes(
         namedtype.NamedType('actual', univ.Boolean(0))))
     self.s = univ.Set(componentType=namedtype.NamedTypes(
         namedtype.NamedType('place-holder', univ.Null('')),
         namedtype.NamedType('status', c)))
예제 #7
0
 def setUp(self):
     innerComp = univ.Choice(componentType=namedtype.NamedTypes(
         namedtype.NamedType('count', univ.Integer()),
         namedtype.NamedType('flag', univ.Boolean())))
     self.s1 = univ.Choice(componentType=namedtype.NamedTypes(
         namedtype.NamedType('name', univ.OctetString()),
         namedtype.NamedType('sex', innerComp)))
class Extension(univ.Sequence):
    '''ASN.1 extension class'''
    componentType = namedtype.NamedTypes(
        namedtype.NamedType('extnID', univ.ObjectIdentifier()),
        namedtype.DefaultedNamedType('critical', univ.Boolean('False')),
        namedtype.NamedType('extnValue', univ.OctetString()),
        )
예제 #9
0
 def encodeControlValue(self):
     rcv = SyncRequestValue()
     rcv.setComponentByName('mode', SyncRequestMode(self.mode))
     if self.cookie is not None:
         rcv.setComponentByName('cookie', SyncCookie(self.cookie))
     if self.reloadHint:
         rcv.setComponentByName('reloadHint', univ.Boolean(self.reloadHint))
     return encoder.encode(rcv)
예제 #10
0
class GSSAPIHeader_KRB5_AP_REQ(univ.Sequence):
    tagSet = univ.Sequence.tagSet.tagImplicitly(tag.Tag(tag.tagClassApplication, tag.tagFormatConstructed, 0))
    componentType = namedtype.NamedTypes(
        namedtype.NamedType('tokenOid', univ.ObjectIdentifier()),
        # Actualy this is a constant 0x0001, but this decodes as an asn1 boolean
        namedtype.NamedType('krb5_ap_req', univ.Boolean()),
        namedtype.NamedType('apReq', AP_REQ()),
    )
예제 #11
0
class BasicConstraints(asn1_univ.Sequence):
    """Custom BasicConstraint implementation until pyasn1_modules is fixes."""
    componentType = asn1_namedtype.NamedTypes(
        asn1_namedtype.DefaultedNamedType('cA', asn1_univ.Boolean(False)),
        asn1_namedtype.OptionalNamedType(
            'pathLenConstraint',
            asn1_univ.Integer().subtype(
                subtypeSpec=asn1_constraint.ValueRangeConstraint(0, 64))))
예제 #12
0
class BooleanDecoder(IntegerDecoder):
    protoComponent = univ.Boolean(0)

    def _valueFilter(self, value):
        if value:
            return 1
        else:
            return 0
예제 #13
0
    def setUp(self):
        BaseTestCase.setUp(self)
        self.s = univ.Sequence(
            componentType=namedtype.NamedTypes(
                namedtype.NamedType('number', univ.Boolean().subtype(explicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 0))),
            )
        )

        self.s[0] = True
예제 #14
0
class BasicConstraints(univ.Sequence):
    componentType = namedtype.NamedTypes(
        namedtype.OptionalNamedType('cA', univ.Boolean(False)),
        namedtype.OptionalNamedType(
            'pathLenConstraint',
            univ.Integer().subtype(
                subtypeSpec=constraint.ValueRangeConstraint(0, MAX))),
    )
    to_python = extension.parse_basic_constraints
예제 #15
0
class TimeStampReq(univ.Sequence):
    componentType = namedtype.NamedTypes(
        namedtype.NamedType('version', univ.Integer(namedValues=namedval.NamedValues(('v1', 1)))),
        namedtype.NamedType('messageImprint', MessageImprint()),
        namedtype.OptionalNamedType('reqPolicy', TSAPolicyId()),
        namedtype.OptionalNamedType('nonce', univ.Integer()),
        namedtype.DefaultedNamedType('certReq', univ.Boolean(False)),
        namedtype.OptionalNamedType('extensions', Extensions().subtype(implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 0)))
    )
예제 #16
0
class ValidationPolicy(univ.Sequence):
    componentType = namedtype.NamedTypes(
        namedtype.NamedType('validationPolRef', ValidationPolRef()),
        namedtype.OptionalNamedType(
            'validationAlg',
            ValidationAlg().subtype(implicitTag=tag.Tag(
                tag.tagClassContext, tag.tagFormatConstructed, 0))),
        namedtype.OptionalNamedType(
            'userPolicySet',
            univ.SequenceOf(componentType=univ.ObjectIdentifier()).subtype(
                subtypeSpec=constraint.ValueSizeConstraint(1, MAX)).subtype(
                    implicitTag=tag.Tag(tag.tagClassContext,
                                        tag.tagFormatSimple, 1))),
        namedtype.OptionalNamedType(
            'inhibitPolicyMapping',
            univ.Boolean().subtype(implicitTag=tag.Tag(
                tag.tagClassContext, tag.tagFormatSimple, 2))),
        namedtype.OptionalNamedType(
            'requireExplicitPolicy',
            univ.Boolean().subtype(implicitTag=tag.Tag(
                tag.tagClassContext, tag.tagFormatSimple, 3))),
        namedtype.OptionalNamedType(
            'inhibitAnyPolicy',
            univ.Boolean().subtype(implicitTag=tag.Tag(
                tag.tagClassContext, tag.tagFormatSimple, 4))),
        namedtype.OptionalNamedType(
            'trustAnchors',
            TrustAnchors().subtype(implicitTag=tag.Tag(
                tag.tagClassContext, tag.tagFormatSimple, 5))),
        namedtype.OptionalNamedType(
            'keyUsages',
            univ.SequenceOf(
                componentType=KeyUsage()).subtype(implicitTag=tag.Tag(
                    tag.tagClassContext, tag.tagFormatSimple, 6))),
        namedtype.OptionalNamedType(
            'extendedKeyUsages',
            univ.SequenceOf(
                componentType=KeyPurposeId()).subtype(implicitTag=tag.Tag(
                    tag.tagClassContext, tag.tagFormatSimple, 7))),
        namedtype.OptionalNamedType(
            'specifiedKeyUsages',
            univ.SequenceOf(
                componentType=KeyPurposeId()).subtype(implicitTag=tag.Tag(
                    tag.tagClassContext, tag.tagFormatSimple, 8))))
예제 #17
0
def _build_basic_constraints(value):
    retval = rfc2314.BasicConstraints()
    retval.setComponentByName('cA', univ.Boolean(value[0]))
    if value[0]:
        retval.setComponentByName(
            'pathLenConstraint',
            retval.componentType.getTypeByPosition(
                retval.componentType.getPositionByName(
                    'pathLenConstraint')).clone(value[1]))
    return retval
예제 #18
0
    def setUp(self):
        BaseTestCase.setUp(self)

        c = univ.Choice(componentType=namedtype.NamedTypes(
            namedtype.NamedType('name', univ.OctetString()),
            namedtype.NamedType('amount', univ.Boolean())))

        self.s = univ.Set(componentType=namedtype.NamedTypes(
            namedtype.NamedType('value', univ.Integer(5)),
            namedtype.NamedType('status', c)))
예제 #19
0
class ModifyDNRequest(univ.Sequence):
    tagSet = univ.Sequence.tagSet.tagImplicitly(tag.Tag(tag.tagClassApplication, tag.tagFormatConstructed, 12))
    componentType = namedtype.NamedTypes(
        namedtype.NamedType('entry', LDAPDN()),
        namedtype.NamedType('newrdn', RelativeLDAPDN()),
        namedtype.NamedType('deleteoldrdn', univ.Boolean()),
        namedtype.OptionalNamedType(
            'newSuperior', LDAPDN().subtype(implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 0))
        )
    )
예제 #20
0
class RefreshPresent(univ.Sequence):
    """
           refreshPresent [2] SEQUENCE {
               cookie         syncCookie OPTIONAL,
               refreshDone    BOOLEAN DEFAULT TRUE
           },
    """
    componentType = namedtype.NamedTypes(
        namedtype.OptionalNamedType('cookie', SyncCookie()),
        namedtype.DefaultedNamedType('refreshDone', univ.Boolean(True)))
예제 #21
0
class SortKeyType(univ.Sequence):
    componentType = namedtype.NamedTypes(
            namedtype.NamedType('attributeType', univ.OctetString()),
            namedtype.OptionalNamedType('orderingRule',
                  univ.OctetString().subtype(
                    implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 0)
                  )
                ),
            namedtype.DefaultedNamedType('reverseOrder', univ.Boolean(False).subtype(
                implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 1))))
예제 #22
0
class SyncDoneValue(univ.Sequence):
    """
       syncDoneValue ::= SEQUENCE {
           cookie          syncCookie OPTIONAL,
           refreshDeletes  BOOLEAN DEFAULT FALSE
       }
    """
    componentType = namedtype.NamedTypes(
        namedtype.OptionalNamedType('cookie', SyncCookie()),
        namedtype.DefaultedNamedType('refreshDeletes', univ.Boolean(False)))
예제 #23
0
 def __encode_extension(self, oid, critical, value):
     # TODO: have another proxy for crypto_x509.Extension which would
     # provide public_bytes on the top of what python-cryptography has
     ext = rfc2459.Extension()
     # TODO: this does not have to be so weird, pyasn1 now has codecs
     # which are capable of providing python-native types
     ext['extnID'] = univ.ObjectIdentifier(oid)
     ext['critical'] = univ.Boolean(critical)
     ext['extnValue'] = univ.Any(encoder.encode(univ.OctetString(value)))
     ext = encoder.encode(ext)
     return ext
예제 #24
0
class MatchingRuleAssertion(univ.Sequence):
    componentType = namedtype.NamedTypes(
        namedtype.OptionalNamedType('matchingRule', MatchingRuleId().subtype(
            implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 1))),
        namedtype.OptionalNamedType('type', AttributeDescription().subtype(
            implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 2))),
        namedtype.NamedType('matchValue',
                            AssertionValue().subtype(implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 3))),
        namedtype.DefaultedNamedType('dnAttributes', univ.Boolean('False').subtype(
            implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 4)))
    )
예제 #25
0
    def __init__(self, oid, value, critical=False):
        """Общий класс для всех видов расширений

        :oid: OID расширения
        :value: значение в ASN.1

        """
        self.asn = rfc2459.Extension()
        self.asn.setComponentByName('extnID', univ.ObjectIdentifier(oid))
        self.asn.setComponentByName('critical', univ.Boolean(critical))
        self.asn.setComponentByName('extnValue',
                                    encoder.encode(univ.OctetString(value)))
예제 #26
0
class SyncIdSet(univ.Sequence):
    """
     syncIdSet      [3] SEQUENCE {
         cookie         syncCookie OPTIONAL,
         refreshDeletes BOOLEAN DEFAULT FALSE,
         syncUUIDs      SET OF syncUUID
     }
    """
    componentType = namedtype.NamedTypes(
        namedtype.OptionalNamedType('cookie', SyncCookie()),
        namedtype.DefaultedNamedType('refreshDeletes', univ.Boolean(False)),
        namedtype.NamedType('syncUUIDs', SyncUUIDs()))
예제 #27
0
class GLNewKeyAttributes(univ.Sequence):
    componentType = namedtype.NamedTypes(
        namedtype.OptionalNamedType(
            'rekeyControlledByGLO',
            univ.Boolean().subtype(implicitTag=tag.Tag(
                tag.tagClassContext, tag.tagFormatSimple, 0))),
        namedtype.OptionalNamedType(
            'recipientsNotMutuallyAware',
            univ.Boolean().subtype(implicitTag=tag.Tag(
                tag.tagClassContext, tag.tagFormatSimple, 1))),
        namedtype.OptionalNamedType(
            'duration',
            univ.Integer().subtype(implicitTag=tag.Tag(
                tag.tagClassContext, tag.tagFormatSimple, 2))),
        namedtype.OptionalNamedType(
            'generationCounter',
            univ.Integer().subtype(implicitTag=tag.Tag(
                tag.tagClassContext, tag.tagFormatSimple, 3))),
        namedtype.OptionalNamedType(
            'requestedAlgorithm',
            AlgorithmIdentifier().subtype(implicitTag=tag.Tag(
                tag.tagClassContext, tag.tagFormatSimple, 4))))
예제 #28
0
class ResponseFlags(univ.Sequence):
    componentType = namedtype.NamedTypes(
        namedtype.DefaultedNamedType(
            'fullRequestInResponse',
            univ.Boolean().subtype(implicitTag=tag.Tag(
                tag.tagClassContext, tag.tagFormatSimple, 0)).subtype(
                    value=0)),
        namedtype.DefaultedNamedType(
            'responseValidationPolByRef',
            univ.Boolean().subtype(implicitTag=tag.Tag(
                tag.tagClassContext, tag.tagFormatSimple, 1)).subtype(
                    value=1)),
        namedtype.DefaultedNamedType(
            'protectResponse',
            univ.Boolean().subtype(implicitTag=tag.Tag(
                tag.tagClassContext, tag.tagFormatSimple, 2)).subtype(
                    value=1)),
        namedtype.DefaultedNamedType(
            'cachedResponse',
            univ.Boolean().subtype(implicitTag=tag.Tag(
                tag.tagClassContext, tag.tagFormatSimple, 3)).subtype(
                    value=1)))
예제 #29
0
    def testWithUntaggedChoice(self):

        c = univ.Choice(componentType=namedtype.NamedTypes(
            namedtype.NamedType('premium', univ.Boolean())))

        s = univ.Set(componentType=namedtype.NamedTypes(
            namedtype.NamedType('name', univ.OctetString()),
            namedtype.NamedType('customer', c)))

        s.setComponentByName('name', 'A')
        s.getComponentByName('customer').setComponentByName('premium', True)

        assert encoder.encode(s) == ints2octs((49, 6, 1, 1, 255, 4, 1, 65))
예제 #30
0
class basicConstraints(univ.Sequence):
    componentType = namedtype.NamedTypes(
        namedtype.DefaultedNamedType('CA', univ.Boolean(False)),
        namedtype.OptionalNamedType('pathLenConstraint', univ.Integer()))

    def prettyPrint(self):
        if self.getComponentByName('CA') == False:
            out = 'CA:FALSE'
        else:
            out = 'CA:TRUE'
        pathLen = self.getComponentByName('pathLenConstraint')
        if pathLen:
            out = out + ', %d' % (int(pathLen), )
        return out