summaryrefslogtreecommitdiff
path: root/M2Crypto/SSL/Cipher.py
blob: 06f2dbfde543d0acb2e2707fd54a55e52b22e44a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
"""SSL Ciphers

Copyright (c) 1999-2003 Ng Pheng Siong. All rights reserved."""

__all__ = ['Cipher', 'Cipher_Stack']

from M2Crypto import m2, six
from typing import Iterable  # noqa


class Cipher(object):
    def __init__(self, cipher):
        # type: (str) -> None
        self.cipher = cipher

    def __len__(self):
        # type: () -> int
        return m2.ssl_cipher_get_bits(self.cipher)

    def __repr__(self):
        # type: () -> str
        return "%s-%s" % (self.name(), len(self))

    def __str__(self):
        # type: () -> str
        return "%s-%s" % (self.name(), len(self))

    def version(self):
        # type: () -> int
        return m2.ssl_cipher_get_version(self.cipher)

    def name(self):
        # type: () -> str
        return six.ensure_text(m2.ssl_cipher_get_name(self.cipher))


class Cipher_Stack(object):
    def __init__(self, stack):
        # type: (bytes) -> None
        """
        :param stack: binary of the C-type STACK_OF(SSL_CIPHER)
        """
        self.stack = stack

    def __len__(self):
        # type: () -> int
        return m2.sk_ssl_cipher_num(self.stack)

    def __getitem__(self, idx):
        # type: (int) -> Cipher
        if not 0 <= idx < m2.sk_ssl_cipher_num(self.stack):
            raise IndexError('index out of range')
        v = m2.sk_ssl_cipher_value(self.stack, idx)
        return Cipher(v)

    def __iter__(self):
        # type: () -> Iterable
        for i in six.moves.range(m2.sk_ssl_cipher_num(self.stack)):
            yield self[i]