41 #define NCBI_USE_ERRCODE_X Connect_TLS
44 #if defined(HAVE_LIBMBEDTLS) || defined(NCBI_CXX_TOOLKIT)
46 # ifdef HAVE_LIBMBEDTLS
69 # define NCBI_NOTSUPPORTED ENOTSUP
70 # elif defined(ENOSYS)
71 # define NCBI_NOTSUPPORTED ENOSYS
73 # define NCBI_NOTSUPPORTED EINVAL
78 # define mbedtls_x509_crt void
79 # define mbedtls_pk_context void
90 #if defined(HAVE_LIBMBEDTLS) || defined(NCBI_CXX_TOOLKIT)
92 # if defined(MBEDTLS_THREADING_ALT) && defined(NCBI_THREADS)
93 # ifdef MBEDTLS_THREADING_PTHREAD
94 # error "MBEDTLS_THREADING_ALT and MBEDTLS_THREADING_PTHREAD conflict"
96 static void mbtls_user_mutex_init(
MT_LOCK* lock)
100 static void mbtls_user_mutex_deinit(
MT_LOCK* lock)
111 static int mbtls_user_mutex_lock(
MT_LOCK* lock)
127 static int mbtls_user_mutex_unlock(
MT_LOCK* lock)
164 const char*
file,
int line,
165 const char* message);
167 static int x_MbedTlsPush (
void*,
const unsigned char*,
size_t);
184 const char*
file,
int line,
188 size_t len = message ? strlen(message) : 0;
189 if (!
len || *message ==
'\n')
191 if (message[
len - 1] ==
'\n')
194 (
"MBEDTLS%d: %.*s", level, (
int)
len, message));
207 status = r_status != eIO_Closed && w_status != eIO_Closed
208 ? r_status > w_status ? r_status : w_status
281 CORE_TRACEF((
"MBEDTLS error %d -> CONNECT MBEDTLS status %s",
324 error ?
"error" :
"errno", x_err));
331 else if (!(
error = errno))
360 "Server-side SSL not yet supported with MBEDTLS");
369 (
"%s credentials in MBEDTLS session",
394 (session, xcred->
cert, xcred->
pkey)) != 0)) {
404 CORE_TRACEF((
"MbedTlsCreate(): Leave(%p)", session));
431 size_t alpn_len = alpn ? strlen(alpn) : 0;
435 size_t sslv_len = sslv ? strlen(sslv) : 0;
439 size_t ciph_len = ciph ? strlen(ciph) : 0;
440 size_t len = alpn_len + sslv_len + ciph_len;
443 else if ((*desc = (
char*)
malloc(
len + 3)) != 0) {
446 memcpy(ptr, alpn, alpn_len);
452 memcpy(ptr, sslv, sslv_len);
458 memcpy(ptr, ciph, ciph_len);
468 CORE_TRACEF((
"MbedTlsOpen(%p): Leave(%d)", session, status));
513 size_t x_written = 0;
522 n_written += (
ssize_t) x_written;
524 data = data + x_written;
527 return (
int) n_written;
537 size_t* n_done,
int*
error)
545 (
unsigned char*)
buf, n_todo);
546 assert(x_read < 0 || (
size_t) x_read <= n_todo);
556 *n_done = (size_t) x_read;
562 size_t n_todo,
size_t* n_done,
int*
error)
570 (
const unsigned char*) data, n_todo);
571 assert(x_written < 0 || (
size_t) x_written <= n_todo);
573 if (x_written <= 0) {
581 *n_done = (size_t) x_written;
587 size_t n_todo,
size_t* n_done,
int*
error)
596 size_t x_todo = n_todo > max_size ? max_size : n_todo;
605 if (x_todo != x_done)
608 data = (
const char*) data + x_done;
643 CORE_TRACEF((
"MbedTlsDelete(%p): Enter", session));
648 CORE_TRACEF((
"MbedTlsDelete(%p): Leave", session));
671 # ifdef MBEDTLS_THREADING_PTHREAD
673 # elif defined(MBEDTLS_THREADING_ALT) && defined(NCBI_THREADS)
678 mbtls_user_mutex_deinit,
679 mbtls_user_mutex_lock,
680 mbtls_user_mutex_unlock);
685 # elif !defined(NCBI_NO_THREADS) && defined(_MT)
687 "MBEDTLS locking uninited: Unknown threading model");
709 # if defined(MBEDTLS_THREADING_ALT) && defined(NCBI_THREADS)
718 static const char kMbedTls[] =
719 # ifdef HAVE_LIBMBEDTLS
733 (
"%s version mismatch: %s headers vs. %s runtime",
841 #if defined(HAVE_LIBMBEDTLS) || defined(NCBI_CXX_TOOLKIT)
852 #if !defined(HAVE_LIBMBEDTLS) && !defined(NCBI_CXX_TOOLKIT)
859 #define ALIGN2(s, a) ((((s) + ((a) - 1)) / (a)) * (a))
860 #define ALIGN(s) ALIGN2(s, sizeof(double))
868 if (xcert && xpkey) {
870 size +=
sizeof(*xcred);
875 if (xcert && xpkey) {
877 ((
char*) cred + 2*
ALIGN(
sizeof(*cred)));
887 #if defined(HAVE_LIBMBEDTLS) || defined(NCBI_CXX_TOOLKIT)
895 memset(xcred, 0,
sizeof(*xcred));
898 switch (cred->
type / 100) {
900 strcpy(who,
"GNUTLS");
903 strcpy(who,
"MBEDTLS");
906 sprintf(who,
"TLS 0x%08X", cred->
type);
910 (
"Deleting unknown certificate credentials (%s/%u)",
911 who, cred->
type % 100));
927 const size_t size = (2*
ALIGN(
sizeof(*ncbi_cred))
928 +
ALIGN(
sizeof(*xcred))
930 +
sizeof(*xcred->
pkey));
937 (
"Cannot allocate NCBI_CRED (%lu bytes)",
938 (
unsigned long)
size));
943 ((
char*) ncbi_cred + 2*
ALIGN(
sizeof(*ncbi_cred)));
945 ((
char*) xcred +
ALIGN(
sizeof(*xcred)));
949 ncbi_cred->
data = xcred;
956 (
const unsigned char*)
cert, certsz ? certsz
957 : strlen((
const char*)
cert) + 1);
961 "mbedTLS cannot parse X.509 certificate");
967 err > 0 ?
":\n" :
"",
968 err > 0 ?
tmp :
""));
971 (
const unsigned char*)
pkey, pkeysz ? pkeysz
972 : strlen((
const char*)
pkey) + 1, 0, 0);
976 "mbedTLS cannot parse private key");
This file contains definitions and functions for the CTR_DRBG pseudorandom generator.
void mbedtls_ctr_drbg_free(mbedtls_ctr_drbg_context *ctx)
This function resets CTR_DRBG context to the state immediately after initial call of mbedtls_ctr_drbg...
void mbedtls_ctr_drbg_init(mbedtls_ctr_drbg_context *ctx)
This function initializes the CTR_DRBG context, and prepares it for mbedtls_ctr_drbg_seed() or mbedtl...
int mbedtls_ctr_drbg_seed(mbedtls_ctr_drbg_context *ctx, int(*f_entropy)(void *, unsigned char *, size_t), void *p_entropy, const unsigned char *custom, size_t len)
This function seeds and sets up the CTR_DRBG entropy source for future reseeds.
int mbedtls_ctr_drbg_random(void *p_rng, unsigned char *output, size_t output_len)
This function uses CTR_DRBG to generate random data.
Functions for controlling and providing debug output from the library.
void mbedtls_debug_set_threshold(int threshold)
Set the threshold error level to handle globally all debug output.
Entropy accumulator implementation.
void mbedtls_entropy_free(mbedtls_entropy_context *ctx)
Free the data in the context.
int mbedtls_entropy_func(void *data, unsigned char *output, size_t len)
Retrieve entropy from the accumulator (Maximum length: MBEDTLS_ENTROPY_BLOCK_SIZE) (Thread-safe if MB...
void mbedtls_entropy_init(mbedtls_entropy_context *ctx)
Initialize the context.
std::ofstream out("events_result.xml")
main entry point for tests
SOCKSSL NcbiSetupMbedTls(void)
Explicitly setup mbedTLS library to support SSL in ncbi_socket.h[pp].
#define DEF_CONN_TLS_LOGLEVEL
struct SNcbiCred * NCBI_CRED
Opaque type for credentials.
#define REG_CONN_TLS_LOGLEVEL
EIO_Status SOCK_Status(SOCK sock, EIO_Event direction)
Return low-level socket I/O status of *last* socket operation.
ESOCK_Side
Sides of socket.
NCBI_CRED NcbiCredMbedTls(void *xcert, void *xpkey)
Convert native mbedTLS certificate credentials' handles into an abstract toolkit handle.
MT_LOCK CORE_GetLOCK(void)
Get the lock handle that is to be used by the core internals.
ELOG_Level
Log severity level.
MT_LOCK MT_LOCK_AddRef(MT_LOCK lk)
Increment internal reference count by 1, then return "lk".
const char * IO_StatusStr(EIO_Status status)
Get the text form of an enum status value.
MT_LOCK MT_LOCK_Delete(MT_LOCK lk)
Decrement internal reference count by 1, and if it reaches 0, then destroy the handle,...
EIO_Event
I/O event (or direction).
#define MT_LOCK_Do(lk, how)
Call "lk->handler(lk->data, how)".
@ eMT_Unlock
unlock critical section
@ eMT_Lock
lock critical section
@ eIO_Timeout
timeout expired before any I/O succeeded
@ eIO_Interrupt
signal arrival prevented any I/O to succeed
@ eIO_NotSupported
operation is not supported or is not available
@ eIO_Success
everything is fine, no error occurred
@ eIO_Unknown
unknown I/O error (likely fatal but can retry)
@ eIO_InvalidArg
bad argument / parameter value(s) supplied
@ eIO_Open
also serves as no-event indicator in SOCK_Poll
const struct ncbi::grid::netcache::search::fields::SIZE size
EIO_Status(* FSSLPush)(SOCK sock, const void *data, size_t size, size_t *done, int logdata)
EIO_Status(* FSSLPull)(SOCK sock, void *buf, size_t size, size_t *done, int logdata)
const char * ConnNetInfo_GetValueInternal(const char *service, const char *param, char *value, size_t value_size, const char *def_value)
static int x_StatusToError(EIO_Status status, SOCK sock, EIO_Event direction)
void NcbiDeleteMbedTlsCertCredentials(NCBI_CRED cred)
static void x_MbedTlsExit(void)
static void s_MbedTlsExit(void)
static EIO_Status s_MbedTlsOpen(void *session, int *error, char **desc)
NCBI_CRED NcbiCreateMbedTlsCertCredentials(const void *cert, size_t certsz, const void *pkey, size_t pkeysz)
static EIO_Status x_MbedTlsWrite(void *session, const void *data, size_t n_todo, size_t *n_done, int *error)
static EIO_Status s_MbedTlsRead(void *session, void *buf, size_t size, size_t *done, int *error)
static int x_MbedTlsPull(void *, unsigned char *, size_t)
static volatile int s_MbedTlsLogLevel
static volatile FSSLPull s_Pull
static EIO_Status s_MbedTlsInit(FSSLPull pull, FSSLPush push)
static mbedtls_ssl_config s_MbedTlsConf
static EIO_Status s_MbedTlsClose(void *session, int how, int *error)
#define NCBI_NOTSUPPORTED
static mbedtls_entropy_context s_MbedTlsEntropy
static EIO_Status x_RetryStatus(SOCK sock, EIO_Event direction)
static const char * s_MbedTlsError(void *session, int error, char *buf, size_t size)
static int x_IfToLog(void)
static void s_MbedTlsDelete(void *session)
static volatile FSSLPush s_Push
static EIO_Status x_InitLocking(void)
static EIO_Status x_ErrorToStatus(int error, mbedtls_ssl_context *session, EIO_Event direction)
static EIO_Status s_MbedTlsWrite(void *session, const void *data, size_t size, size_t *done, int *error)
static void x_MbedTlsLogger(void *data, int level, const char *file, int line, const char *message)
static int x_MbedTlsPush(void *, const unsigned char *, size_t)
static mbedtls_ctr_drbg_context s_MbedTlsCtrDrbg
static void * s_MbedTlsCreate(ESOCK_Side side, SNcbiSSLctx *ctx, int *error)
#define CORE_DEBUG_ARG(arg)
#define CORE_LOGF_X(subcode, level, fmt_args)
#define CORE_LOG_ERRNO_EXX(subcode, level, error, descr, message)
#define CORE_LOGF_ERRNO_X(subcode, level, error, fmt_args)
#define CORE_TRACEF(fmt_args)
#define CORE_TRACE(message)
#define CORE_LOG_X(subcode, level, message)
#define mbedtls_version_get_string
#define mbedtls_pk_parse_key
#define mbedtls_ssl_set_hostname
#define mbedtls_x509_crt_init
#define mbedtls_version_check_feature
#define mbedtls_ssl_get_alpn_protocol
#define mbedtls_x509_crt_free
#define mbedtls_x509_crt_parse
#define mbedtls_ssl_get_output_max_frag_len
#define mbedtls_x509_crt_info
#define mbedtls_ssl_set_hs_own_cert
#define mbedtls_threading_set_alt
#define mbedtls_threading_free_alt
Network sockets abstraction layer to integrate Mbed TLS into a BSD-style sockets API.
#define MBEDTLS_ERR_NET_RECV_FAILED
Reading information from the socket failed.
#define MBEDTLS_ERR_NET_CONN_RESET
Connection was reset by peer.
#define MBEDTLS_ERR_NET_SEND_FAILED
Sending information through the socket failed.
Public Key abstraction layer.
const char * mbedtls_pk_get_name(const mbedtls_pk_context *ctx)
Access the type name.
void mbedtls_pk_init(mbedtls_pk_context *ctx)
Initialize a mbedtls_pk_context (as NONE).
void mbedtls_pk_free(mbedtls_pk_context *ctx)
Free the components of a mbedtls_pk_context.
Error to string translation.
void mbedtls_strerror(int errnum, char *buffer, size_t buflen)
Translate a mbed TLS error code into a string representation, Result is truncated if necessary and al...
Run-time version information.
#define MBEDTLS_VERSION_STRING
#define MBEDTLS_ERR_SSL_NON_FATAL
The alert message received indicates a non-fatal error.
const char * mbedtls_ssl_get_ciphersuite(const mbedtls_ssl_context *ssl)
Return the name of the current ciphersuite.
#define MBEDTLS_SSL_VERIFY_NONE
const char * mbedtls_ssl_get_version(const mbedtls_ssl_context *ssl)
Return the current SSL version (SSLv3/TLSv1/etc)
#define MBEDTLS_SSL_IS_CLIENT
void mbedtls_ssl_free(mbedtls_ssl_context *ssl)
Free referenced items in an SSL context and clear memory.
#define MBEDTLS_ERR_SSL_TIMEOUT
The operation timed out.
void mbedtls_ssl_conf_rng(mbedtls_ssl_config *conf, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Set the random number generator callback.
int mbedtls_ssl_handshake(mbedtls_ssl_context *ssl)
Perform the SSL handshake.
#define MBEDTLS_ERR_SSL_NO_USABLE_CIPHERSUITE
None of the common ciphersuites is usable (eg, no suitable certificate, see debug messages).
void mbedtls_ssl_conf_authmode(mbedtls_ssl_config *conf, int authmode)
Set the certificate verification mode Default: NONE on server, REQUIRED on client.
int mbedtls_ssl_write(mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len)
Try to write exactly 'len' application data bytes.
#define MBEDTLS_ERR_SSL_BAD_INPUT_DATA
Bad input parameters to function.
#define MBEDTLS_ERR_SSL_WANT_WRITE
Connection requires a write call.
#define MBEDTLS_ERR_SSL_WANT_READ
No data of requested type currently available on underlying transport.
#define MBEDTLS_SSL_IS_SERVER
void mbedtls_ssl_config_free(mbedtls_ssl_config *conf)
Free an SSL configuration context.
void mbedtls_ssl_init(mbedtls_ssl_context *ssl)
Initialize an SSL context Just makes the context ready for mbedtls_ssl_setup() or mbedtls_ssl_free()
void mbedtls_ssl_set_bio(mbedtls_ssl_context *ssl, void *p_bio, mbedtls_ssl_send_t *f_send, mbedtls_ssl_recv_t *f_recv, mbedtls_ssl_recv_timeout_t *f_recv_timeout)
Set the underlying BIO callbacks for write, read and read-with-timeout.
int mbedtls_ssl_config_defaults(mbedtls_ssl_config *conf, int endpoint, int transport, int preset)
Load reasonable default SSL configuration values.
int mbedtls_ssl_read(mbedtls_ssl_context *ssl, unsigned char *buf, size_t len)
Read at most 'len' application data bytes.
void mbedtls_ssl_conf_dbg(mbedtls_ssl_config *conf, void(*f_dbg)(void *, int, const char *, int, const char *), void *p_dbg)
Set the debug callback.
#define MBEDTLS_ERR_SSL_CONN_EOF
The connection indicated an EOF.
void mbedtls_ssl_config_init(mbedtls_ssl_config *conf)
Initialize an SSL configuration context Just makes the context ready for mbedtls_ssl_config_defaults(...
int mbedtls_ssl_close_notify(mbedtls_ssl_context *ssl)
Notify the peer that the connection is being closed.
#define MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY
The peer notified us that the connection is going to be closed.
#define MBEDTLS_ERR_SSL_UNKNOWN_CIPHER
An unknown cipher was received.
#define MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE
A fatal alert message was received from our peer.
#define MBEDTLS_SSL_TRANSPORT_STREAM
#define MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE
The requested feature is not available.
#define MBEDTLS_ERR_SSL_INTERNAL_ERROR
Internal error (eg, unexpected failure in lower-level module)
int mbedtls_ssl_setup(mbedtls_ssl_context *ssl, const mbedtls_ssl_config *conf)
Set up an SSL context for use.
#define MBEDTLS_SSL_PRESET_DEFAULT
mbedtls_pk_context * pkey
The CTR_DRBG context structure.
Entropy context structure.
SSL/TLS configuration to be shared between mbedtls_ssl_context structures.
Container for an X.509 certificate.
Threading abstraction layer.
#define MBEDTLS_ERR_THREADING_MUTEX_ERROR
Locking / unlocking / free failed with error code.
#define MBEDTLS_ERR_THREADING_BAD_INPUT_DATA
Bad input parameters to function.
#define MBEDTLS_ERR_THREADING_FEATURE_UNAVAILABLE
The selected feature is not available.
voidp calloc(uInt items, uInt size)