5 // Created by Martin Baulig on 14/11/15.
6 // Copyright (c) 2015 Xamarin. All rights reserved.
10 #include <btls-x509-verify-param.h>
17 #define debug_print(ptr,message) \
18 do { if (mono_btls_ssl_ctx_is_debug_enabled(ptr->ctx)) \
19 mono_btls_ssl_ctx_debug_printf (ptr->ctx, "%s:%d:%s(): " message, __FILE__, __LINE__, \
20 __func__); } while (0)
22 #define debug_printf(ptr,fmt, ...) \
23 do { if (mono_btls_ssl_ctx_is_debug_enabled(ptr->ctx)) \
24 mono_btls_ssl_ctx_debug_printf (ptr->ctx, "%s:%d:%s(): " fmt, __FILE__, __LINE__, \
25 __func__, __VA_ARGS__); } while (0)
27 STACK_OF(SSL_CIPHER
) *ssl_bytes_to_cipher_list (SSL
*s
, const CBS
*cbs
);
29 MONO_API MonoBtlsSsl
*
30 mono_btls_ssl_new (MonoBtlsSslCtx
*ctx
)
34 ptr
= calloc (1, sizeof (MonoBtlsSsl
));
36 ptr
->ctx
= mono_btls_ssl_ctx_up_ref (ctx
);
37 ptr
->ssl
= SSL_new (mono_btls_ssl_ctx_get_ctx (ptr
->ctx
));
43 mono_btls_ssl_destroy (MonoBtlsSsl
*ptr
)
45 mono_btls_ssl_close (ptr
);
51 mono_btls_ssl_ctx_free (ptr
->ctx
);
58 mono_btls_ssl_close (MonoBtlsSsl
*ptr
)
64 mono_btls_ssl_set_bio (MonoBtlsSsl
*ptr
, BIO
*bio
)
67 SSL_set_bio (ptr
->ssl
, bio
, bio
);
71 mono_btls_ssl_print_errors_cb (ERR_print_errors_callback_t callback
, void *ctx
)
73 ERR_print_errors_cb (callback
, ctx
);
77 mono_btls_ssl_use_certificate (MonoBtlsSsl
*ptr
, X509
*x509
)
79 return SSL_use_certificate (ptr
->ssl
, x509
);
83 mono_btls_ssl_use_private_key (MonoBtlsSsl
*ptr
, EVP_PKEY
*key
)
85 return SSL_use_PrivateKey (ptr
->ssl
, key
);
89 mono_btls_ssl_add_chain_certificate (MonoBtlsSsl
*ptr
, X509
*x509
)
91 return SSL_add1_chain_cert (ptr
->ssl
, x509
);
95 mono_btls_ssl_accept (MonoBtlsSsl
*ptr
)
97 return SSL_accept (ptr
->ssl
);
101 mono_btls_ssl_connect (MonoBtlsSsl
*ptr
)
103 return SSL_connect (ptr
->ssl
);
107 mono_btls_ssl_handshake (MonoBtlsSsl
*ptr
)
109 return SSL_do_handshake (ptr
->ssl
);
113 mono_btls_ssl_read (MonoBtlsSsl
*ptr
, void *buf
, int count
)
115 return SSL_read (ptr
->ssl
, buf
, count
);
119 mono_btls_ssl_write (MonoBtlsSsl
*ptr
, void *buf
, int count
)
121 return SSL_write (ptr
->ssl
, buf
, count
);
125 mono_btls_ssl_get_version (MonoBtlsSsl
*ptr
)
127 return SSL_version (ptr
->ssl
);
131 mono_btls_ssl_set_min_version (MonoBtlsSsl
*ptr
, int version
)
133 SSL_set_min_version (ptr
->ssl
, version
);
137 mono_btls_ssl_set_max_version (MonoBtlsSsl
*ptr
, int version
)
139 SSL_set_max_version (ptr
->ssl
, version
);
143 mono_btls_ssl_get_cipher (MonoBtlsSsl
*ptr
)
145 const SSL_CIPHER
*cipher
;
147 cipher
= SSL_get_current_cipher (ptr
->ssl
);
150 return (uint16_t)SSL_CIPHER_get_id (cipher
);
154 mono_btls_ssl_set_cipher_list (MonoBtlsSsl
*ptr
, const char *str
)
156 return SSL_set_cipher_list(ptr
->ssl
, str
);
160 mono_btls_ssl_get_ciphers (MonoBtlsSsl
*ptr
, uint16_t **data
)
162 STACK_OF(SSL_CIPHER
) *ciphers
;
167 ciphers
= SSL_get_ciphers (ptr
->ssl
);
171 count
= (int)sk_SSL_CIPHER_num (ciphers
);
173 *data
= OPENSSL_malloc (2 * count
);
177 for (i
= 0; i
< count
; i
++) {
178 const SSL_CIPHER
*cipher
= sk_SSL_CIPHER_value (ciphers
, i
);
179 (*data
) [i
] = (uint16_t) SSL_CIPHER_get_id (cipher
);
186 mono_btls_ssl_get_peer_certificate (MonoBtlsSsl
*ptr
)
188 return SSL_get_peer_certificate (ptr
->ssl
);
192 mono_btls_ssl_get_error (MonoBtlsSsl
*ptr
, int ret_code
)
194 return SSL_get_error (ptr
->ssl
, ret_code
);
198 mono_btls_ssl_set_verify_param (MonoBtlsSsl
*ptr
, const MonoBtlsX509VerifyParam
*param
)
200 return SSL_set1_param (ptr
->ssl
, mono_btls_x509_verify_param_peek_param (param
));
204 mono_btls_ssl_set_server_name (MonoBtlsSsl
*ptr
, const char *name
)
206 return SSL_set_tlsext_host_name (ptr
->ssl
, name
);
209 MONO_API
const char *
210 mono_btls_ssl_get_server_name (MonoBtlsSsl
*ptr
)
212 return SSL_get_servername (ptr
->ssl
, TLSEXT_NAMETYPE_host_name
);