Skip to content
Snippets Groups Projects
Commit b5a79a4269d7 authored by Alex Gaynor's avatar Alex Gaynor
Browse files

Initial commit. Migrates over basic project files, and the OpenSSL bindings

from OpenTLS.
parent 8c3d4a978a8a
No related branches found
No related tags found
No related merge requests found
Showing
with 1109 additions and 0 deletions
*.py[co]
__pycache__/
_build/
LICENSE 0 → 100644
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work.
To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "[]"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.
Copyright [yyyy] [name of copyright owner]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
from cryptography.c.api import api
__all__ = ["api"]
from __future__ import absolute_import, division, print_function
import atexit
from collections import namedtuple
from cffi import FFI
class API(object):
"""OpenSSL API wrapper."""
SSLVersion = namedtuple('SSLVersion',
['major', 'minor', 'fix', 'patch', 'status']
)
_modules = [
'asn1',
'bio',
'bio_filter',
'bio_sink',
'err',
'evp',
'evp_md',
'evp_cipher',
'evp_cipher_listing',
'hmac',
'obj',
'openssl',
'nid',
'pkcs5',
'rand',
'ssl',
'ssleay',
'stdio',
]
def __init__(self):
self.ffi = FFI()
self.INCLUDES = []
self.TYPES = []
self.FUNCTIONS = []
self.C_CUSTOMIZATION = []
self.OVERRIDES = []
self.SETUP = []
self.TEARDOWN = []
self._import()
self._define()
self._verify()
self._override()
self._populate()
self._initialise()
def _import(self):
"import all library definitions"
for name in self._modules:
module = __import__(__name__ + '.' + name, fromlist=['*'])
self._import_definitions(module, 'INCLUDES')
self._import_definitions(module, 'TYPES')
self._import_definitions(module, 'FUNCTIONS')
self._import_definitions(module, 'C_CUSTOMIZATION')
self._import_definitions(module, 'OVERRIDES')
self._import_definitions(module, 'SETUP')
self._import_definitions(module, 'TEARDOWN')
def _import_definitions(self, module, name):
"import defintions named definitions from module"
container = getattr(self, name)
for definition in getattr(module, name, ()):
if definition not in container:
container.append(definition)
def _define(self):
"parse function definitions"
for typedef in self.TYPES:
self.ffi.cdef(typedef)
for function in self.FUNCTIONS:
self.ffi.cdef(function)
def _verify(self):
"load openssl, create function attributes"
self.openssl = self.ffi.verify(
source="\n".join(self.INCLUDES + self.C_CUSTOMIZATION),
# ext_package must agree with the value in setup.py
ext_package="tls",
extra_compile_args=[
'-Wno-deprecated-declarations',
],
libraries=['ssl']
)
def _override(self):
"""
Create any Python-level overrides of the cffi-based wrappers.
"""
self._overrides = {}
for func in self.OVERRIDES:
name = func.__name__
from_openssl = getattr(self.openssl, name)
override = func(self.openssl, from_openssl)
self._overrides[name] = override
def _populate(self):
"""
Bind some aliases for FFI APIs on self.
"""
self.NULL = self.ffi.NULL
self.buffer = self.ffi.buffer
self.callback = self.ffi.callback
self.cast = self.ffi.cast
self.new = self.ffi.new
self.gc = self.ffi.gc
self.string = self.ffi.string
def __getattr__(self, name):
"""
Try to resolve any attribute that does not exist on self as an
attribute of the OpenSSL FFI object (in other words, as an OpenSSL
API).
"""
return self._overrides.get(name, getattr(self.openssl, name))
def _initialise(self):
"initialise openssl, schedule cleanup at exit"
for function in self.SETUP:
getattr(self, function)()
for function in self.TEARDOWN:
atexit.register(getattr(self, function))
def version_info(self):
"Return SSL version information"
version = self.SSLeay()
major = version >> (7 * 4) & 0xFF
minor = version >> (5 * 4) & 0xFF
fix = version >> (3 * 4) & 0xFF
patch = version >> (1 * 4) & 0xFF
patch = '' if not patch else chr(96 + patch)
status = version & 0x0F
if status == 0x0F:
status = 'release'
elif status == 0x00:
status = 'dev'
else:
status = 'beta{}'.format(status)
return self.SSLVersion(major, minor, fix, patch, status)
def version(self, detail=None):
"Return SSL version string"
detail = self.SSLEAY_VERSION if detail is None else detail
buff = self.SSLeay_version(detail)
return self.string(buff)
api = API()
INCLUDES = [
'#include <openssl/asn1.h>',
]
TYPES = [
'typedef ... ASN1_INTEGER;',
'typedef ... ASN1_OCTET_STRING;',
'typedef ... ASN1_OBJECT;',
'typedef ... ASN1_STRING;',
'typedef ... ASN1_TYPE;',
]
FUNCTIONS = [
'ASN1_OBJECT *ASN1_OBJECT_new(void);',
'void ASN1_OBJECT_free(ASN1_OBJECT *a);',
# ASN1 OBJECT IDENTIFIER
'ASN1_OBJECT *d2i_ASN1_OBJECT(ASN1_OBJECT **a, const unsigned char **pp, long length);',
'int i2d_ASN1_OBJECT(ASN1_OBJECT *a, unsigned char **pp);',
# ASN1 STRING
'ASN1_STRING * ASN1_STRING_new(void);',
'ASN1_STRING * ASN1_STRING_type_new(int type);',
'void ASN1_STRING_free(ASN1_STRING *a);',
'int ASN1_STRING_length(ASN1_STRING *x);',
'unsigned char * ASN1_STRING_data(ASN1_STRING *x);',
'ASN1_STRING * ASN1_STRING_dup(ASN1_STRING *a);',
'int ASN1_STRING_cmp(ASN1_STRING *a, ASN1_STRING *b);',
'int ASN1_STRING_set(ASN1_STRING *str, const void *data, int len);',
'int ASN1_STRING_type(ASN1_STRING *x);',
'int ASN1_STRING_to_UTF8(unsigned char **out, ASN1_STRING *in);',
# ASN1 OCTET STRING
'ASN1_OCTET_STRING * ASN1_OCTET_STRING_new(void);',
'void ASN1_OCTET_STRING_free(ASN1_OCTET_STRING *a);',
'ASN1_OCTET_STRING * ASN1_OCTET_STRING_dup(ASN1_OCTET_STRING *a);',
'int ASN1_OCTET_STRING_cmp(ASN1_OCTET_STRING *a, ASN1_OCTET_STRING *b);',
'int ASN1_OCTET_STRING_set(ASN1_OCTET_STRING *str, const void *data, int len);',
# ASN1 INTEGER
'ASN1_INTEGER * ASN1_INTEGER_new(void);',
'void ASN1_INTEGER_free(ASN1_INTEGER *a);',
'ASN1_INTEGER * ASN1_INTEGER_dup(ASN1_INTEGER *a);',
'int ASN1_INTEGER_cmp(ASN1_INTEGER *a, ASN1_INTEGER *b);',
'int ASN1_INTEGER_set(ASN1_INTEGER *a, long v);',
'long ASN1_INTEGER_get(ASN1_INTEGER *a);',
]
INCLUDES = [
'#include <openssl/bio.h>',
]
TYPES = [
# BIO ctrl constants
'static const int BIO_CTRL_RESET;',
'static const int BIO_CTRL_EOF;',
'static const int BIO_CTRL_SET;',
'static const int BIO_CTRL_SET_CLOSE;',
'static const int BIO_CTRL_FLUSH;',
'static const int BIO_CTRL_DUP;',
'static const int BIO_CTRL_GET_CLOSE;',
'static const int BIO_CTRL_INFO;',
'static const int BIO_CTRL_GET;',
'static const int BIO_CTRL_PENDING;',
'static const int BIO_CTRL_WPENDING;',
'static const int BIO_C_FILE_SEEK;',
'static const int BIO_C_FILE_TELL;',
# BIO type constants
'static const int BIO_TYPE_NONE;',
'static const int BIO_TYPE_PROXY_CLIENT;',
'static const int BIO_TYPE_PROXY_SERVER;',
'static const int BIO_TYPE_NBIO_TEST;',
'static const int BIO_TYPE_BER;',
'static const int BIO_TYPE_BIO;',
'static const int BIO_TYPE_DESCRIPTOR;',
# BIO flags
'static const int BIO_FLAGS_READ;',
'static const int BIO_FLAGS_WRITE;',
'static const int BIO_FLAGS_IO_SPECIAL;',
'static const int BIO_FLAGS_RWS;',
'static const int BIO_FLAGS_SHOULD_RETRY;',
'typedef ... BUF_MEM;',
# BIO forward declaration
'typedef struct bio_st BIO;',
# BIO callbacks definition
'typedef void bio_info_cb(BIO *b, int oper, const char *ptr, int arg1, long arg2, long arg3);',
# BIO_METHOD definition
'''
struct bio_method_st {
int type;
const char *name;
int (*bwrite)(BIO*, const char*, int);
int (*bread)(BIO*, char*, int);
int (*bputs)(BIO*, const char*);
int (*bgets)(BIO*, char*, int);
long (*ctrl)(BIO*, int, long, void*);
int (*create)(BIO*);
int (*destroy)(BIO*);
long (*callback_ctrl)(BIO*, int, bio_info_cb*);
...;
};''',
'typedef struct bio_method_st BIO_METHOD;',
# BIO definition
'''
struct bio_st {
BIO_METHOD *method;
long (*callback)(struct bio_st*, int, const char*, int, long, long);
char *cb_arg;
int init;
int shutdown;
int flags;
int retry_reason;
int num;
void *ptr;
struct bio_st *next_bio;
struct bio_st *prev_bio;
int references;
unsigned long num_read;
unsigned long num_write;
...;
};''',
]
FUNCTIONS = [
# BIO create functions
'BIO* BIO_new(BIO_METHOD *type);',
'int BIO_set(BIO *a, BIO_METHOD *type);',
'int BIO_free(BIO *a);',
'void BIO_vfree(BIO *a);',
'void BIO_free_all(BIO *a);',
# BIO stacking functions
'BIO* BIO_push(BIO *b, BIO *append);',
'BIO* BIO_pop(BIO *b);',
'BIO* BIO_next(BIO *b);',
'BIO* BIO_find_type(BIO *b, int bio_type);',
'int BIO_method_type(BIO *b);',
# BIO control functions
'long BIO_ctrl(BIO *bp, int cmd, long larg, void *parg);',
'long BIO_callback_ctrl(BIO *b, int cmd, void (*fp)(struct bio_st *, int, const char *, int, long, long));',
'char* BIO_ptr_ctrl(BIO *bp, int cmd, long larg);',
'long BIO_int_ctrl(BIO *bp, int cmd, long larg, int iarg);',
'int BIO_reset(BIO *b);',
'int BIO_seek(BIO *b, int ofs);',
'int BIO_tell(BIO *b);',
'int BIO_flush(BIO *b);',
'int BIO_eof(BIO *b);',
'int BIO_set_close(BIO *b,long flag);',
'int BIO_get_close(BIO *b);',
'int BIO_pending(BIO *b);',
'int BIO_wpending(BIO *b);',
'size_t BIO_ctrl_pending(BIO *b);',
'size_t BIO_ctrl_wpending(BIO *b);',
'int BIO_get_info_callback(BIO *b,bio_info_cb **cbp);',
'int BIO_set_info_callback(BIO *b,bio_info_cb *cb);',
# BIO IO functions
'int BIO_read(BIO *b, void *buf, int len);',
'int BIO_gets(BIO *b, char *buf, int size);',
'int BIO_write(BIO *b, const void *buf, int len);',
'int BIO_puts(BIO *b, const char *buf);',
# BIO should functions
'int BIO_should_read(BIO *b);',
'int BIO_should_write(BIO *b);',
'int BIO_should_io_special(BIO *b);',
'int BIO_retry_type(BIO *b);',
'int BIO_should_retry(BIO *b);',
]
INCLUDES = [
'#include <openssl/bio.h>',
]
TYPES = [
'static const int BIO_TYPE_NULL_FILTER;',
'static const int BIO_TYPE_SSL;',
'static const int BIO_TYPE_MD;',
'static const int BIO_TYPE_BUFFER;',
'static const int BIO_TYPE_CIPHER;',
'static const int BIO_TYPE_BASE64;',
'static const int BIO_TYPE_FILTER;',
]
FUNCTIONS = [
# BIO null
'BIO_METHOD *BIO_f_null(void);',
# BIO ssl
# TODO
# BIO message digests
'BIO_METHOD *BIO_f_md(void);',
'int BIO_set_md(BIO *b, EVP_MD *md);',
'int BIO_get_md(BIO *b, EVP_MD **mdp);',
'int BIO_set_md_ctx(BIO *b, EVP_MD_CTX **mdcp);',
'int BIO_get_md_ctx(BIO *b, EVP_MD_CTX **mdcp);',
# BIO buffer
'BIO_METHOD * BIO_f_buffer(void);',
'long BIO_get_buffer_num_lines(BIO *b);',
'long BIO_set_read_buffer_size(BIO *b, long size);',
'long BIO_set_write_buffer_size(BIO *b, long size);',
'long BIO_set_buffer_size(BIO *b, long size);',
'long BIO_set_buffer_read_data(BIO *b, void *buf, long num);',
# BIO cipher
'BIO_METHOD * BIO_f_cipher(void);',
'void BIO_set_cipher(BIO *b,const EVP_CIPHER *cipher, unsigned char *key, unsigned char *iv, int enc);',
'int BIO_get_cipher_status(BIO *b);',
'int BIO_get_cipher_ctx(BIO *b, EVP_CIPHER_CTX **pctx);',
# BIO base64
'BIO_METHOD *BIO_f_base64(void);',
# BIO zlib
# 'BIO_METHOD *BIO_f_zlib(void);',
]
INCLUDES = [
'#include <openssl/bio.h>',
]
TYPES = [
'static const int BIO_TYPE_MEM;',
'static const int BIO_TYPE_FILE;',
'static const int BIO_TYPE_FD;',
'static const int BIO_TYPE_SOCKET;',
'static const int BIO_TYPE_CONNECT;',
'static const int BIO_TYPE_ACCEPT;',
'static const int BIO_TYPE_NULL;',
'static const int BIO_CLOSE;',
'static const int BIO_NOCLOSE;',
'static const int BIO_TYPE_SOURCE_SINK;',
]
FUNCTIONS = [
# BIO mem buffers
'BIO_METHOD *BIO_s_mem(void);',
'long BIO_set_mem_eof_return(BIO *b, int v);',
'long BIO_get_mem_data(BIO *b, char **pp);',
'long BIO_set_mem_buf(BIO *b,BUF_MEM *bm,int c);',
'long BIO_get_mem_ptr(BIO *b,BUF_MEM **pp);',
'BIO *BIO_new_mem_buf(void *buf, int len);',
# BIO files
'BIO_METHOD *BIO_s_file(void);',
'BIO *BIO_new_file(const char *filename, const char *mode);',
'BIO *BIO_new_fp(FILE *stream, int flags);',
'long BIO_set_fp(BIO *b, FILE *fp, int flags);',
'long BIO_get_fp(BIO *b, FILE **fpp);',
'int BIO_read_filename(BIO *b, char *name);',
'int BIO_write_filename(BIO *b, char *name);',
'int BIO_append_filename(BIO *b, char *name);',
'int BIO_rw_filename(BIO *b, char *name);',
# BIO fd
'BIO_METHOD *BIO_s_fd(void);',
'long BIO_set_fd(BIO *bp, long fd, int cmd);',
'long BIO_get_fd(BIO *bp, char *c);',
'BIO *BIO_new_fd(int fd, int close_flag);',
# BIO socket
'BIO_METHOD *BIO_s_socket(void);'
'BIO *BIO_new_socket(int sock, int close_flag);'
# BIO connect
# TODO
# BIO accept
# TODO
# BIO null
'BIO_METHOD *BIO_s_null(void);',
]
INCLUDES = [
'#include <openssl/err.h>',
'#include <openssl/ssl.h>',
]
SETUP = [
'SSL_load_error_strings',
]
TEARDOWN = [
'ERR_free_strings',
]
TYPES = [
'struct ERR_string_data_st { unsigned long error; const char *string; };',
'typedef struct ERR_string_data_st ERR_STRING_DATA;',
]
FUNCTIONS = [
'void ERR_load_crypto_strings(void);',
'void ERR_free_strings(void);',
'void SSL_load_error_strings(void);',
'char* ERR_error_string(unsigned long e, char *buf);',
'void ERR_error_string_n(unsigned long e, char *buf, size_t len);',
'const char* ERR_lib_error_string(unsigned long e);',
'const char* ERR_func_error_string(unsigned long e);',
'const char* ERR_reason_error_string(unsigned long e);',
'void ERR_print_errors(BIO *bp);',
'void ERR_print_errors_fp(FILE *fp);',
'unsigned long ERR_get_error(void);',
'unsigned long ERR_peek_error(void);',
'unsigned long ERR_peek_last_error(void);',
'unsigned long ERR_get_error_line(const char **file, int *line);',
'unsigned long ERR_peek_error_line(const char **file, int *line);',
'unsigned long ERR_peek_last_error_line(const char **file, int *line);',
'unsigned long ERR_get_error_line_data(const char **file, int *line, const char **data, int *flags);',
'unsigned long ERR_peek_error_line_data(const char **file, int *line, const char **data, int *flags);',
'unsigned long ERR_peek_last_error_line_data(const char **file, int *line, const char **data, int *flags);',
'void ERR_put_error(int lib, int func, int reason, const char *file, int line);',
'void ERR_add_error_data(int num, ...);',
'void ERR_load_strings(int lib, ERR_STRING_DATA str[]);',
'int ERR_get_next_error_library(void);',
'unsigned long ERR_PACK(int lib, int func, int reason);',
'int ERR_GET_LIB(unsigned long e);',
'int ERR_GET_FUNC(unsigned long e);',
'int ERR_GET_REASON(unsigned long e);',
]
INCLUDES = [
'#include "openssl/evp.h"',
]
TEARDOWN = [
'EVP_cleanup',
]
TYPES = [
'typedef ... ENGINE;',
]
INCLUDES = [
'#include "openssl/evp.h"',
]
TYPES = [
'static const int EVP_CIPH_ECB_MODE;',
'static const int EVP_CIPH_CBC_MODE;',
'static const int EVP_CIPH_CFB_MODE;',
'static const int EVP_CIPH_OFB_MODE;',
'static const int EVP_CIPH_STREAM_CIPHER;',
'struct evp_cipher_ctx_st { ...; };',
'typedef ... EVP_CIPHER;',
'typedef struct evp_cipher_ctx_st EVP_CIPHER_CTX;',
]
FUNCTIONS = [
'void EVP_CIPHER_CTX_init(EVP_CIPHER_CTX *a);',
# encrypt_ex
'int EVP_EncryptInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type, ENGINE *impl, unsigned char *key, unsigned char *iv);',
'int EVP_EncryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl, unsigned char *in, int inl);',
'int EVP_EncryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl);',
# decrypt_ex
'int EVP_DecryptInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type, ENGINE *impl, unsigned char *key, unsigned char *iv);',
'int EVP_DecryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl, unsigned char *in, int inl);',
'int EVP_DecryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *outm, int *outl);',
# cipher_ex
'int EVP_CipherInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type, ENGINE *impl, unsigned char *key, unsigned char *iv, int enc);',
'int EVP_CipherUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl, unsigned char *in, int inl);',
'int EVP_CipherFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *outm, int *outl);',
# encrypt
'int EVP_EncryptInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type, unsigned char *key, unsigned char *iv);',
'int EVP_EncryptFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl);',
# decrypt
'int EVP_DecryptInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type, unsigned char *key, unsigned char *iv);',
'int EVP_DecryptFinal(EVP_CIPHER_CTX *ctx, unsigned char *outm, int *outl);',
# cipher
'int EVP_CipherInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type, unsigned char *key, unsigned char *iv, int enc);',
'int EVP_CipherFinal(EVP_CIPHER_CTX *ctx, unsigned char *outm, int *outl);',
# control
'int EVP_CIPHER_CTX_set_padding(EVP_CIPHER_CTX *x, int padding);',
'int EVP_CIPHER_CTX_set_key_length(EVP_CIPHER_CTX *x, int keylen);',
'int EVP_CIPHER_CTX_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr);',
'int EVP_CIPHER_CTX_cleanup(EVP_CIPHER_CTX *a);',
'const EVP_CIPHER *EVP_get_cipherbyname(const char *name);',
# cipher macros
'const EVP_CIPHER *EVP_get_cipherbynid(int n);',
'const EVP_CIPHER *EVP_get_cipherbyobj(const ASN1_OBJECT *o);',
'int EVP_CIPHER_nid(const EVP_CIPHER *cipher);',
'int EVP_CIPHER_block_size(const EVP_CIPHER *cipher);',
'int EVP_CIPHER_key_length(const EVP_CIPHER *cipher);',
'int EVP_CIPHER_iv_length(const EVP_CIPHER *cipher);',
'unsigned long EVP_CIPHER_flags(const EVP_CIPHER *cipher);',
'unsigned long EVP_CIPHER_mode(const EVP_CIPHER *cipher);',
'int EVP_CIPHER_type(const EVP_CIPHER *ctx);',
# ctx macros
'const EVP_CIPHER *EVP_CIPHER_CTX_cipher(const EVP_CIPHER_CTX *ctx);',
'int EVP_CIPHER_CTX_nid(const EVP_CIPHER_CTX *ctx);',
'int EVP_CIPHER_CTX_block_size(const EVP_CIPHER_CTX *ctx);',
'int EVP_CIPHER_CTX_key_length(const EVP_CIPHER_CTX *ctx);',
'int EVP_CIPHER_CTX_iv_length(const EVP_CIPHER_CTX *ctx);',
'void *EVP_CIPHER_CTX_get_app_data(const EVP_CIPHER_CTX *ctx);',
'void EVP_CIPHER_CTX_set_app_data(EVP_CIPHER_CTX *ctx, void *data);',
'int EVP_CIPHER_CTX_type(const EVP_CIPHER_CTX *ctx);',
'unsigned long EVP_CIPHER_CTX_flags(const EVP_CIPHER_CTX *ctx);',
'unsigned long EVP_CIPHER_CTX_mode(const EVP_CIPHER_CTX *ctx);',
'int EVP_CIPHER_param_to_asn1(EVP_CIPHER_CTX *c, ASN1_TYPE *type);',
'int EVP_CIPHER_asn1_to_param(EVP_CIPHER_CTX *c, ASN1_TYPE *type);',
]
INCLUDES = [
'#include "openssl/evp.h"',
]
FUNCTIONS = [
'const EVP_CIPHER *EVP_enc_null(void);',
'const EVP_CIPHER *EVP_des_ecb(void);',
'const EVP_CIPHER *EVP_des_ede(void);',
'const EVP_CIPHER *EVP_des_ede3(void);',
'const EVP_CIPHER *EVP_des_ede_ecb(void);',
'const EVP_CIPHER *EVP_des_ede3_ecb(void);',
'const EVP_CIPHER *EVP_des_cfb64(void);',
'const EVP_CIPHER *EVP_des_cfb1(void);',
'const EVP_CIPHER *EVP_des_cfb8(void);',
'const EVP_CIPHER *EVP_des_ede_cfb64(void);',
'const EVP_CIPHER *EVP_des_ede3_cfb64(void);',
'const EVP_CIPHER *EVP_des_ede3_cfb1(void);',
'const EVP_CIPHER *EVP_des_ede3_cfb8(void);',
'const EVP_CIPHER *EVP_des_ofb(void);',
'const EVP_CIPHER *EVP_des_ede_ofb(void);',
'const EVP_CIPHER *EVP_des_ede3_ofb(void);',
'const EVP_CIPHER *EVP_des_cbc(void);',
'const EVP_CIPHER *EVP_des_ede_cbc(void);',
'const EVP_CIPHER *EVP_des_ede3_cbc(void);',
'const EVP_CIPHER *EVP_desx_cbc(void);',
'const EVP_CIPHER *EVP_rc4(void);',
'const EVP_CIPHER *EVP_rc4_40(void);',
'const EVP_CIPHER *EVP_rc2_ecb(void);',
'const EVP_CIPHER *EVP_rc2_cbc(void);',
'const EVP_CIPHER *EVP_rc2_40_cbc(void);',
'const EVP_CIPHER *EVP_rc2_64_cbc(void);',
'const EVP_CIPHER *EVP_rc2_cfb64(void);',
'const EVP_CIPHER *EVP_rc2_ofb(void);',
'const EVP_CIPHER *EVP_bf_ecb(void);',
'const EVP_CIPHER *EVP_bf_cbc(void);',
'const EVP_CIPHER *EVP_bf_cfb64(void);',
'const EVP_CIPHER *EVP_bf_ofb(void);',
'const EVP_CIPHER *EVP_cast5_ecb(void);',
'const EVP_CIPHER *EVP_cast5_cbc(void);',
'const EVP_CIPHER *EVP_cast5_cfb64(void);',
'const EVP_CIPHER *EVP_cast5_ofb(void);',
# 'const EVP_CIPHER *EVP_rc5_32_12_16_ecb(void);',
# 'const EVP_CIPHER *EVP_rc5_32_12_16_cbc(void);',
# 'const EVP_CIPHER *EVP_rc5_32_12_16_cfb64(void);',
# 'const EVP_CIPHER *EVP_rc5_32_12_16_ofb(void);',
'const EVP_CIPHER *EVP_aes_128_ecb(void);',
'const EVP_CIPHER *EVP_aes_128_cbc(void);',
'const EVP_CIPHER *EVP_aes_128_cfb1(void);',
'const EVP_CIPHER *EVP_aes_128_cfb8(void);',
'const EVP_CIPHER *EVP_aes_128_cfb128(void);',
'const EVP_CIPHER *EVP_aes_128_ofb(void);',
'const EVP_CIPHER *EVP_aes_192_ecb(void);',
'const EVP_CIPHER *EVP_aes_192_cbc(void);',
'const EVP_CIPHER *EVP_aes_192_cfb1(void);',
'const EVP_CIPHER *EVP_aes_192_cfb8(void);',
'const EVP_CIPHER *EVP_aes_192_cfb128(void);',
'const EVP_CIPHER *EVP_aes_192_ofb(void);',
'const EVP_CIPHER *EVP_aes_256_ecb(void);',
'const EVP_CIPHER *EVP_aes_256_cbc(void);',
'const EVP_CIPHER *EVP_aes_256_cfb1(void);',
'const EVP_CIPHER *EVP_aes_256_cfb8(void);',
'const EVP_CIPHER *EVP_aes_256_cfb128(void);',
'const EVP_CIPHER *EVP_aes_256_ofb(void);',
# 'const EVP_CIPHER *EVP_camellia_128_ecb(void);',
# 'const EVP_CIPHER *EVP_camellia_128_cbc(void);',
# 'const EVP_CIPHER *EVP_camellia_128_cfb1(void);',
# 'const EVP_CIPHER *EVP_camellia_128_cfb8(void);',
# 'const EVP_CIPHER *EVP_camellia_128_cfb128(void);',
# 'const EVP_CIPHER *EVP_camellia_128_ofb(void);',
# 'const EVP_CIPHER *EVP_camellia_192_ecb(void);',
# 'const EVP_CIPHER *EVP_camellia_192_cbc(void);',
# 'const EVP_CIPHER *EVP_camellia_192_cfb1(void);',
# 'const EVP_CIPHER *EVP_camellia_192_cfb8(void);',
# 'const EVP_CIPHER *EVP_camellia_192_cfb128(void);',
# 'const EVP_CIPHER *EVP_camellia_192_ofb(void);',
# 'const EVP_CIPHER *EVP_camellia_256_ecb(void);',
# 'const EVP_CIPHER *EVP_camellia_256_cbc(void);',
# 'const EVP_CIPHER *EVP_camellia_256_cfb1(void);',
# 'const EVP_CIPHER *EVP_camellia_256_cfb8(void);',
# 'const EVP_CIPHER *EVP_camellia_256_cfb128(void);',
# 'const EVP_CIPHER *EVP_camellia_256_ofb(void);',
]
INCLUDES = [
'#include "openssl/evp.h"',
]
TYPES = [
'static const int EVP_MAX_MD_SIZE;',
'static const int EVP_MAX_KEY_LENGTH;',
'static const int EVP_MAX_IV_LENGTH;',
'static const int EVP_MAX_BLOCK_LENGTH;',
'struct env_md_ctx_st { ...; };',
'typedef ... EVP_MD;',
'typedef struct env_md_ctx_st EVP_MD_CTX;',
]
FUNCTIONS = [
'void EVP_cleanup(void);',
'void EVP_MD_CTX_init(EVP_MD_CTX *ctx);',
'EVP_MD_CTX *EVP_MD_CTX_create(void);',
'int EVP_DigestInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type, ENGINE *impl);',
'int EVP_DigestUpdate(EVP_MD_CTX *ctx, const void *d, size_t cnt);',
'int EVP_DigestFinal_ex(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *s);',
'int EVP_MD_CTX_cleanup(EVP_MD_CTX *ctx);',
'void EVP_MD_CTX_destroy(EVP_MD_CTX *ctx);',
'int EVP_MD_CTX_copy_ex(EVP_MD_CTX *out,const EVP_MD_CTX *in);',
'int EVP_DigestInit(EVP_MD_CTX *ctx, const EVP_MD *type);',
'int EVP_DigestFinal(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *s);',
'int EVP_MD_CTX_copy(EVP_MD_CTX *out,EVP_MD_CTX *in);',
'const EVP_MD *EVP_get_digestbyname(const char *name);',
'const EVP_MD *EVP_get_digestbynid(int n);',
'const EVP_MD *EVP_get_digestbyobj(const ASN1_OBJECT *o);',
'const EVP_MD *EVP_md_null(void);',
# 'const EVP_MD *EVP_md2(void);',
'const EVP_MD *EVP_md4(void);',
'const EVP_MD *EVP_md5(void);',
'const EVP_MD *EVP_sha(void);',
'const EVP_MD *EVP_sha1(void);',
'const EVP_MD *EVP_dss(void);',
'const EVP_MD *EVP_dss1(void);',
'const EVP_MD *EVP_ecdsa(void);',
'const EVP_MD *EVP_sha224(void);',
'const EVP_MD *EVP_sha256(void);',
'const EVP_MD *EVP_sha384(void);',
'const EVP_MD *EVP_sha512(void);',
# 'const EVP_MD *EVP_mdc(void);',
'const EVP_MD *EVP_ripemd160(void);',
# 'const EVP_MD *EVP_dsa_sha(void);',
# 'const EVP_MD *EVP_dsa_sha1(void);',
'int EVP_MD_type(const EVP_MD *md);',
'int EVP_MD_pkey_type(const EVP_MD *md);',
'int EVP_MD_size(const EVP_MD *md);',
'int EVP_MD_block_size(const EVP_MD *md);',
'const EVP_MD *EVP_MD_CTX_md(const EVP_MD_CTX *ctx);',
'int EVP_MD_CTX_size(const EVP_MD_CTX *ctx);',
'int EVP_MD_CTX_block_size(const EVP_MD_CTX *ctx);',
'int EVP_MD_CTX_type(const EVP_MD_CTX *ctx);',
]
INCLUDES = [
'#include <openssl/hmac.h>',
]
TYPES = [
'struct hmac_ctx_st { ...; };',
'typedef struct hmac_ctx_st HMAC_CTX;',
]
FUNCTIONS = [
'unsigned char *HMAC(const EVP_MD *evp_md, const void *key,'
'int key_len, const unsigned char *d, int n,'
'unsigned char *md, unsigned int *md_len);',
'void HMAC_CTX_init(HMAC_CTX *ctx);',
'void HMAC_Init(HMAC_CTX *ctx, const void *key, int key_len, const EVP_MD *md);',
'void HMAC_Init_ex(HMAC_CTX *ctx, const void *key, int key_len, const EVP_MD *md, ENGINE *impl);',
'void HMAC_Update(HMAC_CTX *ctx, const unsigned char *data, int len);',
'void HMAC_Final(HMAC_CTX *ctx, unsigned char *md, unsigned int *len);',
'void HMAC_CTX_cleanup(HMAC_CTX *ctx);',
'void HMAC_cleanup(HMAC_CTX *ctx);',
]
TYPES = [
'static const int NID_undef;',
'static const int NID_dsa;',
'static const int NID_dsaWithSHA;',
'static const int NID_dsaWithSHA1;',
# 'static const int NID_md2;',
'static const int NID_md4;',
'static const int NID_md5;',
'static const int NID_mdc2;',
'static const int NID_ripemd160;',
'static const int NID_sha;',
'static const int NID_sha1;',
'static const int NID_sha256;',
'static const int NID_sha384;',
'static const int NID_sha512;',
'static const int NID_sha224;',
'static const int NID_sha;',
'static const int NID_ecdsa_with_SHA1;',
'static const int NID_ecdsa_with_SHA224;',
'static const int NID_ecdsa_with_SHA256;',
'static const int NID_ecdsa_with_SHA384;',
'static const int NID_ecdsa_with_SHA512;',
]
INCLUDES = [
'#include <openssl/objects.h>',
]
TYPES = [
'static const int OBJ_NAME_TYPE_UNDEF;',
'static const int OBJ_NAME_TYPE_MD_METH;',
'static const int OBJ_NAME_TYPE_CIPHER_METH;',
'static const int OBJ_NAME_TYPE_PKEY_METH;',
'static const int OBJ_NAME_TYPE_COMP_METH;',
'static const int OBJ_NAME_TYPE_NUM;',
'struct obj_name_st { int type; int alias; const char *name; const char *data; ...; };',
'typedef struct obj_name_st OBJ_NAME;',
]
FUNCTIONS = [
'ASN1_OBJECT *OBJ_nid2obj(int n);',
'const char *OBJ_nid2ln(int n);',
'const char *OBJ_nid2sn(int n);',
'int OBJ_obj2nid(const ASN1_OBJECT *o);',
'int OBJ_ln2nid(const char *ln);',
'int OBJ_sn2nid(const char *sn);',
'int OBJ_txt2nid(const char *s);',
'ASN1_OBJECT * OBJ_txt2obj(const char *s, int no_name);',
'int OBJ_obj2txt(char *buf, int buf_len, const ASN1_OBJECT *a, int no_name);',
'int OBJ_cmp(const ASN1_OBJECT *a,const ASN1_OBJECT *b);',
'ASN1_OBJECT * OBJ_dup(const ASN1_OBJECT *o);',
'int OBJ_create(const char *oid,const char *sn,const char *ln);',
'void OBJ_cleanup(void);',
'int OBJ_NAME_init(void);',
'void OBJ_NAME_do_all(int type,void (*fn)(const OBJ_NAME *,void *arg), void *arg);',
'void OBJ_NAME_do_all_sorted(int type,void (*fn)(const OBJ_NAME *,void *arg), void *arg);',
]
INCLUDES = [
'#include "openssl/ssl.h"',
]
SETUP = [
'OpenSSL_add_all_digests',
'OpenSSL_add_all_ciphers',
]
FUNCTIONS = [
"void OpenSSL_add_all_algorithms(void);",
"void OpenSSL_add_all_ciphers(void);",
"void OpenSSL_add_all_digests(void);",
]
INCLUDES = [
'#include "openssl/evp.h"',
]
FUNCTIONS = [
'int PKCS5_PBKDF2_HMAC_SHA1(const char *pass, int passlen,'
'const unsigned char *salt, int saltlen, int iter,'
'int keylen, unsigned char *out);',
'int EVP_BytesToKey(const EVP_CIPHER *type,const EVP_MD *md,'
'const unsigned char *salt,'
'const unsigned char *data, int datal, int count,'
'unsigned char *key,unsigned char *iv);',
]
INCLUDES = [
'#include <openssl/rand.h>',
]
FUNCTIONS = [
'void RAND_seed(const void *buf, int num);',
'void RAND_add(const void *buf, int num, double entropy);',
'int RAND_status(void);',
'int RAND_egd(const char *path);',
'int RAND_egd_bytes(const char *path, int bytes);',
'int RAND_query_egd_bytes(const char *path, unsigned char *buf, int bytes);',
'const char *RAND_file_name(char *buf, size_t num);',
'int RAND_load_file(const char *filename, long max_bytes);',
'int RAND_write_file(const char *filename);',
'void RAND_cleanup(void);',
'int RAND_bytes(unsigned char *buf, int num);',
'int RAND_pseudo_bytes(unsigned char *buf, int num);',
]
from functools import wraps
INCLUDES = [
'#include "openssl/ssl.h"',
]
SETUP = [
'SSL_library_init',
]
TYPES = [
# Internally invented symbol to tell us if SSLv2 is supported
'static const int OPENTLS_NO_SSL2;',
'typedef ... SSL_METHOD;',
'typedef ... SSL_CTX;',
]
FUNCTIONS = [
'int SSL_library_init(void);',
# methods
'const SSL_METHOD *SSLv3_method(void);',
'const SSL_METHOD *SSLv3_server_method(void);',
'const SSL_METHOD *SSLv3_client_method(void);',
'const SSL_METHOD *TLSv1_method(void);',
'const SSL_METHOD *TLSv1_server_method(void);',
'const SSL_METHOD *TLSv1_client_method(void);',
'const SSL_METHOD *SSLv23_method(void);',
'const SSL_METHOD *SSLv23_server_method(void);',
'const SSL_METHOD *SSLv23_client_method(void);',
# SSLv2 support is compiled out of some versions of OpenSSL. These will
# get special support when we generate the bindings so that if they are
# available they will be wrapped, but if they are not they won't cause
# problems (like link errors).
'SSL_METHOD *SSLv2_method(void);',
'SSL_METHOD *SSLv2_server_method(void);',
'SSL_METHOD *SSLv2_client_method(void);',
# context
'SSL_CTX *SSL_CTX_new(SSL_METHOD *method);',
'void SSL_CTX_free(SSL_CTX *ctx);',
]
C_CUSTOMIZATION = [
"""
#ifdef OPENSSL_NO_SSL2
static const int OPENTLS_NO_SSL2 = 1;
SSL_METHOD* (*SSLv2_method)(void) = NULL;
SSL_METHOD* (*SSLv2_client_method)(void) = NULL;
SSL_METHOD* (*SSLv2_server_method)(void) = NULL;
#else
static const int OPENTLS_NO_SSL2 = 0;
#endif
"""]
def _not_implemented_override(wrapped):
"""
Decorator to help define an override which just raises NotImplementedError,
useful to define friendly versions of APIs which are not actually available
in the version of OpenSSL currently in use.
wrapped is the Python function which will override the cffi-defined
wrapper.
This returns a factory to create the override function. It expects to be
called by the tls.c.api setup machinery. See tls/c/__init__.py.
"""
@wraps(wrapped)
def _not_implemented_factory(api, from_openssl):
"""
If SSLv2 is not supported by the OpenSSL library represented by the
given api object, create an override function which raises
NotImplementedError instead of trying to call the requested API (which
would probably result in a null pointer dereference).
"""
if api.OPENTLS_NO_SSL2:
# SSLv2 is unsupported, give back the safe wrapper
@wraps(wrapped)
def not_implemented(*args, **kwargs):
raise NotImplementedError()
return not_implemented
else:
# SSLv2 is supported, give back the original function
return from_openssl
return _not_implemented_factory
@_not_implemented_override
def SSLv2_method():
pass
@_not_implemented_override
def SSLv2_client_method():
pass
@_not_implemented_override
def SSLv2_server_method():
pass
OVERRIDES = [
SSLv2_method, SSLv2_client_method, SSLv2_server_method,
]
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment