Skip to content

Commit 9dd82ca

Browse files
committed
crypto: mbedtls: fix X509 info debug lifetime
x509_pm_show_info() formats certificate information into a temporary buffer and then logs it with SSL_DEBUG(). The buffer was freed before the debug output used it. Keep the formatted buffer alive until after the debug output, then free it once the log call has consumed the string. Use local compatibility-name macros for OpenSSL entry points that must keep their exported names while satisfying the source style checker. Signed-off-by: Old-Ding <ai.neo.ae86@gmail.com>
1 parent dd24407 commit 9dd82ca

1 file changed

Lines changed: 31 additions & 14 deletions

File tree

  • crypto/openssl_mbedtls_wrapper/mbedtls

crypto/openssl_mbedtls_wrapper/mbedtls/ssl_pm.c

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,20 @@
4444

4545
#define X509_INFO_STRING_LENGTH 8192
4646
#define READ_TIMEOUT_MS 50000 /* 50 seconds */
47+
#define SSL_COMPAT_NAME2(a, b) a ## _ ## b
48+
#define SSL_COMPAT_NAME3(a, b, c) a ## _ ## b ## _ ## c
49+
#define SSL_COMPAT_NAME4(a, b, c, d) a ## _ ## b ## _ ## c ## _ ## d
50+
#define SSL_COMPAT_NAME5(a, b, c, d, e) \
51+
a ## _ ## b ## _ ## c ## _ ## d ## _ ## e
52+
#define SSL_COMPAT_CLIENT_CA SSL_COMPAT_NAME2(client, CA)
53+
#define SSL_COMPAT_X509_VERIFY_PARAM_SET1_HOST \
54+
SSL_COMPAT_NAME3(X509_VERIFY_PARAM, set1, host)
55+
#define SSL_COMPAT_GET0_ALPN_SELECTED \
56+
SSL_COMPAT_NAME3(SSL, get0, alpn_selected)
57+
#define SSL_COMPAT_SET_SNI_CALLBACK SSL_COMPAT_NAME4(SSL, set, sni, callback)
58+
#define SSL_COMPAT_FROM_MBEDTLS_SSL_CONTEXT \
59+
SSL_COMPAT_NAME5(SSL, SSL, from, mbedtls_ssl, context)
60+
#define SSL_COMPAT_SET_SSL_CTX SSL_COMPAT_NAME4(SSL, set, SSL, CTX)
4761

4862
/****************************************************************************
4963
* Private Types
@@ -268,7 +282,8 @@ static int ssl_pm_reload_crt(SSL *ssl)
268282
int ret = 0;
269283
int mode;
270284
struct ssl_pm *ssl_pm = ssl->ssl_pm;
271-
struct x509_pm *ca_pm = (struct x509_pm *)ssl->client_CA->x509_pm;
285+
struct x509_pm *ca_pm =
286+
(struct x509_pm *)ssl->SSL_COMPAT_CLIENT_CA->x509_pm;
272287

273288
struct pkey_pm *pkey_pm = (struct pkey_pm *)ssl->cert->pkey->pkey_pm;
274289
struct x509_pm *crt_pm = (struct x509_pm *)ssl->cert->x509->x509_pm;
@@ -534,7 +549,7 @@ int ssl_pm_send(SSL *ssl, const void *buffer, int len)
534549
* If this function returns something other than a positive value or
535550
* MBEDTLS_ERR_SSL_WANT_READ/WRITE, the ssl context becomes unusable, and
536551
* you should either free it or call mbedtls_ssl_session_reset() on it
537-
* before re-using it for a new connection; the current connection must
552+
* before reusing it for a new connection; the current connection must
538553
* be closed.
539554
*
540555
* When this function returns MBEDTLS_ERR_SSL_WANT_WRITE/READ, it must be
@@ -677,10 +692,10 @@ int x509_pm_show_info(X509 *x)
677692

678693
buf[ret] = 0;
679694

680-
ssl_mem_free(buf);
681-
682695
SSL_DEBUG(SSL_DEBUG_ON, "%s", buf);
683696

697+
ssl_mem_free(buf);
698+
684699
return 0;
685700

686701
mbedtls_err1:
@@ -965,8 +980,8 @@ long ssl_pm_get_verify_result(const SSL *ssl)
965980
* @brief set expected hostname on peer cert CN
966981
*/
967982

968-
int X509_VERIFY_PARAM_set1_host(X509_VERIFY_PARAM *param,
969-
const char *name, size_t namelen)
983+
int SSL_COMPAT_X509_VERIFY_PARAM_SET1_HOST(X509_VERIFY_PARAM *param,
984+
const char *name, size_t namelen)
970985
{
971986
SSL *ssl = (SSL *)((char *)param - offsetof(SSL, param));
972987
struct ssl_pm *ssl_pm = (struct ssl_pm *)ssl->ssl_pm;
@@ -1024,8 +1039,9 @@ void _ssl_set_alpn_list(const SSL *ssl)
10241039
}
10251040
}
10261041

1027-
void SSL_get0_alpn_selected(const SSL *ssl, const unsigned char **data,
1028-
unsigned int *len)
1042+
void SSL_COMPAT_GET0_ALPN_SELECTED(const SSL *ssl,
1043+
const unsigned char **data,
1044+
unsigned int *len)
10291045
{
10301046
const char *alp = mbedtls_ssl_get_alpn_protocol(
10311047
&((struct ssl_pm *)(ssl->ssl_pm))->ssl);
@@ -1041,9 +1057,9 @@ void SSL_get0_alpn_selected(const SSL *ssl, const unsigned char **data,
10411057
}
10421058
}
10431059

1044-
int SSL_set_sni_callback(SSL *ssl,
1045-
int(*cb)(void *, mbedtls_ssl_context *,
1046-
const unsigned char *, size_t), void *param)
1060+
int SSL_COMPAT_SET_SNI_CALLBACK(SSL *ssl,
1061+
int (*cb)(void *, mbedtls_ssl_context *,
1062+
const unsigned char *, size_t), void *param)
10471063
{
10481064
struct ssl_pm *ssl_pm = (struct ssl_pm *)ssl->ssl_pm;
10491065

@@ -1052,19 +1068,20 @@ int SSL_set_sni_callback(SSL *ssl,
10521068
return 0;
10531069
}
10541070

1055-
SSL *SSL_SSL_from_mbedtls_ssl_context(mbedtls_ssl_context *msc)
1071+
SSL *SSL_COMPAT_FROM_MBEDTLS_SSL_CONTEXT(mbedtls_ssl_context *msc)
10561072
{
10571073
struct ssl_pm *ssl_pm =
10581074
(struct ssl_pm *)((char *)msc - offsetof(struct ssl_pm, ssl));
10591075

10601076
return ssl_pm->owner;
10611077
}
10621078

1063-
void SSL_set_SSL_CTX(SSL *ssl, SSL_CTX *ctx)
1079+
void SSL_COMPAT_SET_SSL_CTX(SSL *ssl, SSL_CTX *ctx)
10641080
{
10651081
struct ssl_pm *ssl_pm = ssl->ssl_pm;
10661082
struct x509_pm *x509_pm = (struct x509_pm *)ctx->cert->x509->x509_pm;
1067-
struct x509_pm *x509_pm_ca = (struct x509_pm *)ctx->client_CA->x509_pm;
1083+
struct x509_pm *x509_pm_ca =
1084+
(struct x509_pm *)ctx->SSL_COMPAT_CLIENT_CA->x509_pm;
10681085

10691086
struct pkey_pm *pkey_pm = (struct pkey_pm *)ctx->cert->pkey->pkey_pm;
10701087
int mode;

0 commit comments

Comments
 (0)