Ejemplo n.º 1
0
 def readline(self, size=4096):
     # type: (int) -> bytes
     if not self.readable():
         raise IOError('cannot read')
     buf = m2.bio_gets(self.bio, size)
     buf = '' if buf is None else buf
     return six.ensure_binary(buf)
Ejemplo n.º 2
0
 def __init__(self, host=None, peerCertHash=None, peerCertDigest='sha1'):
     # type: (Optional[str], Optional[bytes], str) -> None
     self.host = host
     if peerCertHash is not None:
         peerCertHash = six.ensure_binary(peerCertHash)
     self.fingerprint = peerCertHash
     self.digest = peerCertDigest  # type: str
Ejemplo n.º 3
0
 def readline(self, size=4096):
     # type: (int) -> bytes
     if not self.readable():
         raise IOError('cannot read')
     buf = m2.bio_gets(self.bio, size)
     buf = '' if buf is None else buf
     return six.ensure_binary(buf)
Ejemplo n.º 4
0
def new_stack_from_der(der_string):
    # type: (bytes) -> X509_Stack
    """
    Create a new X509_Stack from DER string.

    :return: X509_Stack
    """
    der_string = six.ensure_binary(der_string)
    stack_ptr = m2.make_stack_from_der_sequence(der_string)
    return X509_Stack(stack_ptr, 1, 1)
Ejemplo n.º 5
0
def new_stack_from_der(der_string):
    # type: (bytes) -> X509_Stack
    """
    Create a new X509_Stack from DER string.

    :return: X509_Stack
    """
    der_string = six.ensure_binary(der_string)
    stack_ptr = m2.make_stack_from_der_sequence(der_string)
    return X509_Stack(stack_ptr, 1, 1)
Ejemplo n.º 6
0
 def _encode_auth(self):
     # type: () -> Optional[bytes]
     """ Encode the username and password for use in the auth header. """
     if not (self._username and self._password):
         return None
     # Authenticated proxy
     userpass = "******" % (self._username, self._password)
     with warnings.catch_warnings():
         warnings.simplefilter("ignore", DeprecationWarning)
         enc_userpass = base64.encodestring(userpass).replace("\n", "")
     return six.ensure_binary("Basic %s" % enc_userpass)
Ejemplo n.º 7
0
def load_request_der_string(string):
    # type: (AnyStr) -> Request
    """
    Load certificate request from a string.

    :param string: String containing a certificate request in DER format.
    :return: M2Crypto.X509.Request object.
    """
    string = six.ensure_binary(string)
    bio = BIO.MemoryBuffer(string)
    return load_request_bio(bio, FORMAT_DER)
Ejemplo n.º 8
0
def load_request_der_string(string):
    # type: (AnyStr) -> Request
    """
    Load certificate request from a string.

    :param string: String containing a certificate request in DER format.
    :return: M2Crypto.X509.Request object.
    """
    string = six.ensure_binary(string)
    bio = BIO.MemoryBuffer(string)
    return load_request_bio(bio, FORMAT_DER)
Ejemplo n.º 9
0
 def readlines(self, sizehint='ignored'):
     # type: (Union[AnyStr, int]) -> Iterable[bytes]
     if not self.readable():
         raise IOError('cannot read')
     lines = []
     while 1:
         buf = m2.bio_gets(self.bio, 4096)
         if buf is None:
             break
         lines.append(six.ensure_binary(buf))
     return lines
Ejemplo n.º 10
0
 def _get_connect_msg(self):
     # type: () -> bytes
     """ Return an HTTP CONNECT request to send to the proxy. """
     msg = "CONNECT %s:%d HTTP/1.1\r\n" % (self._real_host, self._real_port)
     msg = msg + "Host: %s:%d\r\n" % (self._real_host, self._real_port)
     if self._proxy_UA:
         msg = msg + "%s: %s\r\n" % (self._UA_HEADER, self._proxy_UA)
     if self._proxy_auth:
         msg = msg + "%s: %s\r\n" % (self._AUTH_HEADER, self._proxy_auth)
     msg = msg + "\r\n"
     return six.ensure_binary(msg)
Ejemplo n.º 11
0
 def _get_connect_msg(self):
     # type: () -> bytes
     """ Return an HTTP CONNECT request to send to the proxy. """
     msg = "CONNECT %s:%d HTTP/1.1\r\n" % (self._real_host, self._real_port)
     msg = msg + "Host: %s:%d\r\n" % (self._real_host, self._real_port)
     if self._proxy_UA:
         msg = msg + "%s: %s\r\n" % (self._UA_HEADER, self._proxy_UA)
     if self._proxy_auth:
         msg = msg + "%s: %s\r\n" % (self._AUTH_HEADER, self._proxy_auth)
     msg = msg + "\r\n"
     return six.ensure_binary(msg)
Ejemplo n.º 12
0
 def _encode_auth(self):
     # type: () -> Optional[bytes]
     """ Encode the username and password for use in the auth header. """
     if not (self._username and self._password):
         return None
     # Authenticated proxy
     userpass = "******" % (self._username, self._password)
     with warnings.catch_warnings():
         warnings.simplefilter("ignore", DeprecationWarning)
         enc_userpass = base64.encodestring(userpass).replace("\n", "")
     return six.ensure_binary("Basic %s" % enc_userpass)
Ejemplo n.º 13
0
 def readlines(self, sizehint='ignored'):
     # type: (Union[AnyStr, int]) -> Iterable[bytes]
     if not self.readable():
         raise IOError('cannot read')
     lines = []
     while 1:
         buf = m2.bio_gets(self.bio, 4096)
         if buf is None:
             break
         lines.append(six.ensure_binary(buf))
     return lines
Ejemplo n.º 14
0
    def __setattr__(self, attr, value):
        # type: (str, AnyStr) -> int
        """
        :return: 1 for success of 0 if an error occurred.
        """
        if attr in self.nid:
            assert m2.x509_name_type_check(self.x509_name), \
                "'x509_name' type error"
            return m2.x509_name_set_by_nid(self.x509_name, self.nid[attr],
                                           six.ensure_binary(value))

        self.__dict__[attr] = value
Ejemplo n.º 15
0
    def __setattr__(self, attr, value):
        # type: (str, AnyStr) -> int
        """
        :return: 1 for success of 0 if an error occurred.
        """
        if attr in self.nid:
            assert m2.x509_name_type_check(self.x509_name), \
                "'x509_name' type error"
            return m2.x509_name_set_by_nid(self.x509_name, self.nid[attr],
                                           six.ensure_binary(value))

        self.__dict__[attr] = value
Ejemplo n.º 16
0
 def test_mix_unmix3(self):
     c = self.jar.makeCookie(self.exp, self.data)
     s = SimpleCookie()
     s.load(c.output(header=""))
     exp, data, digest = unmix3(s[self._token].value)
     self.assertEqual(data, self.data)
     # see comment in test_mix_unmix
     self.assertAlmostEqual(exp, self.exp, places=4)
     key = self.jar._key  # pylint: disable=protected-access
     mac = util.bin_to_hex(
         EVP.hmac(key, six.ensure_binary(mix(self.exp, self.data)), 'sha1'))
     self.assertEqual(digest, mac)
Ejemplo n.º 17
0
def load_cert_der_string(string):
    # type: (AnyStr) -> X509
    """
    Load certificate from a string.

    :param string: String containing a certificate in DER format.

    :return: M2Crypto.X509.X509 object.
    """
    string = six.ensure_binary(string)
    bio = BIO.MemoryBuffer(string)
    cptr = m2.d2i_x509(bio._ptr())
    return X509(cptr, _pyfree=1)
Ejemplo n.º 18
0
 def test_make_cookie(self):
     c = self.jar.makeCookie(self.exp, self.data)
     self.assertTrue(isinstance(c, AuthCookie))
     self.assertEqual(c.expiry(), self.exp)
     self.assertEqual(c.data(), self.data)
     # Peek inside the cookie jar...
     key = self.jar._key  # pylint: disable=protected-access
     mac = util.bin_to_hex(
         EVP.hmac(key, six.ensure_binary(mix(self.exp, self.data)), 'sha1'))
     self.assertEqual(c.mac(), mac)
     # Ok, stop peeking now.
     cookie_str = self._format % (self.exp, self.data, mac)
     self.assertEqual(c.output(), cookie_str)
Ejemplo n.º 19
0
def load_cert_der_string(string):
    # type: (AnyStr) -> X509
    """
    Load certificate from a string.

    :param string: String containing a certificate in DER format.

    :return: M2Crypto.X509.X509 object.
    """
    string = six.ensure_binary(string)
    bio = BIO.MemoryBuffer(string)
    cptr = m2.d2i_x509(bio._ptr())
    return X509(cptr, _pyfree=1)
Ejemplo n.º 20
0
def load_cert_string(string, format=FORMAT_PEM):
    # type: (AnyStr, int) -> X509
    """
    Load certificate from a string.

    :param string: String containing a certificate in either DER or PEM format.

    :param format: Describes the format of the cert to be loaded,
                   either PEM or DER (via constants FORMAT_PEM
                   and FORMAT_FORMAT_DER)

    :return: M2Crypto.X509.X509 object.
    """
    string = six.ensure_binary(string)
    bio = BIO.MemoryBuffer(string)
    return load_cert_bio(bio, format)
Ejemplo n.º 21
0
def load_cert_string(string, format=FORMAT_PEM):
    # type: (AnyStr, int) -> X509
    """
    Load certificate from a string.

    :param string: String containing a certificate in either DER or PEM format.

    :param format: Describes the format of the cert to be loaded,
                   either PEM or DER (via constants FORMAT_PEM
                   and FORMAT_FORMAT_DER)

    :return: M2Crypto.X509.X509 object.
    """
    string = six.ensure_binary(string)
    bio = BIO.MemoryBuffer(string)
    return load_cert_bio(bio, format)
Ejemplo n.º 22
0
    def get_ext(self, name):
        # type: (str) -> X509_Extension
        """
        Get X509 extension by name.

        :param name:    Name of the extension

        :return:       X509_Extension
        """
        # Optimizations to reduce attribute accesses
        m2x509_get_ext = m2.x509_get_ext
        m2x509_extension_get_name = m2.x509_extension_get_name
        x509 = self.x509

        name = six.ensure_binary(name)
        for i in range(m2.x509_get_ext_count(x509)):
            ext_ptr = m2x509_get_ext(x509, i)
            if m2x509_extension_get_name(ext_ptr) == name:
                return X509_Extension(ext_ptr, _pyfree=0)

        raise LookupError
Ejemplo n.º 23
0
    def get_ext(self, name):
        # type: (str) -> X509_Extension
        """
        Get X509 extension by name.

        :param name:    Name of the extension

        :return:       X509_Extension
        """
        # Optimizations to reduce attribute accesses
        m2x509_get_ext = m2.x509_get_ext
        m2x509_extension_get_name = m2.x509_extension_get_name
        x509 = self.x509

        name = six.ensure_binary(name)
        for i in range(m2.x509_get_ext_count(x509)):
            ext_ptr = m2x509_get_ext(x509, i)
            if m2x509_extension_get_name(ext_ptr) == name:
                return X509_Extension(ext_ptr, _pyfree=0)

        raise LookupError
Ejemplo n.º 24
0
 def test_py3bytes_bytes(self):
     self.assertIsInstance(six.ensure_binary(b'test'), six.binary_type)
Ejemplo n.º 25
0
 def test_py3bytes_bytearray(self):
     self.assertIsInstance(six.ensure_binary(bytearray(b'test')), bytearray)
Ejemplo n.º 26
0
 def test_py3bytes_None(self):
     with self.assertRaises(TypeError):
         six.ensure_binary(None)
Ejemplo n.º 27
0
 def test_py3bytes_bytes(self):
     self.assertIsInstance(six.ensure_binary(b'test'), six.binary_type)
Ejemplo n.º 28
0
 def _hmac(self, key, data):
     # type: (bytes, str) -> str
     return util.bin_to_hex(m2.hmac(key, six.ensure_binary(data),
                                    m2.sha1()))
Ejemplo n.º 29
0
 def test_py3bytes_None(self):
     with self.assertRaises(TypeError):
         six.ensure_binary(None)
Ejemplo n.º 30
0
 def _hmac(self, key, data):
     # type: (bytes, str) -> str
     return util.bin_to_hex(m2.hmac(key, six.ensure_binary(data), m2.sha1()))
Ejemplo n.º 31
0
 def test_py3bytes_str(self):
     self.assertIsInstance(six.ensure_binary(u'test'), bytes)