diff options
| author | Alex Gaynor <alex.gaynor@gmail.com> | 2013-08-06 19:25:52 -0700 |
|---|---|---|
| committer | Alex Gaynor <alex.gaynor@gmail.com> | 2013-08-06 19:31:02 -0700 |
| commit | c62e91f3eca01c7e06974994fc16bce06fbffabf (patch) | |
| tree | 4372729eaf6f3f6b3fdd43baa882270f6373bd82 /cryptography | |
| parent | 8d7fb752184873f2aa3c2cc375d1556d43920d1b (diff) | |
| download | cryptography-c62e91f3eca01c7e06974994fc16bce06fbffabf.tar.gz cryptography-c62e91f3eca01c7e06974994fc16bce06fbffabf.tar.bz2 cryptography-c62e91f3eca01c7e06974994fc16bce06fbffabf.zip | |
Initial commit. Migrates over basic project files, and the OpenSSL bindings
from OpenTLS.
Diffstat (limited to 'cryptography')
| -rw-r--r-- | cryptography/c/__init__.py | 4 | ||||
| -rw-r--r-- | cryptography/c/api.py | 153 | ||||
| -rw-r--r-- | cryptography/c/asn1.py | 43 | ||||
| -rw-r--r-- | cryptography/c/bio.py | 118 | ||||
| -rw-r--r-- | cryptography/c/bio_filter.py | 42 | ||||
| -rw-r--r-- | cryptography/c/bio_sink.py | 50 | ||||
| -rw-r--r-- | cryptography/c/err.py | 47 | ||||
| -rw-r--r-- | cryptography/c/evp.py | 11 | ||||
| -rw-r--r-- | cryptography/c/evp_cipher.py | 68 | ||||
| -rw-r--r-- | cryptography/c/evp_cipher_listing.py | 82 | ||||
| -rw-r--r-- | cryptography/c/evp_md.py | 56 | ||||
| -rw-r--r-- | cryptography/c/hmac.py | 21 | ||||
| -rw-r--r-- | cryptography/c/nid.py | 23 | ||||
| -rw-r--r-- | cryptography/c/obj.py | 33 | ||||
| -rw-r--r-- | cryptography/c/openssl.py | 14 | ||||
| -rw-r--r-- | cryptography/c/pkcs5.py | 13 | ||||
| -rw-r--r-- | cryptography/c/rand.py | 18 | ||||
| -rw-r--r-- | cryptography/c/ssl.py | 108 | ||||
| -rw-r--r-- | cryptography/c/ssleay.py | 16 | ||||
| -rw-r--r-- | cryptography/c/stdio.py | 12 |
20 files changed, 932 insertions, 0 deletions
diff --git a/cryptography/c/__init__.py b/cryptography/c/__init__.py new file mode 100644 index 00000000..07927848 --- /dev/null +++ b/cryptography/c/__init__.py @@ -0,0 +1,4 @@ +from cryptography.c.api import api + + +__all__ = ["api"] diff --git a/cryptography/c/api.py b/cryptography/c/api.py new file mode 100644 index 00000000..f33ddda9 --- /dev/null +++ b/cryptography/c/api.py @@ -0,0 +1,153 @@ +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() diff --git a/cryptography/c/asn1.py b/cryptography/c/asn1.py new file mode 100644 index 00000000..1979f85d --- /dev/null +++ b/cryptography/c/asn1.py @@ -0,0 +1,43 @@ +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);', +] diff --git a/cryptography/c/bio.py b/cryptography/c/bio.py new file mode 100644 index 00000000..ea1f936e --- /dev/null +++ b/cryptography/c/bio.py @@ -0,0 +1,118 @@ +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);', +] diff --git a/cryptography/c/bio_filter.py b/cryptography/c/bio_filter.py new file mode 100644 index 00000000..a29d8d8c --- /dev/null +++ b/cryptography/c/bio_filter.py @@ -0,0 +1,42 @@ +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);', +] diff --git a/cryptography/c/bio_sink.py b/cryptography/c/bio_sink.py new file mode 100644 index 00000000..578d7a95 --- /dev/null +++ b/cryptography/c/bio_sink.py @@ -0,0 +1,50 @@ +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);', +] diff --git a/cryptography/c/err.py b/cryptography/c/err.py new file mode 100644 index 00000000..b59ece93 --- /dev/null +++ b/cryptography/c/err.py @@ -0,0 +1,47 @@ +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);', +] diff --git a/cryptography/c/evp.py b/cryptography/c/evp.py new file mode 100644 index 00000000..a063a222 --- /dev/null +++ b/cryptography/c/evp.py @@ -0,0 +1,11 @@ +INCLUDES = [ + '#include "openssl/evp.h"', +] + +TEARDOWN = [ + 'EVP_cleanup', +] + +TYPES = [ + 'typedef ... ENGINE;', +] diff --git a/cryptography/c/evp_cipher.py b/cryptography/c/evp_cipher.py new file mode 100644 index 00000000..4e6ae729 --- /dev/null +++ b/cryptography/c/evp_cipher.py @@ -0,0 +1,68 @@ +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);', +] diff --git a/cryptography/c/evp_cipher_listing.py b/cryptography/c/evp_cipher_listing.py new file mode 100644 index 00000000..5a0812c2 --- /dev/null +++ b/cryptography/c/evp_cipher_listing.py @@ -0,0 +1,82 @@ +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);', +] diff --git a/cryptography/c/evp_md.py b/cryptography/c/evp_md.py new file mode 100644 index 00000000..ce3583e5 --- /dev/null +++ b/cryptography/c/evp_md.py @@ -0,0 +1,56 @@ +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);', +] diff --git a/cryptography/c/hmac.py b/cryptography/c/hmac.py new file mode 100644 index 00000000..aec734a8 --- /dev/null +++ b/cryptography/c/hmac.py @@ -0,0 +1,21 @@ +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);', +] diff --git a/cryptography/c/nid.py b/cryptography/c/nid.py new file mode 100644 index 00000000..6d55a058 --- /dev/null +++ b/cryptography/c/nid.py @@ -0,0 +1,23 @@ +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;', +] diff --git a/cryptography/c/obj.py b/cryptography/c/obj.py new file mode 100644 index 00000000..6b5bf67e --- /dev/null +++ b/cryptography/c/obj.py @@ -0,0 +1,33 @@ +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);', +] diff --git a/cryptography/c/openssl.py b/cryptography/c/openssl.py new file mode 100644 index 00000000..0fc9d706 --- /dev/null +++ b/cryptography/c/openssl.py @@ -0,0 +1,14 @@ +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);", +] diff --git a/cryptography/c/pkcs5.py b/cryptography/c/pkcs5.py new file mode 100644 index 00000000..423f5e64 --- /dev/null +++ b/cryptography/c/pkcs5.py @@ -0,0 +1,13 @@ +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);', +] diff --git a/cryptography/c/rand.py b/cryptography/c/rand.py new file mode 100644 index 00000000..ce7cd1bc --- /dev/null +++ b/cryptography/c/rand.py @@ -0,0 +1,18 @@ +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);', +] diff --git a/cryptography/c/ssl.py b/cryptography/c/ssl.py new file mode 100644 index 00000000..25dd006a --- /dev/null +++ b/cryptography/c/ssl.py @@ -0,0 +1,108 @@ +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, +] diff --git a/cryptography/c/ssleay.py b/cryptography/c/ssleay.py new file mode 100644 index 00000000..a75d664c --- /dev/null +++ b/cryptography/c/ssleay.py @@ -0,0 +1,16 @@ +INCLUDES = [ + '#include "openssl/ssl.h"', +] + +TYPES = [ + 'static const int SSLEAY_VERSION;', + 'static const int SSLEAY_CFLAGS;', + 'static const int SSLEAY_BUILT_ON;', + 'static const int SSLEAY_PLATFORM;', + 'static const int SSLEAY_DIR;', +] + +FUNCTIONS = [ + "long SSLeay(void);", + "const char* SSLeay_version(int);", +] diff --git a/cryptography/c/stdio.py b/cryptography/c/stdio.py new file mode 100644 index 00000000..553b6fc9 --- /dev/null +++ b/cryptography/c/stdio.py @@ -0,0 +1,12 @@ +INCLUDES = [ + '#include <stdio.h>', +] + +TYPES = [ +] + +FUNCTIONS = [ + 'FILE *fdopen(int fildes, const char *mode);', + 'FILE *fopen(const char *restrict filename, const char *restrict mode);', + 'FILE *freopen(const char *restrict filename, const char *restrict mode, FILE *restrict stream);', +] |
