Ejemplo n.º 1
0
Archivo: cert.py Proyecto: 6WIND/scapy
    def fill_and_store(self, modulus=None, modulusLen=None, pubExp=None,
                             prime1=None, prime2=None, coefficient=None,
                             exponent1=None, exponent2=None, privExp=None):
        pubExp = pubExp or 65537
        if None in [modulus, prime1, prime2, coefficient, privExp,
                    exponent1, exponent2]:
            # note that the library requires every parameter
            # in order to call RSAPrivateNumbers(...)
            # if one of these is missing, we generate a whole new key
            real_modulusLen = modulusLen or 2048
            self.key = rsa.generate_private_key(public_exponent=pubExp,
                                                key_size=real_modulusLen,
                                                backend=default_backend())
            self.pubkey = self.key.public_key()
        else:
            real_modulusLen = len(binrepr(modulus))
            if modulusLen and real_modulusLen != modulusLen:
                warning("modulus and modulusLen do not match!")
            pubNum = rsa.RSAPublicNumbers(n=modulus, e=pubExp)
            privNum = rsa.RSAPrivateNumbers(p=prime1, q=prime2,
                                            dmp1=exponent1, dmq1=exponent2,
                                            iqmp=coefficient, d=privExp,
                                            public_numbers=pubNum)
            self.key = privNum.private_key(default_backend())
            self.pubkey = self.key.public_key()

        # Lines below are only useful for the legacy part of pkcs1.py
        pubNum = self.pubkey.public_numbers()
        self._modulusLen = real_modulusLen
        self._modulus = pubNum.n
        self._pubExp = pubNum.e
Ejemplo n.º 2
0
 def do_dec(cls,
            s,  # type: bytes
            context=None,  # type: Optional[Type[ASN1_Class]]
            safe=False  # type: bool
            ):
     # type: (...) -> Tuple[ASN1_Object[str], bytes]
     # /!\ the unused_bits information is lost after this decoding
     l, s, t = cls.check_type_check_len(s)
     if len(s) > 0:
         unused_bits = orb(s[0])
         if safe and unused_bits > 7:
             raise BER_Decoding_Error(
                 "BERcodec_BIT_STRING: too many unused_bits advertised",
                 remaining=s
             )
         fs = "".join(binrepr(orb(x)).zfill(8) for x in s[1:])
         if unused_bits > 0:
             fs = fs[:-unused_bits]
         return cls.tag.asn1_object(fs), t
     else:
         raise BER_Decoding_Error(
             "BERcodec_BIT_STRING found no content "
             "(not even unused_bits byte)",
             remaining=s
         )
Ejemplo n.º 3
0
 def __setattr__(self, name, value):
     if name == "val_readable":
         if isinstance(value, str):
             val = "".join(binrepr(ord(x)).zfill(8) for x in value)
         else:
             val = "<invalid val_readable>"
         super(ASN1_Object, self).__setattr__("val", val)
         super(ASN1_Object, self).__setattr__(name, value)
         super(ASN1_Object, self).__setattr__("unused_bits", 0)
     elif name == "val":
         if isinstance(value, str):
             if len([c for c in value if c not in ["0", "1"]]) > 0:
                 print "Invalid operation: 'val' is not a valid bit string."
                 return
             else:
                 if len(value) % 8 == 0:
                     unused_bits = 0
                 else:
                     unused_bits = 8 - (len(value) % 8)
                 padded_value = value + ("0" * unused_bits)
                 bytes_arr = zip(*[iter(padded_value)] * 8)
                 val_readable = "".join(
                     chr(int("".join(x), 2)) for x in bytes_arr)
         else:
             val_readable = "<invalid val>"
             unused_bits = 0
         super(ASN1_Object, self).__setattr__("val_readable", val_readable)
         super(ASN1_Object, self).__setattr__(name, value)
         super(ASN1_Object, self).__setattr__("unused_bits", unused_bits)
     elif name == "unused_bits":
         print "Invalid operation: unused_bits rewriting is not supported."
     else:
         super(ASN1_Object, self).__setattr__(name, value)
Ejemplo n.º 4
0
 def __init__(self, name, default, default_readable=True, context=None,
              implicit_tag=None, explicit_tag=None):
     if default is not None and default_readable:
         default = "".join(binrepr(ord(x)).zfill(8) for x in default)
     ASN1F_field.__init__(self, name, default, context=context,
                          implicit_tag=implicit_tag,
                          explicit_tag=explicit_tag)
Ejemplo n.º 5
0
 def __init__(self, name, default, default_readable=True, context=None,
              implicit_tag=None, explicit_tag=None):
     if default is not None and default_readable:
         default = b"".join(binrepr(orb(x)).zfill(8).encode("utf8") for x in default)
     ASN1F_field.__init__(self, name, default, context=context,
                          implicit_tag=implicit_tag,
                          explicit_tag=explicit_tag)
Ejemplo n.º 6
0
 def i2m(self, pkt, x):
     if x is None:
         s = b""
     else:
         s = raw(x)
     s = b"".join(binrepr(orb(x)).zfill(8).encode("utf8") for x in s)
     return ASN1F_BIT_STRING.i2m(self, pkt, s)
Ejemplo n.º 7
0
 def i2m(self, pkt, x):
     if x is None:
         s = ""
     else:
         s = str(x)
     s = "".join(binrepr(ord(x)).zfill(8) for x in s)
     return ASN1F_BIT_STRING.i2m(self, pkt, s)
Ejemplo n.º 8
0
 def __init__(self, name, default, default_readable=True, context=None,
              implicit_tag=None, explicit_tag=None):
     if default is not None and default_readable:
         default = b"".join(binrepr(orb(x)).zfill(8).encode("utf8") for x in default)  # noqa: E501
     ASN1F_field.__init__(self, name, default, context=context,
                          implicit_tag=implicit_tag,
                          explicit_tag=explicit_tag)
Ejemplo n.º 9
0
 def __setattr__(self, name, value):
     if name == "val_readable":
         if isinstance(value, str):
             val = "".join(binrepr(ord(x)).zfill(8) for x in value)
         else:
             val = "<invalid val_readable>"
         super(ASN1_Object, self).__setattr__("val", val)
         super(ASN1_Object, self).__setattr__(name, value)
         super(ASN1_Object, self).__setattr__("unused_bits", 0)
     elif name == "val":
         if isinstance(value, str):
             if len([c for c in value if c not in ["0", "1"]]) > 0:
                 print "Invalid operation: 'val' is not a valid bit string."
                 return
             else:
                 if len(value) % 8 == 0:
                     unused_bits = 0
                 else:
                     unused_bits = 8 - (len(value) % 8)
                 padded_value = value + ("0" * unused_bits)
                 bytes_arr = zip(*[iter(padded_value)]*8)
                 val_readable = "".join(chr(int("".join(x),2)) for x in bytes_arr)
         else:
             val_readable = "<invalid val>"
             unused_bits = 0
         super(ASN1_Object, self).__setattr__("val_readable", val_readable)
         super(ASN1_Object, self).__setattr__(name, value)
         super(ASN1_Object, self).__setattr__("unused_bits", unused_bits)
     elif name == "unused_bits":
         print "Invalid operation: unused_bits rewriting is not supported."
     else:
         super(ASN1_Object, self).__setattr__(name, value)
Ejemplo n.º 10
0
 def i2m(self, pkt, x):
     if x is None:
         s = b""
     else:
         s = raw(x)
     s = b"".join(binrepr(orb(x)).zfill(8).encode("utf8") for x in s)
     return ASN1F_BIT_STRING.i2m(self, pkt, s)
Ejemplo n.º 11
0
 def i2m(self, pkt, x):
     if x is None:
         s = ""
     else:
         s = str(x)
     s = "".join(binrepr(ord(x)).zfill(8) for x in s)
     return ASN1F_BIT_STRING.i2m(self, pkt, s)
Ejemplo n.º 12
0
Archivo: cert.py Proyecto: qbsonn/scapy
    def fill_and_store(self, modulus=None, modulusLen=None, pubExp=None,
                       prime1=None, prime2=None, coefficient=None,
                       exponent1=None, exponent2=None, privExp=None):
        pubExp = pubExp or 65537
        if None in [modulus, prime1, prime2, coefficient, privExp,
                    exponent1, exponent2]:
            # note that the library requires every parameter
            # in order to call RSAPrivateNumbers(...)
            # if one of these is missing, we generate a whole new key
            real_modulusLen = modulusLen or 2048
            self.key = rsa.generate_private_key(public_exponent=pubExp,
                                                key_size=real_modulusLen,
                                                backend=default_backend())
            self.pubkey = self.key.public_key()
        else:
            real_modulusLen = len(binrepr(modulus))
            if modulusLen and real_modulusLen != modulusLen:
                warning("modulus and modulusLen do not match!")
            pubNum = rsa.RSAPublicNumbers(n=modulus, e=pubExp)
            privNum = rsa.RSAPrivateNumbers(p=prime1, q=prime2,
                                            dmp1=exponent1, dmq1=exponent2,
                                            iqmp=coefficient, d=privExp,
                                            public_numbers=pubNum)
            self.key = privNum.private_key(default_backend())
            self.pubkey = self.key.public_key()

        # Lines below are only useful for the legacy part of pkcs1.py
        pubNum = self.pubkey.public_numbers()
        self._modulusLen = real_modulusLen
        self._modulus = pubNum.n
        self._pubExp = pubNum.e
Ejemplo n.º 13
0
 def updateWith(self, pubkey):
     self.modulus = pubkey.modulus.val
     self.modulusLen = len(binrepr(pubkey.modulus.val))
     self.pubExp = pubkey.publicExponent.val
     self.key = RSA.construct((
         self.modulus,
         self.pubExp,
     ))
Ejemplo n.º 14
0
 def updateWith(self, privkey):
     self.modulus     = privkey.modulus.val
     self.modulusLen  = len(binrepr(privkey.modulus.val))
     self.pubExp      = privkey.publicExponent.val
     self.privExp     = privkey.privateExponent.val
     self.prime1      = privkey.prime1.val
     self.prime2      = privkey.prime2.val
     self.exponent1   = privkey.exponent1.val
     self.exponent2   = privkey.exponent2.val
     self.coefficient = privkey.coefficient.val
     self.key = RSA.construct((self.modulus, self.pubExp, self.privExp))
Ejemplo n.º 15
0
 def updateWith(self, privkey):
     self.modulus = privkey.modulus.val
     self.modulusLen = len(binrepr(privkey.modulus.val))
     self.pubExp = privkey.publicExponent.val
     self.privExp = privkey.privateExponent.val
     self.prime1 = privkey.prime1.val
     self.prime2 = privkey.prime2.val
     self.exponent1 = privkey.exponent1.val
     self.exponent2 = privkey.exponent2.val
     self.coefficient = privkey.coefficient.val
     self.key = RSA.construct((self.modulus, self.pubExp, self.privExp))
Ejemplo n.º 16
0
Archivo: ber.py Proyecto: mtury/scapy
 def do_dec(cls, s, context=None, safe=False):
     # /!\ the unused_bits information is lost after this decoding
     l, s, t = cls.check_type_check_len(s)
     if len(s) > 0:
         unused_bits = ord(s[0])
         if safe and unused_bits > 7:
             raise BER_Decoding_Error("BERcodec_BIT_STRING: too many unused_bits advertised", remaining=s)
         s = "".join(binrepr(ord(x)).zfill(8) for x in s[1:])
         if unused_bits > 0:
             s = s[:-unused_bits]
         return cls.tag.asn1_object(s), t
     else:
         raise BER_Decoding_Error("BERcodec_BIT_STRING found no content (not even unused_bits byte)", remaining=s)
Ejemplo n.º 17
0
 def do_dec(cls, s, context=None, safe=False):
     # /!\ the unused_bits information is lost after this decoding
     l, s, t = cls.check_type_check_len(s)
     if len(s) > 0:
         unused_bits = orb(s[0])
         if safe and unused_bits > 7:
             raise BER_Decoding_Error("BERcodec_BIT_STRING: too many unused_bits advertised", remaining=s)
         s = "".join(binrepr(orb(x)).zfill(8) for x in s[1:])
         if unused_bits > 0:
             s = s[:-unused_bits]
         return cls.tag.asn1_object(s), t
     else:
         raise BER_Decoding_Error("BERcodec_BIT_STRING found no content (not even unused_bits byte)", remaining=s)
Ejemplo n.º 18
0
 def __init__(self, val, readable=False):
     if readable:
         self.val_readable = val
         val = "".join(binrepr(ord(x)).zfill(8) for x in val)
         self.unused_bits = 0
     else:
         if len(val) % 8 == 0:
             self.unused_bits = 0
         else:
             self.unused_bits = 8 - len(val) % 8
         padded_val = val + "0" * self.unused_bits
         bytes_arr = zip(*[iter(padded_val)] * 8)
         self.val_readable = "".join(chr(int("".join(x), 2)) for x in bytes_arr)
     ASN1_Object.__init__(self, val)
Ejemplo n.º 19
0
def encode_point(point, point_format=0):
    """
    Return a string representation of the Point p, according to point_format.
    """
    pLen = len(binrepr(point.curve().p()))
    x = pkcs_i2osp(point.x(), math.ceil(pLen / 8))
    y = pkcs_i2osp(point.y(), math.ceil(pLen / 8))
    if point_format == 0:
        frmt = '\x04'
    elif point_format == 1:
        frmt = chr(2 + y % 2)
        y = ''
    else:
        raise Exception("No support for point_format %d" % point_format)
    return frmt + x + y
Ejemplo n.º 20
0
def encode_point(point, point_format=0):
    """
    Return a string representation of the Point p, according to point_format.
    """
    pLen = len(binrepr(point.curve().p()))
    x = pkcs_i2osp(point.x(), math.ceil(pLen/8))
    y = pkcs_i2osp(point.y(), math.ceil(pLen/8))
    if point_format == 0:
        frmt = '\x04'
    elif point_format == 1:
        frmt = chr(2 + y%2)
        y = ''
    else:
        raise Exception("No support for point_format %d" % point_format)
    return frmt + x + y
Ejemplo n.º 21
0
 def __init__(self, val, readable=False):
     if readable:
         self.val_readable = val
         val = "".join(binrepr(ord(x)).zfill(8) for x in val)
         self.unused_bits = 0
     else:
         if len(val) % 8 == 0:
             self.unused_bits = 0
         else:
             self.unused_bits = 8 - len(val) % 8
         padded_val = val + "0" * self.unused_bits
         bytes_arr = zip(*[iter(padded_val)] * 8)
         self.val_readable = "".join(
             chr(int("".join(x), 2)) for x in bytes_arr)
     ASN1_Object.__init__(self, val)
Ejemplo n.º 22
0
 def updateWith(self, privkey):
     self.modulus = privkey.modulus.val
     self.modulusLen = len(binrepr(privkey.modulus.val))
     self.pubExp = privkey.publicExponent.val
     self.privExp = privkey.privateExponent.val
     self.prime1 = privkey.prime1.val
     self.prime2 = privkey.prime2.val
     self.exponent1 = privkey.exponent1.val
     self.exponent2 = privkey.exponent2.val
     self.coefficient = privkey.coefficient.val
     self.key = rsa.RSAPrivateNumbers(
         p=self.prime1,
         q=self.prime2,
         d=self.privExp,
         dmp1=self.exponent1,
         dmq1=self.exponent2,
         iqmp=self.coefficient,
         public_numbers=rsa.RSAPublicNumbers(n=self.modulus, e=self.pubExp),
     ).private_key(default_backend())
Ejemplo n.º 23
0
 def fill_and_store(self, modulus=None, modulusLen=None, pubExp=None):
     pubExp = pubExp or 65537
     if modulus is None:
         real_modulusLen = modulusLen or 2048
         private_key = rsa.generate_private_key(public_exponent=pubExp,
                                                key_size=real_modulusLen,
                                                backend=default_backend())
         self.pubkey = private_key.public_key()
     else:
         real_modulusLen = len(binrepr(modulus))
         if real_modulusLen != modulusLen:
             warning("modulus and modulusLen do not match!")
         pubNum = rsa.RSAPublicNumbers(n=modulus, e=pubExp)
         self.pubkey = pubNum.public_key(default_backend())
     #XXX lines below should be removed once pkcs1.py is cleaned of legacy
     pubNum = self.pubkey.public_numbers()
     self._modulusLen = real_modulusLen
     self._modulus = pubNum.n
     self._pubExp = pubNum.e
Ejemplo n.º 24
0
Archivo: cert.py Proyecto: qbsonn/scapy
 def fill_and_store(self, modulus=None, modulusLen=None, pubExp=None):
     pubExp = pubExp or 65537
     if not modulus:
         real_modulusLen = modulusLen or 2048
         private_key = rsa.generate_private_key(public_exponent=pubExp,
                                                key_size=real_modulusLen,
                                                backend=default_backend())
         self.pubkey = private_key.public_key()
     else:
         real_modulusLen = len(binrepr(modulus))
         if modulusLen and real_modulusLen != modulusLen:
             warning("modulus and modulusLen do not match!")
         pubNum = rsa.RSAPublicNumbers(n=modulus, e=pubExp)
         self.pubkey = pubNum.public_key(default_backend())
     # Lines below are only useful for the legacy part of pkcs1.py
     pubNum = self.pubkey.public_numbers()
     self._modulusLen = real_modulusLen
     self._modulus = pubNum.n
     self._pubExp = pubNum.e
Ejemplo n.º 25
0
Archivo: cert.py Proyecto: 6WIND/scapy
 def fill_and_store(self, modulus=None, modulusLen=None, pubExp=None):
     pubExp = pubExp or 65537
     if modulus is None:
         real_modulusLen = modulusLen or 2048
         private_key = rsa.generate_private_key(public_exponent=pubExp,
                                                key_size=real_modulusLen,
                                                backend=default_backend())
         self.pubkey = private_key.public_key()
     else:
         real_modulusLen = len(binrepr(modulus))
         if modulusLen and real_modulusLen != modulusLen:
             warning("modulus and modulusLen do not match!")
         pubNum = rsa.RSAPublicNumbers(n=modulus, e=pubExp)
         self.pubkey = pubNum.public_key(default_backend())
     # Lines below are only useful for the legacy part of pkcs1.py
     pubNum = self.pubkey.public_numbers()
     self._modulusLen = real_modulusLen
     self._modulus = pubNum.n
     self._pubExp = pubNum.e
Ejemplo n.º 26
0
 def __setattr__(self, name, value):
     str_value = None
     if isinstance(value, str):
         str_value = value
         value = raw(value)
     if name == "val_readable":
         if isinstance(value, bytes):
             val = b"".join(
                 binrepr(orb(x)).zfill(8).encode("utf8")
                 for x in value)  # noqa: E501
         else:
             val = "<invalid val_readable>"
         super(ASN1_Object, self).__setattr__("val", val)
         super(ASN1_Object, self).__setattr__(name, value)
         super(ASN1_Object, self).__setattr__("unused_bits", 0)
     elif name == "val":
         if not str_value:
             str_value = plain_str(value)
         if isinstance(value, bytes):
             if any(c for c in str_value if c not in ["0", "1"]):
                 print("Invalid operation: 'val' is not a valid bit string."
                       )  # noqa: E501
                 return
             else:
                 if len(value) % 8 == 0:
                     unused_bits = 0
                 else:
                     unused_bits = 8 - (len(value) % 8)
                 padded_value = str_value + ("0" * unused_bits)
                 bytes_arr = zip(*[iter(padded_value)] * 8)
                 val_readable = b"".join(
                     chb(int("".join(x), 2))
                     for x in bytes_arr)  # noqa: E501
         else:
             val_readable = "<invalid val>"
             unused_bits = 0
         super(ASN1_Object, self).__setattr__("val_readable", val_readable)
         super(ASN1_Object, self).__setattr__(name, value)
         super(ASN1_Object, self).__setattr__("unused_bits", unused_bits)
     elif name == "unused_bits":
         print("Invalid operation: unused_bits rewriting is not supported.")
     else:
         super(ASN1_Object, self).__setattr__(name, value)
Ejemplo n.º 27
0
 def fill_and_store(self, modulus=None, modulusLen=None, pubExp=None):
     pubExp = pubExp or 65537
     if modulus is None:
         real_modulusLen = modulusLen or 2048
         private_key = rsa.generate_private_key(public_exponent=pubExp,
                                                key_size=real_modulusLen,
                                                backend=default_backend())
         self.pubkey = private_key.public_key()
     else:
         real_modulusLen = len(binrepr(modulus))
         if real_modulusLen != modulusLen:
             warning("modulus and modulusLen do not match!")
         pubNum = rsa.RSAPublicNumbers(n=modulus, e=pubExp)
         self.pubkey = pubNum.public_key(default_backend())
     #XXX lines below should be removed once pkcs1.py is cleaned of legacy
     pubNum = self.pubkey.public_numbers()
     self._modulusLen = real_modulusLen
     self._modulus = pubNum.n
     self._pubExp = pubNum.e
Ejemplo n.º 28
0
 def __setattr__(self, name, value):
     # type: (str, Any) -> None
     if name == "val_readable":
         if isinstance(value, (str, bytes)):
             val = "".join(binrepr(orb(x)).zfill(8) for x in value)
         else:
             warning("Invalid val: should be bytes")
             val = "<invalid val_readable>"
         object.__setattr__(self, "val", val)
         object.__setattr__(self, name, bytes_encode(value))
         object.__setattr__(self, "unused_bits", 0)
     elif name == "val":
         value = plain_str(value)
         if isinstance(value, str):
             if any(c for c in value if c not in ["0", "1"]):
                 warning(
                     "Invalid operation: 'val' is not a valid bit string."
                 )  # noqa: E501
                 return
             else:
                 if len(value) % 8 == 0:
                     unused_bits = 0
                 else:
                     unused_bits = 8 - (len(value) % 8)
                 padded_value = value + ("0" * unused_bits)
                 bytes_arr = zip(*[iter(padded_value)] * 8)
                 val_readable = b"".join(
                     chb(int("".join(x), 2))
                     for x in bytes_arr)  # noqa: E501
         else:
             warning("Invalid val: should be str")
             val_readable = b"<invalid val>"
             unused_bits = 0
         object.__setattr__(self, "val_readable", val_readable)
         object.__setattr__(self, name, value)
         object.__setattr__(self, "unused_bits", unused_bits)
     elif name == "unused_bits":
         warning("Invalid operation: unused_bits rewriting "
                 "is not supported.")
     else:
         object.__setattr__(self, name, value)
Ejemplo n.º 29
0
 def __setattr__(self, name, value):
     if isinstance(value, str):
         value = bytes_encode(value)
     if name == "val_readable":
         if isinstance(value, bytes):
             val = b"".join(
                 binrepr(orb(x)).zfill(8).encode("utf8")
                 for x in value)  # noqa: E501
         else:
             val = "<invalid val_readable>"
         super(ASN1_Object, self).__setattr__("val", val)
         super(ASN1_Object, self).__setattr__(name, value)
         super(ASN1_Object, self).__setattr__("unused_bits", 0)
     elif name == "val":
         if isinstance(value, bytes):
             if any(True for x in range(len(value))
                    if value[x:x + 1] not in [b"0", b"1"]):
                 print("Invalid operation: 'val' is not a valid bit string."
                       )  # noqa: E501
                 return
             else:
                 if len(value) % 8 == 0:
                     unused_bits = 0
                 else:
                     unused_bits = 8 - (len(value) % 8)
                 padded_value = value + (b"0" * unused_bits)
                 bytes_arr = zip(*[(padded_value[i:i + 1]
                                    for i in range(len(padded_value)))] * 8)
                 val_readable = b"".join(
                     chb(int(b"".join(x), 2)) for x in bytes_arr)
         else:
             val_readable = "<invalid val>"
             unused_bits = 0
         super(ASN1_Object, self).__setattr__("val_readable", val_readable)
         super(ASN1_Object, self).__setattr__(name, value)
         super(ASN1_Object, self).__setattr__("unused_bits", unused_bits)
     elif name == "unused_bits":
         print("Invalid operation: unused_bits rewriting is not supported.")
     else:
         super(ASN1_Object, self).__setattr__(name, value)
Ejemplo n.º 30
0
 def __setattr__(self, name, value):
     str_value = None
     if isinstance(value, str):
         str_value = value
         value = raw(value)
     if name == "val_readable":
         if isinstance(value, bytes):
             val = b"".join(binrepr(orb(x)).zfill(8).encode("utf8") for x in value)  # noqa: E501
         else:
             val = "<invalid val_readable>"
         super(ASN1_Object, self).__setattr__("val", val)
         super(ASN1_Object, self).__setattr__(name, value)
         super(ASN1_Object, self).__setattr__("unused_bits", 0)
     elif name == "val":
         if not str_value:
             str_value = plain_str(value)
         if isinstance(value, bytes):
             if any(c for c in str_value if c not in ["0", "1"]):
                 print("Invalid operation: 'val' is not a valid bit string.")  # noqa: E501
                 return
             else:
                 if len(value) % 8 == 0:
                     unused_bits = 0
                 else:
                     unused_bits = 8 - (len(value) % 8)
                 padded_value = str_value + ("0" * unused_bits)
                 bytes_arr = zip(*[iter(padded_value)] * 8)
                 val_readable = b"".join(chb(int("".join(x), 2)) for x in bytes_arr)  # noqa: E501
         else:
             val_readable = "<invalid val>"
             unused_bits = 0
         super(ASN1_Object, self).__setattr__("val_readable", val_readable)
         super(ASN1_Object, self).__setattr__(name, value)
         super(ASN1_Object, self).__setattr__("unused_bits", unused_bits)
     elif name == "unused_bits":
         print("Invalid operation: unused_bits rewriting is not supported.")
     else:
         super(ASN1_Object, self).__setattr__(name, value)
Ejemplo n.º 31
0
    def post_build(self, p, pay):
        # first 10 bits of an ofp_match are always set to 0
        l = "0"*10

        # when one field has not been declared, it is assumed to be wildcarded
        if self.wildcards1 is None:
            if self.nw_tos is None:
                l += "1"
            else:
                l += "0"
            if self.dl_vlan_pcp is None:
                l += "1"
            else:
                l += "0"
        else:
            w1 = binrepr(self.wildcards1)
            l += "0"*(2-len(w1))
            l += w1

        # ip masks use 6 bits each
        if self.nw_dst_mask is None:
            if self.nw_dst is "0":
                l += "111111"
            # 0x100000 would be ok too (32-bit IP mask)
            else:
                l += "0"*6
        else:
            m1 = binrepr(self.nw_dst_mask)
            l += "0"*(6-len(m1))
            l += m1
        if self.nw_src_mask is None:
            if self.nw_src is "0":
                l += "111111"
            else:
                l += "0"*6
        else:
            m2 = binrepr(self.nw_src_mask)
            l += "0"*(6-len(m2))
            l += m2

        # wildcards2 works the same way as wildcards1
        if self.wildcards2 is None:
            if self.tp_dst is None:
                l += "1"
            else:
                l += "0"
            if self.tp_src is None:
                l += "1"
            else:
                l += "0"
            if self.nw_proto is None:
                l += "1"
            else:
                l += "0"
            if self.dl_type is None:
                l += "1"
            else:
                l += "0"
            if self.dl_dst is None:
                l += "1"
            else:
                l += "0"
            if self.dl_src is None:
                l += "1"
            else:
                l += "0"
            if self.dl_vlan is None:
                l += "1"
            else:
                l += "0"
            if self.in_port is None:
                l += "1"
            else:
                l += "0"
        else:
            w2 = binrepr(self.wildcards2)
            l += "0"*(8-len(w2))
            l += w2

        # In order to write OFPMatch compliant with the specifications,
        # if prereq_autocomplete has been set to True
        # we assume ethertype=IP or nwproto=TCP when appropriate subfields are
        # provided.
        if prereq_autocomplete:
            if self.dl_type is None:
                if self.nw_src is not "0" or self.nw_dst is not "0" or self.nw_proto is not None or self.nw_tos is not None:
                    p = p[:22] + struct.pack("!H", 0x0800) + p[24:]
                    l = l[:-5] + "0" + l[-4:]
            if self.nw_proto is None:
                if self.tp_src is not None or self.tp_dst is not None:
                    p = p[:22] + struct.pack("!H", 0x0800) + p[24:]
                    l = l[:-5] + "0" + l[-4:]
                    p = p[:25] + struct.pack("!B", 0x06) + p[26:]
                    l = l[:-6] + "0" + l[-5:]

        ins = "".join(chr(int("".join(x), 2)) for x in zip(*[iter(l)]*8))
        p = ins + p[4:]
        return p + pay
Ejemplo n.º 32
0
 def updateWith(self, pubkey):
     self.modulus = pubkey.modulus.val
     self.modulusLen = len(binrepr(pubkey.modulus.val))
     self.pubExp = pubkey.publicExponent.val
     self.key = rsa.RSAPublicNumbers(
         n=self.modulus, e=self.pubExp).public_key(default_backend())
Ejemplo n.º 33
0
 def updateWith(self, pubkey):
     self.modulus    = pubkey.modulus.val
     self.modulusLen = len(binrepr(pubkey.modulus.val))
     self.pubExp     = pubkey.publicExponent.val
     self.key = RSA.construct((self.modulus, self.pubExp, ))