Python pyasn1.type.univ.OctetString() Examples

The following are 30 code examples of pyasn1.type.univ.OctetString(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module pyasn1.type.univ , or try the search function .
Example #1
Source File: test_rfc7906.py    From pyasn1-modules with BSD 2-Clause "Simplified" License 6 votes vote down vote up
def testOpenTypes(self):
        openTypesMap = rfc5280.certificateAttributesMap.copy()
        openTypesMap.update(rfc5652.cmsAttributesMap)

        substrate = pem.readBase64fromText(self.attr_set_pem_text)
        asn1Object, rest = der_decoder(
            substrate, asn1Spec=self.asn1Spec, openTypes=openTypesMap,
            decodeOpenTypes=True)

        self.assertFalse(rest)
        self.assertTrue(asn1Object.prettyPrint())
        self.assertEqual(substrate, der_encoder(asn1Object))

        for attr in asn1Object:
            if attr['type'] == rfc7906.id_aa_KP_contentDecryptKeyID:
                self.assertEqual(
                    univ.OctetString(hexValue='7906'), attr['values'][0]) 
Example #2
Source File: extended.py    From teleport with Apache License 2.0 6 votes vote down vote up
def extended_operation(request_name,
                       request_value=None,
                       no_encode=None):
    request = ExtendedRequest()
    request['requestName'] = RequestName(request_name)
    if request_value and isinstance(request_value, Asn1Item):
        request['requestValue'] = RequestValue(encode(request_value))
    elif str is not bytes and isinstance(request_value, (bytes, bytearray)):  # in Python 3 doesn't try to encode a byte value
        request['requestValue'] = request_value
    elif request_value and no_encode:  # doesn't encode the value
        request['requestValue'] = request_value
    elif request_value:  # tries to encode as a octet string
        request['requestValue'] = RequestValue(encode(OctetString(str(request_value))))

    # elif request_value is not None:
    #     raise LDAPExtensionError('unable to encode value for extended operation')
    return request 
Example #3
Source File: test_rfc3279.py    From pyasn1-modules with BSD 2-Clause "Simplified" License 6 votes vote down vote up
def testOpenTypes(self):
        substrate = pem.readBase64fromText(self.kea_cert_pem_text)
        asn1Object, rest = der_decoder(
            substrate, asn1Spec=self.asn1Spec, decodeOpenTypes=True)

        self.assertFalse(rest)
        self.assertTrue(asn1Object.prettyPrint())
        self.assertEqual(substrate, der_encoder(asn1Object))

        spki_a = asn1Object['tbsCertificate']['subjectPublicKeyInfo']['algorithm']

        self.assertEqual(rfc3279.id_keyExchangeAlgorithm, spki_a['algorithm'])
        self.assertEqual(
            univ.OctetString(hexValue='5cf8f127e6569d6d88b3'), spki_a['parameters'])

        self.assertEqual(rfc3279.id_dsa_with_sha1,
                         asn1Object['tbsCertificate']['signature']['algorithm'])
        self.assertEqual(
            rfc3279.id_dsa_with_sha1, asn1Object['signatureAlgorithm']['algorithm']) 
Example #4
Source File: char.py    From teleport with Apache License 2.0 6 votes vote down vote up
def prettyIn(self, value):
            try:
                if isinstance(value, str):
                    return value
                elif isinstance(value, bytes):
                    return value.decode(self.encoding)
                elif isinstance(value, (tuple, list)):
                    return self.prettyIn(bytes(value))
                elif isinstance(value, univ.OctetString):
                    return value.asOctets().decode(self.encoding)
                else:
                    return str(value)

            except (UnicodeDecodeError, LookupError):
                exc = sys.exc_info()[1]
                raise error.PyAsn1UnicodeDecodeError(
                    "Can't decode string '%s' with codec "
                    "%s" % (value, self.encoding), exc
                ) 
Example #5
Source File: microsoft.py    From teleport with Apache License 2.0 6 votes vote down vote up
def dir_sync_control(criticality, object_security, ancestors_first, public_data_only, incremental_values, max_length, cookie):
    control_value = DirSyncControlRequestValue()
    flags = 0x0
    if object_security:
        flags |= 0x00000001

    if ancestors_first:
        flags |= 0x00000800

    if public_data_only:
        flags |= 0x00002000

    if incremental_values:
        flags |= 0x80000000
        # converts flags to signed 32 bit (AD expects a 4 bytes long unsigned integer, but ASN.1 Integer type is signed
        # so the BER encoder gives back a 5 bytes long signed integer
        flags = ctypes.c_long(flags & 0xFFFFFFFF).value

    control_value.setComponentByName('Flags', flags)
    control_value.setComponentByName('MaxBytes', max_length)
    if cookie:
        control_value.setComponentByName('Cookie', cookie)
    else:
        control_value.setComponentByName('Cookie', OctetString(''))
    return build_control('1.2.840.113556.1.4.841', criticality, control_value) 
Example #6
Source File: char.py    From teleport with Apache License 2.0 6 votes vote down vote up
def prettyIn(self, value):
            try:
                if isinstance(value, unicode):
                    return value
                elif isinstance(value, str):
                    return value.decode(self.encoding)
                elif isinstance(value, (tuple, list)):
                    return self.prettyIn(''.join([chr(x) for x in value]))
                elif isinstance(value, univ.OctetString):
                    return value.asOctets().decode(self.encoding)
                else:
                    return unicode(value)

            except (UnicodeDecodeError, LookupError):
                exc = sys.exc_info()[1]
                raise error.PyAsn1UnicodeDecodeError(
                    "Can't decode string '%s' with codec "
                    "%s" % (value, self.encoding), exc
                ) 
Example #7
Source File: char.py    From teleport with Apache License 2.0 6 votes vote down vote up
def prettyIn(self, value):
            try:
                if isinstance(value, str):
                    return value
                elif isinstance(value, bytes):
                    return value.decode(self.encoding)
                elif isinstance(value, (tuple, list)):
                    return self.prettyIn(bytes(value))
                elif isinstance(value, univ.OctetString):
                    return value.asOctets().decode(self.encoding)
                else:
                    return str(value)

            except (UnicodeDecodeError, LookupError):
                raise error.PyAsn1Error(
                    "Can't decode string '%s' with codec %s" % (value, self.encoding)
                ) 
Example #8
Source File: char.py    From teleport with Apache License 2.0 6 votes vote down vote up
def prettyIn(self, value):
            try:
                if isinstance(value, str):
                    return value
                elif isinstance(value, bytes):
                    return value.decode(self.encoding)
                elif isinstance(value, (tuple, list)):
                    return self.prettyIn(bytes(value))
                elif isinstance(value, univ.OctetString):
                    return value.asOctets().decode(self.encoding)
                else:
                    return str(value)

            except (UnicodeDecodeError, LookupError):
                exc = sys.exc_info()[1]
                raise error.PyAsn1UnicodeDecodeError(
                    "Can't decode string '%s' with codec "
                    "%s" % (value, self.encoding), exc
                ) 
Example #9
Source File: char.py    From teleport with Apache License 2.0 6 votes vote down vote up
def prettyIn(self, value):
            try:
                if isinstance(value, unicode):
                    return value
                elif isinstance(value, str):
                    return value.decode(self.encoding)
                elif isinstance(value, (tuple, list)):
                    return self.prettyIn(''.join([chr(x) for x in value]))
                elif isinstance(value, univ.OctetString):
                    return value.asOctets().decode(self.encoding)
                else:
                    return unicode(value)

            except (UnicodeDecodeError, LookupError):
                raise error.PyAsn1Error(
                    "Can't decode string '%s' with codec %s" % (value, self.encoding)
                ) 
Example #10
Source File: test_rfc2876.py    From pyasn1-modules with BSD 2-Clause "Simplified" License 6 votes vote down vote up
def testOpenTypes(self):
        substrate = pem.readBase64fromText(self.env_data_pem_text)
        asn1Object, rest = der_decoder(
            substrate, asn1Spec=self.asn1Spec, decodeOpenTypes=True)
        self.assertFalse(rest)
        self.assertTrue(asn1Object.prettyPrint())
        self.assertEqual(substrate, der_encoder(asn1Object))
        
        self.assertIn(asn1Object['contentType'], rfc5652.cmsContentTypesMap.keys())
        kari_kea = asn1Object['content']['recipientInfos'][0]['kari']['keyEncryptionAlgorithm']
        self.assertEqual(rfc2876.id_kEAKeyEncryptionAlgorithm, kari_kea['algorithm'])
        self.assertEqual(rfc2876.id_fortezzaWrap80, kari_kea['parameters']['algorithm'])

        cea = asn1Object['content']['encryptedContentInfo']['contentEncryptionAlgorithm']
        self.assertEqual(rfc2876.id_fortezzaConfidentialityAlgorithm, cea['algorithm'])

        iv = univ.OctetString(hexValue='424f4755535f4956')
        self.assertEqual(iv, cea['parameters']['initialization-vector']) 
Example #11
Source File: test_rfc4010.py    From pyasn1-modules with BSD 2-Clause "Simplified" License 6 votes vote down vote up
def testOpenTypes(self):
        substrate = pem.readBase64fromText(self.env_data_pem_text)
        asn1Object, rest = der_decoder(
            substrate, asn1Spec=self.asn1Spec, decodeOpenTypes=True)
        self.assertFalse(rest)
        self.assertTrue(asn1Object.prettyPrint())
        self.assertEqual(substrate, der_encoder(asn1Object))
        self.assertTrue(asn1Object['contentType'] in rfc5652.cmsContentTypesMap.keys())

        kekri = asn1Object['content']['recipientInfos'][0]['kekri']
        kwa = kekri['keyEncryptionAlgorithm']
        self.assertEqual(rfc4010.id_npki_app_cmsSeed_wrap, kwa['algorithm'])

        eci = asn1Object['content']['encryptedContentInfo']
        cea = eci['contentEncryptionAlgorithm']
        self.assertEqual(rfc4010.id_seedCBC, cea['algorithm'])

        iv = univ.OctetString(hexValue='424f47555349565f424f475553495621')
        self.assertEqual(iv, cea['parameters']) 
Example #12
Source File: test_rfc4055.py    From pyasn1-modules with BSD 2-Clause "Simplified" License 6 votes vote down vote up
def testOpenTypes(self):
        substrate = pem.readBase64fromText(self.oaep_full_pem_text)
        asn1Object, rest = der_decoder.decode(substrate,
                                              asn1Spec=self.asn1Spec,
                                              decodeOpenTypes=True)
        self.assertFalse(rest)
        self.assertTrue(asn1Object.prettyPrint())
        self.assertEqual(substrate, der_encoder.encode(asn1Object))

        self.assertTrue(asn1Object['parameters'].hasValue())

        oaep_p = asn1Object['parameters']

        self.assertEqual(univ.Null(""), oaep_p['hashFunc']['parameters'])
        self.assertEqual(
            univ.Null(""), oaep_p['maskGenFunc']['parameters']['parameters'])
        self.assertEqual(
            univ.OctetString(value='foobar'),
            oaep_p['pSourceFunc']['parameters']) 
Example #13
Source File: test_rfc3370.py    From pyasn1-modules with BSD 2-Clause "Simplified" License 6 votes vote down vote up
def testOpenTypes(self):
        substrate = pem.readBase64fromText(self.env_data_pem_text)
        asn1Object, rest = der_decoder(
            substrate, asn1Spec=self.asn1Spec, decodeOpenTypes=True)
        self.assertFalse(rest)
        self.assertTrue(asn1Object.prettyPrint())
        self.assertEqual(substrate, der_encoder(asn1Object))
        self.assertTrue(asn1Object['contentType'] in rfc5652.cmsContentTypesMap.keys())

        ri0 = asn1Object['content']['recipientInfos'][0]
        kwa = ri0['kekri']['keyEncryptionAlgorithm']
        self.assertEqual(rfc3370.id_alg_CMSRC2wrap, kwa['algorithm'])
        self.assertEqual(58, kwa['parameters'])

        eci = asn1Object['content']['encryptedContentInfo']
        cea = eci['contentEncryptionAlgorithm']
        self.assertEqual(rfc3370.rc2CBC, cea['algorithm'])

        iv = univ.OctetString(hexValue='424f4755535f4956')
        self.assertEqual(iv, cea['parameters']['iv'])
        self.assertEqual(58, cea['parameters']['rc2ParameterVersion']) 
Example #14
Source File: test_rfc8479.py    From pyasn1-modules with BSD 2-Clause "Simplified" License 6 votes vote down vote up
def testOpenTypes(self):
        substrate = pem.readBase64fromText(self.pem_text)
        asn1Object, rest = der_decoder(
            substrate, asn1Spec=self.asn1Spec, decodeOpenTypes=True)

        self.assertFalse(rest)
        self.assertTrue(asn1Object.prettyPrint())
        self.assertEqual(substrate, der_encoder(asn1Object))

        for attr in asn1Object['attributes']:
            self.assertIn(attr['attrType'], rfc5652.cmsAttributesMap)
            if attr['attrType'] == rfc8479.id_attr_validation_parameters:
                av = attr['attrValues'][0]

                self.assertEqual(av['hashAlg'], rfc4055.id_sha384)

                seed = univ.OctetString(hexValue='8af4328c87bebcec31e303b8f553'
                                                 '7effcb6a91d947084d99a369823b'
                                                 '36f01462')

                self.assertEqual(seed, av['seed']) 
Example #15
Source File: test_rfc7906.py    From pyasn1-modules with BSD 2-Clause "Simplified" License 6 votes vote down vote up
def testDerCodec(self):
        substrate = pem.readBase64fromText(self.attr_set_pem_text)
        asn1Object, rest = der_decoder(substrate, asn1Spec=self.asn1Spec)

        self.assertFalse(rest)
        self.assertTrue(asn1Object.prettyPrint())
        self.assertEqual(substrate, der_encoder(asn1Object))

        for attr in asn1Object:
            self.assertIn(attr['type'], rfc5652.cmsAttributesMap)

            av, rest = der_decoder(
                attr['values'][0],
                asn1Spec=rfc5652.cmsAttributesMap[attr['type']])

            self.assertFalse(rest)
            self.assertTrue(av.prettyPrint())
            self.assertEqual(attr['values'][0], der_encoder(av))

            if attr['type'] == rfc7906.id_aa_KP_contentDecryptKeyID:
                self.assertEqual(univ.OctetString(hexValue='7906'), av) 
Example #16
Source File: microsoft.py    From teleport with Apache License 2.0 6 votes vote down vote up
def dir_sync_control(criticality, object_security, ancestors_first, public_data_only, incremental_values, max_length, cookie):
    control_value = DirSyncControlRequestValue()
    flags = 0x0
    if object_security:
        flags |= 0x00000001

    if ancestors_first:
        flags |= 0x00000800

    if public_data_only:
        flags |= 0x00002000

    if incremental_values:
        flags |= 0x80000000
        # converts flags to signed 32 bit (AD expects a 4 bytes long unsigned integer, but ASN.1 Integer type is signed
        # so the BER encoder gives back a 5 bytes long signed integer
        flags = ctypes.c_long(flags & 0xFFFFFFFF).value

    control_value.setComponentByName('Flags', flags)
    control_value.setComponentByName('MaxBytes', max_length)
    if cookie:
        control_value.setComponentByName('Cookie', cookie)
    else:
        control_value.setComponentByName('Cookie', OctetString(''))
    return build_control('1.2.840.113556.1.4.841', criticality, control_value) 
Example #17
Source File: test_rfc3058.py    From pyasn1-modules with BSD 2-Clause "Simplified" License 6 votes vote down vote up
def testOpenTypes(self):
        substrate = pem.readBase64fromText(self.env_data_pem_text)
        asn1Object, rest = der_decoder(
            substrate, asn1Spec=self.asn1Spec, decodeOpenTypes=True)

        self.assertFalse(rest)
        self.assertTrue(asn1Object.prettyPrint())
        self.assertEqual(substrate, der_encoder(asn1Object))

        kekri = asn1Object['content']['recipientInfos'][0]['kekri']
        kwa = kekri['keyEncryptionAlgorithm']
        self.assertEqual(rfc3058.id_alg_CMSIDEAwrap, kwa['algorithm'])
        self.assertEqual(univ.Null(""), kwa['parameters'])

        eci = asn1Object['content']['encryptedContentInfo']
        cea = eci['contentEncryptionAlgorithm']
        self.assertEqual(rfc3058.id_IDEA_CBC, cea['algorithm'])

        iv = univ.OctetString(hexValue='424f4755535f4956')
        self.assertEqual(iv, cea['parameters']['iv']) 
Example #18
Source File: char.py    From learn_python3_spider with MIT License 6 votes vote down vote up
def prettyIn(self, value):
            try:
                if isinstance(value, unicode):
                    return value
                elif isinstance(value, str):
                    return value.decode(self.encoding)
                elif isinstance(value, (tuple, list)):
                    return self.prettyIn(''.join([chr(x) for x in value]))
                elif isinstance(value, univ.OctetString):
                    return value.asOctets().decode(self.encoding)
                else:
                    return unicode(value)

            except (UnicodeDecodeError, LookupError):
                exc = sys.exc_info()[1]
                raise error.PyAsn1UnicodeDecodeError(
                    "Can't decode string '%s' with codec "
                    "%s" % (value, self.encoding), exc
                ) 
Example #19
Source File: char.py    From learn_python3_spider with MIT License 6 votes vote down vote up
def prettyIn(self, value):
            try:
                if isinstance(value, str):
                    return value
                elif isinstance(value, bytes):
                    return value.decode(self.encoding)
                elif isinstance(value, (tuple, list)):
                    return self.prettyIn(bytes(value))
                elif isinstance(value, univ.OctetString):
                    return value.asOctets().decode(self.encoding)
                else:
                    return str(value)

            except (UnicodeDecodeError, LookupError):
                exc = sys.exc_info()[1]
                raise error.PyAsn1UnicodeDecodeError(
                    "Can't decode string '%s' with codec "
                    "%s" % (value, self.encoding), exc
                ) 
Example #20
Source File: char.py    From bash-lambda-layer with MIT License 6 votes vote down vote up
def prettyIn(self, value):
            try:
                if isinstance(value, unicode):
                    return value
                elif isinstance(value, str):
                    return value.decode(self.encoding)
                elif isinstance(value, (tuple, list)):
                    return self.prettyIn(''.join([chr(x) for x in value]))
                elif isinstance(value, univ.OctetString):
                    return value.asOctets().decode(self.encoding)
                else:
                    return unicode(value)

            except (UnicodeDecodeError, LookupError):
                raise error.PyAsn1Error(
                    "Can't decode string '%s' with codec %s" % (value, self.encoding)
                ) 
Example #21
Source File: char.py    From bash-lambda-layer with MIT License 6 votes vote down vote up
def prettyIn(self, value):
            try:
                if isinstance(value, str):
                    return value
                elif isinstance(value, bytes):
                    return value.decode(self.encoding)
                elif isinstance(value, (tuple, list)):
                    return self.prettyIn(bytes(value))
                elif isinstance(value, univ.OctetString):
                    return value.asOctets().decode(self.encoding)
                else:
                    return str(value)

            except (UnicodeDecodeError, LookupError):
                raise error.PyAsn1Error(
                    "Can't decode string '%s' with codec %s" % (value, self.encoding)
                ) 
Example #22
Source File: aes.py    From scalyr-agent-2 with Apache License 2.0 6 votes vote down vote up
def __getEncryptionKey(self, privKey, snmpEngineBoots, snmpEngineTime):
        salt = [
            self._localInt>>56&0xff,
            self._localInt>>48&0xff,
            self._localInt>>40&0xff,
            self._localInt>>32&0xff,
            self._localInt>>24&0xff,
            self._localInt>>16&0xff,
            self._localInt>>8&0xff,
            self._localInt&0xff
            ]
        
        if self._localInt == 0xffffffffffffffff:
            self._localInt = 0
        else:
            self._localInt = self._localInt + 1

        return self.__getDecryptionKey(
            privKey, snmpEngineBoots, snmpEngineTime, salt
            ) + ( univ.OctetString(salt).asOctets(), ) 
Example #23
Source File: aes.py    From scalyr-agent-2 with Apache License 2.0 6 votes vote down vote up
def __getDecryptionKey(self, privKey, snmpEngineBoots,
                           snmpEngineTime, salt):
        snmpEngineBoots, snmpEngineTime, salt = (
            int(snmpEngineBoots), int(snmpEngineTime), salt
            )

        iv = [ snmpEngineBoots>>24&0xff,
               snmpEngineBoots>>16&0xff,
               snmpEngineBoots>>8&0xff,
               snmpEngineBoots&0xff,
               snmpEngineTime>>24&0xff,
               snmpEngineTime>>16&0xff,
               snmpEngineTime>>8&0xff,
               snmpEngineTime&0xff ] + salt

        return privKey[:self.keySize].asOctets(), univ.OctetString(iv).asOctets() 
Example #24
Source File: des.py    From scalyr-agent-2 with Apache License 2.0 6 votes vote down vote up
def __getEncryptionKey(self, privKey, snmpEngineBoots):
        desKey = privKey[:8]
        preIV = privKey[8:16]

        securityEngineBoots = int(snmpEngineBoots)

        salt = [
            securityEngineBoots>>24&0xff,
            securityEngineBoots>>16&0xff,
            securityEngineBoots>>8&0xff,
            securityEngineBoots&0xff,
            self._localInt>>24&0xff,
            self._localInt>>16&0xff,
            self._localInt>>8&0xff,
            self._localInt&0xff
            ]
        if self._localInt == 0xffffffff:
            self._localInt = 0
        else:
            self._localInt = self._localInt + 1

        return desKey.asOctets(), \
               univ.OctetString(salt).asOctets(), \
               univ.OctetString(map(lambda x,y:x^y,salt,preIV.asNumbers())).asOctets() 
Example #25
Source File: localkey.py    From scalyr-agent-2 with Apache License 2.0 6 votes vote down vote up
def hashPassphraseMD5(passphrase):
    passphrase = univ.OctetString(passphrase).asOctets()
    md = md5()
    ringBuffer = passphrase * (passphrase and (64//len(passphrase)+1) or 1)
    ringBufferLen = len(ringBuffer)
    count = 0
    mark = 0
    while count < 16384:
        e = mark + 64
        if e < ringBufferLen:
            md.update(ringBuffer[mark:e])
            mark = e
        else:
            md.update(
                ringBuffer[mark:ringBufferLen] + ringBuffer[0:e-ringBufferLen]
                )
            mark = e-ringBufferLen
        count = count + 1
    return md.digest() 
Example #26
Source File: localkey.py    From scalyr-agent-2 with Apache License 2.0 6 votes vote down vote up
def hashPassphraseSHA(passphrase):
    passphrase = univ.OctetString(passphrase).asOctets()    
    md = sha1()
    ringBuffer = passphrase * (64//len(passphrase)+1)
    ringBufferLen = len(ringBuffer)
    count = 0
    mark = 0
    while count < 16384:
        e = mark + 64
        if e < ringBufferLen:
            md.update(ringBuffer[mark:e])
            mark = e
        else:
            md.update(
                ringBuffer[mark:ringBufferLen] + ringBuffer[0:e-ringBufferLen]
                )
            mark = e-ringBufferLen
        count = count + 1
    return md.digest() 
Example #27
Source File: securityblob.py    From addon with GNU General Public License v3.0 6 votes vote down vote up
def generateNegotiateSecurityBlob(ntlm_data):
    mech_token = univ.OctetString(ntlm_data).subtype(explicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 2))
    mech_types = MechTypeList().subtype(explicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatConstructed, 0))
    mech_types.setComponentByPosition(0, univ.ObjectIdentifier('1.3.6.1.4.1.311.2.2.10'))

    n = NegTokenInit().subtype(explicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatConstructed, 0))
    n.setComponentByName('mechTypes', mech_types)
    n.setComponentByName('mechToken', mech_token)

    nt = NegotiationToken()
    nt.setComponentByName('negTokenInit', n)

    ct = ContextToken()
    ct.setComponentByName('thisMech', univ.ObjectIdentifier('1.3.6.1.5.5.2'))
    ct.setComponentByName('innerContextToken', nt)

    return encoder.encode(ct) 
Example #28
Source File: char.py    From luci-py with Apache License 2.0 6 votes vote down vote up
def prettyIn(self, value):
            try:
                if isinstance(value, unicode):
                    return value
                elif isinstance(value, str):
                    return value.decode(self.encoding)
                elif isinstance(value, (tuple, list)):
                    return self.prettyIn(''.join([chr(x) for x in value]))
                elif isinstance(value, univ.OctetString):
                    return value.asOctets().decode(self.encoding)
                else:
                    return unicode(value)

            except (UnicodeDecodeError, LookupError):
                raise error.PyAsn1Error(
                    "Can't decode string '%s' with codec %s" % (value, self.encoding)
                ) 
Example #29
Source File: char.py    From luci-py with Apache License 2.0 6 votes vote down vote up
def prettyIn(self, value):
            try:
                if isinstance(value, str):
                    return value
                elif isinstance(value, bytes):
                    return value.decode(self.encoding)
                elif isinstance(value, (tuple, list)):
                    return self.prettyIn(bytes(value))
                elif isinstance(value, univ.OctetString):
                    return value.asOctets().decode(self.encoding)
                else:
                    return str(value)

            except (UnicodeDecodeError, LookupError):
                raise error.PyAsn1Error(
                    "Can't decode string '%s' with codec %s" % (value, self.encoding)
                ) 
Example #30
Source File: securityblob.py    From pelisalacarta-ce with GNU General Public License v3.0 6 votes vote down vote up
def generateNegotiateSecurityBlob(ntlm_data):
    mech_token = univ.OctetString(ntlm_data).subtype(explicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 2))
    mech_types = MechTypeList().subtype(explicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatConstructed, 0))
    mech_types.setComponentByPosition(0, univ.ObjectIdentifier('1.3.6.1.4.1.311.2.2.10'))

    n = NegTokenInit().subtype(explicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatConstructed, 0))
    n.setComponentByName('mechTypes', mech_types)
    n.setComponentByName('mechToken', mech_token)

    nt = NegotiationToken()
    nt.setComponentByName('negTokenInit', n)

    ct = ContextToken()
    ct.setComponentByName('thisMech', univ.ObjectIdentifier('1.3.6.1.5.5.2'))
    ct.setComponentByName('innerContextToken', nt)

    return encoder.encode(ct)