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)
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
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)
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)
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)
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
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)
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
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)
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)
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)
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)
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
def test_py3bytes_bytes(self): self.assertIsInstance(six.ensure_binary(b'test'), six.binary_type)
def test_py3bytes_bytearray(self): self.assertIsInstance(six.ensure_binary(bytearray(b'test')), bytearray)
def test_py3bytes_None(self): with self.assertRaises(TypeError): six.ensure_binary(None)
def _hmac(self, key, data): # type: (bytes, str) -> str return util.bin_to_hex(m2.hmac(key, six.ensure_binary(data), m2.sha1()))
def test_py3bytes_str(self): self.assertIsInstance(six.ensure_binary(u'test'), bytes)