aboutsummaryrefslogtreecommitdiff
path: root/openssl-sys/src/openssl_shim.c
blob: 8ebe23ac2c0390db58dab5fe015ae8e0d62ea61d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include <openssl/hmac.h>
#include <openssl/ssl.h>
#include <openssl/dh.h>
#include <openssl/bn.h>

#if defined(__APPLE__) || defined(__linux)

#include<pthread.h>
#include<openssl/crypto.h>

unsigned long thread_id()
{
    return (unsigned long) pthread_self();
}

void rust_openssl_set_id_callback() {
  CRYPTO_set_id_callback(thread_id);
}

#else
// Openssl already handles Windows directly, so we don't
// need to explicitly set it

void rust_openssl_set_id_callback() {
  // We don't know how to set the callback for arbitrary OSes
  // Let openssl use its defaults and hope they work.
}

#endif


#if OPENSSL_VERSION_NUMBER < 0x10000000L
// Copied from openssl crypto/hmac/hmac.c
int HMAC_CTX_copy(HMAC_CTX *dctx, HMAC_CTX *sctx)
     {
     if (!EVP_MD_CTX_copy(&dctx->i_ctx, &sctx->i_ctx))
         goto err;
     if (!EVP_MD_CTX_copy(&dctx->o_ctx, &sctx->o_ctx))
         goto err;
     if (!EVP_MD_CTX_copy(&dctx->md_ctx, &sctx->md_ctx))
         goto err;
     memcpy(dctx->key, sctx->key, HMAC_MAX_MD_CBLOCK);
     dctx->key_length = sctx->key_length;
     dctx->md = sctx->md;
     return 1;
     err:
     return 0;
     }

int HMAC_Init_ex_shim(HMAC_CTX *ctx, const void *key, int key_len, const EVP_MD *md, ENGINE *impl) {
    HMAC_Init_ex(ctx, key, key_len, md, impl);
    return 1;
}

int HMAC_Update_shim(HMAC_CTX *ctx, const unsigned char *data, int len) {
    HMAC_Update(ctx, data, len);
    return 1;
}

int HMAC_Final_shim(HMAC_CTX *ctx, unsigned char *md, unsigned int *len) {
    HMAC_Final(ctx, md, len);
    return 1;
}

#else

int HMAC_Init_ex_shim(HMAC_CTX *ctx, const void *key, int key_len, const EVP_MD *md, ENGINE *impl) {
    return HMAC_Init_ex(ctx, key, key_len, md, impl);
}

int HMAC_Update_shim(HMAC_CTX *ctx, const unsigned char *data, int len) {
    return HMAC_Update(ctx, data, len);
}

int HMAC_Final_shim(HMAC_CTX *ctx, unsigned char *md, unsigned int *len) {
    return HMAC_Final(ctx, md, len);
}
#endif

// shims for OpenSSL macros

int BIO_eof_shim(BIO *b) {
    return BIO_eof(b);
}

void BIO_set_mem_eof_return_shim(BIO *b, int v) {
    BIO_set_mem_eof_return(b, v);
}

long SSL_CTX_set_options_shim(SSL_CTX *ctx, long options) {
    return SSL_CTX_set_options(ctx, options);
}

long SSL_CTX_get_options_shim(SSL_CTX *ctx) {
    return SSL_CTX_get_options(ctx);
}

long SSL_CTX_clear_options_shim(SSL_CTX *ctx, long options) {
    return SSL_CTX_clear_options(ctx, options);
}

long SSL_CTX_add_extra_chain_cert_shim(SSL_CTX *ctx, X509 *x509) {
    return SSL_CTX_add_extra_chain_cert(ctx, x509);
}

long SSL_CTX_set_read_ahead_shim(SSL_CTX *ctx, long m) {
    return SSL_CTX_set_read_ahead(ctx, m);
}

long SSL_CTX_set_tmp_dh_shim(SSL_CTX *ctx, DH *dh) {
    return SSL_CTX_set_tmp_dh(ctx, dh);
}

#if OPENSSL_VERSION_NUMBER >= 0x10002000L
int SSL_CTX_set_ecdh_auto_shim(SSL_CTX *ctx, int onoff) {
    return SSL_CTX_set_ecdh_auto(ctx, onoff);
}
#endif

DH *DH_new_from_params(BIGNUM *p, BIGNUM *g, BIGNUM *q) {
    DH *dh;

    if ((dh = DH_new()) == NULL) {
        return NULL;
    }
    dh->p = p;
    dh->g = g;
    dh->q = q;
    return dh;
}

long SSL_set_tlsext_host_name_shim(SSL *s, char *name) {
    return SSL_set_tlsext_host_name(s, name);
}

STACK_OF(X509_EXTENSION) *X509_get_extensions_shim(X509 *x) {
    return x->cert_info ? x->cert_info->extensions : NULL;
}