Commit a8a10e64 authored by Steven Fackler's avatar Steven Fackler
Browse files

Split stuff requiring a shim out to a separate crate

parent 8139fadb
Loading
Loading
Loading
Loading
+16 −0
Original line number Diff line number Diff line
[package]
name = "openssl-sys-extras"
version = "0.6.7"
authors = ["Steven Fackler <sfackler@gmail.com>"]
links = "openssl_shim"
build = "build.rs"

[features]
ecdh_auto = []

[dependencies]
libc = "0.2"
openssl-sys = { version = "0.6.7", path = "../openssl-sys" }

[build-dependencies]
gcc = "0.3"
+77 −0
Original line number Diff line number Diff line
extern crate gcc;

use std::env;
use std::path::PathBuf;
use std::fs::File;
use std::io::Write as IoWrite;
use std::fmt::Write;

fn main() {
    let options_shim_file = generate_options_shim();
    let mut config = gcc::Config::new();

    if let Some(paths) = env::var_os("DEP_OPENSSL_INCLUDE") {
        for path in env::split_paths(&paths) {
            config.include(PathBuf::from(path));
        }
    }

    config.file("src/openssl_shim.c")
        .file(options_shim_file)
        .compile("libopenssl_shim.a");
}

macro_rules! import_options {
    ( $( $name:ident $val:expr  )* ) => {
       &[ $( (stringify!($name),$val), )* ]
    };
}

fn generate_options_shim() -> PathBuf {
    let options: &[(&'static str,u64)]=include!("src/ssl_options.rs");
    let mut shim = String::new();
    writeln!(shim,"#include <stdint.h>").unwrap();
    writeln!(shim,"#include <openssl/ssl.h>").unwrap();

    for &(name,value) in options {
        writeln!(shim,"#define RUST_{} UINT64_C({})",name,value).unwrap();
        writeln!(shim,"#ifndef {}",name).unwrap();
        writeln!(shim,"# define {} 0",name).unwrap();
        writeln!(shim,"#endif").unwrap();
    }

    writeln!(shim,"#define COPY_MASK ( \\").unwrap();

    let mut it=options.iter().peekable();
    while let Some(&(name,_))=it.next()  {
        let eol=match it.peek() {
            Some(_) => " | \\",
            None    => " )"
        };
        writeln!(shim,"    ((RUST_{0}==(uint64_t)(uint32_t){0})?RUST_{0}:UINT64_C(0)){1}",name,eol).unwrap();
    }

    writeln!(shim,"long rust_openssl_ssl_ctx_options_rust_to_c(uint64_t rustval) {{").unwrap();
    writeln!(shim,"    long cval=rustval&COPY_MASK;").unwrap();
    for &(name,_) in options {
        writeln!(shim,"    if (rustval&RUST_{0}) cval|={0};",name).unwrap();
    }
    writeln!(shim,"    return cval;").unwrap();
    writeln!(shim,"}}").unwrap();

    writeln!(shim,"uint64_t rust_openssl_ssl_ctx_options_c_to_rust(long cval) {{").unwrap();
    writeln!(shim,"    uint64_t rustval=cval&COPY_MASK;").unwrap();
    for &(name,_) in options {
        writeln!(shim,"    if (cval&{0}) rustval|=RUST_{0};",name).unwrap();
    }
    writeln!(shim,"    return rustval;").unwrap();
    writeln!(shim,"}}").unwrap();

    let out_dir = env::var("OUT_DIR").unwrap();
    let dest_file = PathBuf::from(&out_dir).join("ssl_ctx_options_shim.c");
    let mut f = File::create(&dest_file).unwrap();

    f.write_all(shim.as_bytes()).unwrap();

    dest_file
}
+64 −0
Original line number Diff line number Diff line
#![allow(non_upper_case_globals, non_snake_case)]

extern crate openssl_sys;
extern crate libc;

use libc::{c_int, c_uint, c_long, c_char};
use openssl_sys::{HMAC_CTX, EVP_MD, ENGINE, SSL_CTX, BIO, X509, stack_st_X509_EXTENSION, SSL, DH};

macro_rules! import_options {
    ( $( $name:ident $val:expr  )* ) => {
       $( pub const $name: u64 = $val; )*
    };
}

include!("ssl_options.rs");

pub unsafe fn SSL_CTX_set_options(ssl: *mut SSL_CTX, op: u64) -> u64 {
    rust_openssl_ssl_ctx_options_c_to_rust(SSL_CTX_set_options_shim(ssl, rust_openssl_ssl_ctx_options_rust_to_c(op)))
}

pub unsafe fn SSL_CTX_get_options(ssl: *mut SSL_CTX) -> u64 {
    rust_openssl_ssl_ctx_options_c_to_rust(SSL_CTX_get_options_shim(ssl))
}

pub unsafe fn SSL_CTX_clear_options(ssl: *mut SSL_CTX, op: u64) -> u64 {
    rust_openssl_ssl_ctx_options_c_to_rust(SSL_CTX_clear_options_shim(ssl, rust_openssl_ssl_ctx_options_rust_to_c(op)))
}

extern {
    fn rust_openssl_ssl_ctx_options_rust_to_c(rustval: u64) -> c_long;
    fn rust_openssl_ssl_ctx_options_c_to_rust(cval: c_long) -> u64;

    // Pre-1.0 versions of these didn't return anything, so the shims bridge that gap
    #[cfg_attr(not(target_os = "nacl"), link_name = "HMAC_Init_ex_shim")]
    pub fn HMAC_Init_ex(ctx: *mut HMAC_CTX, key: *const u8, keylen: c_int, md: *const EVP_MD, imple: *const ENGINE) -> c_int;
    #[cfg_attr(not(target_os = "nacl"), link_name = "HMAC_Final_shim")]
    pub fn HMAC_Final(ctx: *mut HMAC_CTX, output: *mut u8, len: *mut c_uint) -> c_int;
    #[cfg_attr(not(target_os = "nacl"), link_name = "HMAC_Update_shim")]
    pub fn HMAC_Update(ctx: *mut HMAC_CTX, input: *const u8, len: c_uint) -> c_int;

    // These functions are defined in OpenSSL as macros, so we shim them
    #[link_name = "BIO_eof_shim"]
    pub fn BIO_eof(b: *mut BIO) -> c_int;
    #[link_name = "BIO_set_nbio_shim"]
    pub fn BIO_set_nbio(b: *mut BIO, enabled: c_long) -> c_long;
    #[link_name = "BIO_set_mem_eof_return_shim"]
    pub fn BIO_set_mem_eof_return(b: *mut BIO, v: c_int);
    pub fn SSL_CTX_set_options_shim(ctx: *mut SSL_CTX, options: c_long) -> c_long;
    pub fn SSL_CTX_get_options_shim(ctx: *mut SSL_CTX) -> c_long;
    pub fn SSL_CTX_clear_options_shim(ctx: *mut SSL_CTX, options: c_long) -> c_long;
    #[link_name = "SSL_CTX_add_extra_chain_cert_shim"]
    pub fn SSL_CTX_add_extra_chain_cert(ctx: *mut SSL_CTX, x509: *mut X509) -> c_long;
    #[link_name = "SSL_CTX_set_read_ahead_shim"]
    pub fn SSL_CTX_set_read_ahead(ctx: *mut SSL_CTX, m: c_long) -> c_long;
    #[cfg(feature = "ecdh_auto")]
    #[link_name = "SSL_CTX_set_ecdh_auto_shim"]
    pub fn SSL_CTX_set_ecdh_auto(ssl: *mut SSL_CTX, onoff: c_int) -> c_int;
    #[link_name = "SSL_set_tlsext_host_name_shim"]
    pub fn SSL_set_tlsext_host_name(s: *mut SSL, name: *const c_char) -> c_long;
    #[link_name = "SSL_CTX_set_tmp_dh_shim"]
    pub fn SSL_CTX_set_tmp_dh(s: *mut SSL, dh: *const DH) -> c_long;
    #[link_name = "X509_get_extensions_shim"]
    pub fn X509_get_extensions(x: *mut X509) -> *mut stack_st_X509_EXTENSION;
}
Loading