示例#1
0
    def test_email_ssl_encrypt(self):
        cmd = [
            'openssl',
            'smime',
            '-encrypt',
            '-aes256',
            '-in',
            fixture('smime-unsigned.txt'),
            '-out',
            fixture('smime-ssl-encrypted.txt'),
            fixture('demo2_user1.crt.pem'),
        ]
        process = Popen(cmd, stdout=PIPE, stderr=PIPE)
        stdout, stderr = process.communicate()
        assert stderr == b''
        assert stdout == b''

        with open(fixture('demo2_user1.key.pem'), 'rb') as fh:
            key = crypto.load_privatekey(crypto.FILETYPE_PEM, fh.read(),
                                         b'1234')
        key = key.to_cryptography_key()
        with io.open(fixture('smime-ssl-encrypted.txt'),
                     'rt',
                     encoding='utf-8') as fh:
            datae = fh.read()
        datad = email.decrypt(datae, key)
        with open(fixture('smime-unsigned.txt'), 'rb') as fh:
            datau = fh.read()

        assert datau == datad.replace(b'\r\n', b'\n')
示例#2
0
def main():
    with open('demo2_user1.p12', 'rb') as fp:
        p12 = pkcs12.load_key_and_certificates(fp.read(), b'1234', backends.default_backend())
    for fname in (
        'smime-ssl-encrypted.txt',
        'smime-ssl-oaep-encrypted.txt',
        'smime-encrypted.txt',
        'smime-encrypted-oaep.txt',
    ):
        print('*' * 20, fname)
        try:
            datae = io.open(fname, 'rt', encoding='utf-8').read()
        except:
            print('no such file')
            continue
        datad = email.decrypt(datae, p12[0])
        datad = datad.decode('utf-8')
        io.open(fname.replace('encrypted', 'decrypted'), 'wt', encoding='utf-8').write(datad)
示例#3
0
def main():
    key = crypto.load_privatekey(crypto.FILETYPE_PEM,
                                 open('demo2_user1.key.pem', 'rb').read(),
                                 b'1234')
    key = key.to_cryptography_key()
    for fname in (
            'smime-ssl-encrypted.txt',
            'smime-encrypted.txt',
    ):
        print('*' * 20, fname)
        try:
            datae = open(fname, 'rt', encoding='utf-8').read()
        except FileNotFoundError:
            print('no such file')
            continue
        datad = email.decrypt(datae, key)
        datad = datad.decode('utf-8')
        open(fname.replace('encrypted', 'decrypted'), 'wt',
             encoding='utf-8').write(datad)
示例#4
0
    def test_email_crypt(self):
        def load_cert(fname):
            with open(fname, 'rb') as f:
                cert_bytes = f.read()
                return crypto.load_certificate(crypto.FILETYPE_PEM, cert_bytes)

        certs = (load_cert(fixture('demo2_user1.crt.pem')), )
        datau = open(fixture('smime-unsigned.txt'), 'rb').read()
        datae = email.encrypt(datau, certs, 'aes256_ofb')
        fname = fixture('smime-encrypted.txt')
        open(fname, 'wt').write(datae)

        key = crypto.load_privatekey(
            crypto.FILETYPE_PEM,
            open(fixture('demo2_user1.key.pem'), 'rb').read(), b'1234')
        key = key.to_cryptography_key()
        datae = io.open(fname, 'rt', encoding='utf-8').read()
        datad = email.decrypt(datae, key)

        assert datau == datad
示例#5
0
    def test_email_crypt(self):
        def load_cert(relative_path):
            with open(relative_path, 'rb') as f:
                return x509.load_pem_x509_certificate(f.read(), backends.default_backend())
        certs = (
            load_cert(fixture('demo2_user1.crt.pem')),
        )
        with open(fixture('smime-unsigned.txt'), 'rb') as fh:
            datau = fh.read()
        datae = email.encrypt(datau, certs, 'aes256_ofb')
        fname = fixture('smime-encrypted.txt')
        with open(fname, 'wt') as fh:
            fh.write(datae)

        with open(fixture('demo2_user1.key.pem'), 'rb') as fh:
            key = load_pem_private_key(fh.read(), b'1234', backends.default_backend())
        with io.open(fname, 'rt', encoding='utf-8') as fh:
            datae = fh.read()
        datad = email.decrypt(datae, key)

        assert datau == datad
示例#6
0
    def test_email_ssl_encrypt(self):
        cmd = [
            'openssl', 'smime', '-encrypt', '-aes256',
            '-in', fixture('smime-unsigned.txt'),
            '-out', fixture('smime-ssl-encrypted.txt'),
            fixture('demo2_user1.crt.pem'),
        ]
        process = Popen(cmd, stdout=PIPE, stderr=PIPE)
        stdout, stderr = process.communicate()
        assert stderr == b''
        assert stdout == b''

        with open(fixture('demo2_user1.key.pem'), 'rb') as fh:
            key = load_pem_private_key(fh.read(), None, backends.default_backend())
        with io.open(fixture('smime-ssl-encrypted.txt'), 'rt', encoding='utf-8') as fh:
            datae = fh.read()
        datad = email.decrypt(datae, key)
        with open(fixture('smime-unsigned.txt'), 'rb') as fh:
            datau = fh.read()

        assert datau == datad.replace(b'\r\n', b'\n')