Exemple #1
0
 def set_cipher(self, algo, key, iv, op):
     # type: (str, AnyStr, AnyStr, int) -> None
     cipher = getattr(m2, algo, None)
     if cipher is None:
         raise ValueError('unknown cipher', algo)
     else:
         if not isinstance(key, bytes):
             key = key.encode('utf8')
         if not isinstance(iv, bytes):
             iv = iv.encode('utf8')
     m2.bio_set_cipher(self.bio, cipher(), key, iv, int(op))
     m2.bio_push(self.bio, self.obio._ptr())
Exemple #2
0
 def set_cipher(self, algo, key, iv, op):
     # type: (str, AnyStr, AnyStr, int) -> None
     cipher = getattr(m2, algo, None)
     if cipher is None:
         raise ValueError('unknown cipher', algo)
     else:
         if not isinstance(key, bytes):
             key = key.encode('utf8')
         if not isinstance(iv, bytes):
             iv = iv.encode('utf8')
     m2.bio_set_cipher(self.bio, cipher(), key, iv, int(op))
     m2.bio_push(self.bio, self.obio._ptr())
Exemple #3
0
 def __init__(self, under_bio, mode='rwb', _pyfree=1):
     BIO.__init__(self, _pyfree=_pyfree)
     self.io = m2.bio_new(m2.bio_f_buffer())
     self.bio = m2.bio_push(self.io, under_bio._ptr())
     # This reference keeps the underlying BIO alive while we're not closed.
     self._under_bio = under_bio
     if 'w' in mode:
         self.write_closed = 0
     else:
         self.write_closed = 1
Exemple #4
0
 def __init__(self, under_bio, mode='rwb', _pyfree=1):
     BIO.__init__(self, _pyfree=_pyfree)
     self.io = m2.bio_new(m2.bio_f_buffer())
     self.bio = m2.bio_push(self.io, under_bio._ptr())
     # This reference keeps the underlying BIO alive while we're not closed.
     self._under_bio = under_bio
     if 'w' in mode:
         self.write_closed = 0
     else:
         self.write_closed = 1
Exemple #5
0
def c_style(HOST, PORT, req):

    # Set up SSL context.
    ctx = m2.ssl_ctx_new(m2.sslv3_method())
    m2.ssl_ctx_use_cert(ctx, 'client.pem')
    m2.ssl_ctx_use_privkey(ctx, 'client.pem')

    # Make the socket connection.
    s = socket(AF_INET, SOCK_STREAM)
    s.connect((HOST, PORT))

    # Set up the SSL connection.
    sbio = m2.bio_new_socket(s.fileno(), 0)
    ssl = m2.ssl_new(ctx)
    m2.ssl_set_bio(ssl, sbio, sbio)
    m2.ssl_connect(ssl)
    sslbio = m2.bio_new(m2.bio_f_ssl())
    m2.bio_set_ssl(sslbio, ssl, 0)

    # Push a buffering BIO over the SSL BIO.
    iobuf = m2.bio_new(m2.bio_f_buffer())
    topbio = m2.bio_push(iobuf, sslbio)

    # Send the request.
    m2.bio_write(sslbio, req)

    # Receive the response.
    while 1:
        data = m2.bio_gets(topbio, 4096)
        if not data: break
        sys.stdout.write(data)

    # Cleanup. May be missing some necessary steps. ;-|
    m2.bio_pop(topbio)
    m2.bio_free(iobuf)
    m2.ssl_shutdown(ssl)
    m2.ssl_free(ssl)
    m2.ssl_ctx_free(ctx)
    s.close()
Exemple #6
0
def c_style(HOST, PORT, req):

    # Set up SSL context.
    ctx = m2.ssl_ctx_new(m2.sslv3_method())
    m2.ssl_ctx_use_cert(ctx, 'client.pem')
    m2.ssl_ctx_use_privkey(ctx, 'client.pem')

    # Make the socket connection.
    s = socket(AF_INET, SOCK_STREAM)
    s.connect((HOST, PORT))

    # Set up the SSL connection.
    sbio = m2.bio_new_socket(s.fileno(), 0)
    ssl = m2.ssl_new(ctx)
    m2.ssl_set_bio(ssl, sbio, sbio)
    m2.ssl_connect(ssl)
    sslbio = m2.bio_new(m2.bio_f_ssl())
    m2.bio_set_ssl(sslbio, ssl, 0)

    # Push a buffering BIO over the SSL BIO.
    iobuf = m2.bio_new(m2.bio_f_buffer())
    topbio = m2.bio_push(iobuf, sslbio)

    # Send the request.
    m2.bio_write(sslbio, req)

    # Receive the response.
    while 1:
        data = m2.bio_gets(topbio, 4096)
        if not data: break
        sys.stdout.write(data)

    # Cleanup. May be missing some necessary steps. ;-|
    m2.bio_pop(topbio)
    m2.bio_free(iobuf)
    m2.ssl_shutdown(ssl)
    m2.ssl_free(ssl)
    m2.ssl_ctx_free(ctx)
    s.close()
Exemple #7
0
 def set_cipher(self, algo, key, iv, op):
     cipher = getattr(m2, algo, None)
     if cipher is None:
         raise ValueError('unknown cipher', algo)
     m2.bio_set_cipher(self.bio, cipher(), key, iv, op)
     m2.bio_push(self.bio, self.obio._ptr())
Exemple #8
0
 def set_cipher(self, algo, key, iv, op):
     cipher = getattr(m2, algo, None)
     if cipher is None:
         raise ValueError('unknown cipher', algo)
     m2.bio_set_cipher(self.bio, cipher(), key, iv, op)
     m2.bio_push(self.bio, self.obio._ptr())
Exemple #9
0
"""Demonstrates the use of m2.bio_set_mem_eof_return().
Copyright (c) 1999-2003 Ng Pheng Siong. All rights reserved."""

from M2Crypto import m2
m2.lib_init()

use_mem = 1

if use_mem:
    bio = m2.bio_new(m2.bio_s_mem())
else:
    bio = m2.bio_new_file('XXX', 'wb')
ciph = m2.bf_cbc()
filt = m2.bio_new(m2.bio_f_cipher())
m2.bio_set_cipher(filt, ciph, 'key', 'iv', 1)
m2.bio_push(filt, bio)
m2.bio_write(filt, '12345678901234567890')
m2.bio_flush(filt)
m2.bio_pop(filt)
m2.bio_free(filt)
if use_mem:
    m2.bio_set_mem_eof_return(bio, 0)
    xxx = m2.bio_read(bio, 100)
    print `xxx`, len(xxx)
m2.bio_free(bio)

if use_mem:
    bio = m2.bio_new(m2.bio_s_mem())
    m2.bio_write(bio, xxx)
    m2.bio_set_mem_eof_return(bio, 0)
else: