def test_decode_with_missing_data(self):
        data = bytearray(b'\x04'
                         b'\x00\x55\x43\x89\x4a\xf3\xd0\x0e\xd7\xd7'
                         b'\x40\xab\xdb\xd7\x5c\x96\xb0\x68\x77\xb7'
                         b'\x87\xdb\x5f\x70\xee\xa7\x8b\x90\xa8\xd7'
                         b'\xc0\x0a'
                         b'\xbb\x4c\x85\xa3\xd8\xea\x29\xef\xaa\xfa'
                         b'\x24\x40\x69\x12\xdd\x84\xd5\xb1\x4d\xc3'
                         b'\x2b\xf6\x56\xef\x6c\x6b\xd5\x8a\x5d\x94'
                         #b'\x3f\x92'
                         )

        # XXX will change later as decoder in tlslite-ng needs to be updated
        with self.assertRaises(SyntaxError):
            decodeX962Point(data, ecdsa.NIST256p)
    def test_decode_with_missing_data(self):
        data = bytearray(b'\x04'
                         b'\x00\x55\x43\x89\x4a\xf3\xd0\x0e\xd7\xd7'
                         b'\x40\xab\xdb\xd7\x5c\x96\xb0\x68\x77\xb7'
                         b'\x87\xdb\x5f\x70\xee\xa7\x8b\x90\xa8\xd7'
                         b'\xc0\x0a'
                         b'\xbb\x4c\x85\xa3\xd8\xea\x29\xef\xaa\xfa'
                         b'\x24\x40\x69\x12\xdd\x84\xd5\xb1\x4d\xc3'
                         b'\x2b\xf6\x56\xef\x6c\x6b\xd5\x8a\x5d\x94'
                         #b'\x3f\x92'
                         )

        # XXX will change later as decoder in tlslite-ng needs to be updated
        with self.assertRaises(SyntaxError):
            decodeX962Point(data, ecdsa.NIST256p)
    def test_ECDHE_key_exchange(self):
        srv_key_ex = self.keyExchange.makeServerKeyExchange('sha1')

        KeyExchange.verifyServerKeyExchange(srv_key_ex,
                                            self.srv_pub_key,
                                            self.client_hello.random,
                                            self.server_hello.random,
                                            [(HashAlgorithm.sha1,
                                              SignatureAlgorithm.rsa)])

        curveName = GroupName.toStr(srv_key_ex.named_curve)
        curve = getCurveByName(curveName)
        generator = curve.generator
        cln_Xc = ecdsa.util.randrange(generator.order())
        cln_Ys = decodeX962Point(srv_key_ex.ecdh_Ys, curve)
        cln_Yc = encodeX962Point(generator * cln_Xc)

        cln_key_ex = ClientKeyExchange(self.cipher_suite, (3, 3))
        cln_key_ex.createECDH(cln_Yc)

        cln_S = cln_Ys * cln_Xc
        cln_premaster = numberToByteArray(cln_S.x(),
                                          getPointByteSize(cln_S))

        srv_premaster = self.keyExchange.processClientKeyExchange(cln_key_ex)

        self.assertEqual(cln_premaster, srv_premaster)
    def test_ECDHE_key_exchange(self):
        srv_key_ex = self.keyExchange.makeServerKeyExchange('sha1')

        KeyExchange.verifyServerKeyExchange(srv_key_ex,
                                            self.srv_pub_key,
                                            self.client_hello.random,
                                            self.server_hello.random,
                                            [(HashAlgorithm.sha1,
                                              SignatureAlgorithm.rsa)])

        curveName = GroupName.toStr(srv_key_ex.named_curve)
        curve = getCurveByName(curveName)
        generator = curve.generator
        cln_Xc = ecdsa.util.randrange(generator.order())
        cln_Ys = decodeX962Point(srv_key_ex.ecdh_Ys, curve)
        cln_Yc = encodeX962Point(generator * cln_Xc)

        cln_key_ex = ClientKeyExchange(self.cipher_suite, (3, 3))
        cln_key_ex.createECDH(cln_Yc)

        cln_S = cln_Ys * cln_Xc
        cln_premaster = numberToByteArray(cln_S.x(),
                                          getPointByteSize(cln_S))

        srv_premaster = self.keyExchange.processClientKeyExchange(cln_key_ex)

        self.assertEqual(cln_premaster, srv_premaster)
    def test_decode_P_256_point(self):
        point = ecdsa.NIST256p.generator * 379
        data = bytearray(b'\x04'
                         b'\x00\x55\x43\x89\x4a\xf3\xd0\x0e\xd7\xd7'
                         b'\x40\xab\xdb\xd7\x5c\x96\xb0\x68\x77\xb7'
                         b'\x87\xdb\x5f\x70\xee\xa7\x8b\x90\xa8\xd7'
                         b'\xc0\x0a'
                         b'\xbb\x4c\x85\xa3\xd8\xea\x29\xef\xaa\xfa'
                         b'\x24\x40\x69\x12\xdd\x84\xd5\xb1\x4d\xc3'
                         b'\x2b\xf6\x56\xef\x6c\x6b\xd5\x8a\x5d\x94'
                         b'\x3f\x92')

        decoded_point = decodeX962Point(data, ecdsa.NIST256p)

        self.assertEqual(point, decoded_point)
    def test_decode_P_256_point(self):
        point = ecdsa.NIST256p.generator * 379
        data = bytearray(b'\x04'
                         b'\x00\x55\x43\x89\x4a\xf3\xd0\x0e\xd7\xd7'
                         b'\x40\xab\xdb\xd7\x5c\x96\xb0\x68\x77\xb7'
                         b'\x87\xdb\x5f\x70\xee\xa7\x8b\x90\xa8\xd7'
                         b'\xc0\x0a'
                         b'\xbb\x4c\x85\xa3\xd8\xea\x29\xef\xaa\xfa'
                         b'\x24\x40\x69\x12\xdd\x84\xd5\xb1\x4d\xc3'
                         b'\x2b\xf6\x56\xef\x6c\x6b\xd5\x8a\x5d\x94'
                         b'\x3f\x92'
                         )

        decoded_point = decodeX962Point(data, ecdsa.NIST256p)

        self.assertEqual(point, decoded_point)
    def test_decode_P_521_point(self):

        data = bytearray(b'\x04'
                         b'\x01\x7d\x8a\x5d\x11\x03\x4a\xaf\x01\x26'
                         b'\x5f\x2d\xd6\x2d\x76\xeb\xd8\xbe\x4e\xfb'
                         b'\x3b\x4b\xd2\x05\x5a\xed\x4c\x6d\x20\xc7'
                         b'\xf3\xd7\x08\xab\x21\x9e\x34\xfd\x14\x56'
                         b'\x3d\x47\xd0\x02\x65\x15\xc2\xdd\x2d\x60'
                         b'\x66\xf9\x15\x64\x55\x7a\xae\x56\xa6\x7a'
                         b'\x28\x51\x65\x26\x5c\xcc'
                         b'\x01\xd4\x19\x56\xfa\x14\x6a\xdb\x83\x1c'
                         b'\xb6\x1a\xc4\x4b\x40\xb1\xcb\xcc\x9e\x4f'
                         b'\x57\x2c\xb2\x72\x70\xb9\xef\x38\x15\xae'
                         b'\x87\x1f\x85\x40\x94\xda\x69\xed\x97\xeb'
                         b'\xdc\x72\x25\x25\x61\x76\xb2\xde\xed\xa2'
                         b'\xb0\x5c\xca\xc4\x83\x8f\xfb\x54\xae\xe0'
                         b'\x07\x45\x0b\xbf\x7c\xfc')

        point = decodeX962Point(data, ecdsa.NIST521p)
        self.assertIsNotNone(point)

        self.assertEqual(encodeX962Point(point), data)
    def test_decode_P_521_point(self):

        data = bytearray(b'\x04'
                         b'\x01\x7d\x8a\x5d\x11\x03\x4a\xaf\x01\x26'
                         b'\x5f\x2d\xd6\x2d\x76\xeb\xd8\xbe\x4e\xfb'
                         b'\x3b\x4b\xd2\x05\x5a\xed\x4c\x6d\x20\xc7'
                         b'\xf3\xd7\x08\xab\x21\x9e\x34\xfd\x14\x56'
                         b'\x3d\x47\xd0\x02\x65\x15\xc2\xdd\x2d\x60'
                         b'\x66\xf9\x15\x64\x55\x7a\xae\x56\xa6\x7a'
                         b'\x28\x51\x65\x26\x5c\xcc'
                         b'\x01\xd4\x19\x56\xfa\x14\x6a\xdb\x83\x1c'
                         b'\xb6\x1a\xc4\x4b\x40\xb1\xcb\xcc\x9e\x4f'
                         b'\x57\x2c\xb2\x72\x70\xb9\xef\x38\x15\xae'
                         b'\x87\x1f\x85\x40\x94\xda\x69\xed\x97\xeb'
                         b'\xdc\x72\x25\x25\x61\x76\xb2\xde\xed\xa2'
                         b'\xb0\x5c\xca\xc4\x83\x8f\xfb\x54\xae\xe0'
                         b'\x07\x45\x0b\xbf\x7c\xfc')

        point = decodeX962Point(data, ecdsa.NIST521p)
        self.assertIsNotNone(point)

        self.assertEqual(encodeX962Point(point), data)
    def test_ECDHE_key_exchange_with_invalid_CKE(self):
        srv_key_ex = self.keyExchange.makeServerKeyExchange('sha1')

        KeyExchange.verifyServerKeyExchange(srv_key_ex,
                                            self.srv_pub_key,
                                            self.client_hello.random,
                                            self.server_hello.random,
                                            [(HashAlgorithm.sha1,
                                              SignatureAlgorithm.rsa)])

        curveName = GroupName.toStr(srv_key_ex.named_curve)
        curve = getCurveByName(curveName)
        generator = curve.generator
        cln_Xc = ecdsa.util.randrange(generator.order())
        cln_Ys = decodeX962Point(srv_key_ex.ecdh_Ys, curve)
        cln_Yc = encodeX962Point(generator * cln_Xc)

        cln_key_ex = ClientKeyExchange(self.cipher_suite, (3, 3))
        cln_key_ex.createECDH(cln_Yc)

        cln_key_ex.ecdh_Yc[-1] ^= 0x01

        with self.assertRaises(TLSIllegalParameterException):
            self.keyExchange.processClientKeyExchange(cln_key_ex)