2012-09-25 21:55:46 +00:00
|
|
|
/*
|
|
|
|
* SSL session cache implementation
|
|
|
|
*
|
2020-08-19 14:54:51 +00:00
|
|
|
* Copyright The Mbed TLS Contributors
|
Update license headers to Apache-2.0 OR GPL-2.0-or-later
This will allow us to ship the LTS branches in a single archive
This commit was generated using the following script:
# ========================
#!/bin/sh
header1='\ * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later\
*\
* This file is provided under the Apache License 2.0, or the\
* GNU General Public License v2.0 or later.\
*\
* **********\
* Apache License 2.0:\
*\
* Licensed under the Apache License, Version 2.0 (the "License"); you may\
* not use this file except in compliance with the License.\
* You may obtain a copy of the License at\
*\
* http://www.apache.org/licenses/LICENSE-2.0\
*\
* Unless required by applicable law or agreed to in writing, software\
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT\
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\
* See the License for the specific language governing permissions and\
* limitations under the License.\
*\
* **********\
*\
* **********\
* GNU General Public License v2.0 or later:\
*\
* This program is free software; you can redistribute it and/or modify\
* it under the terms of the GNU General Public License as published by\
* the Free Software Foundation; either version 2 of the License, or\
* (at your option) any later version.\
*\
* This program is distributed in the hope that it will be useful,\
* but WITHOUT ANY WARRANTY; without even the implied warranty of\
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\
* GNU General Public License for more details.\
*\
* You should have received a copy of the GNU General Public License along\
* with this program; if not, write to the Free Software Foundation, Inc.,\
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.\
*\
* **********'
find -path './.git' -prune -o '(' -name '*.c' -o -name '*.cpp' -o -name '*.fmt' -o -name '*.h' ')' -print | xargs sed -i "
# Normalize the first line of the copyright headers (no text on the first line of a block comment)
/^\/\*.*Copyright.*Arm/I s/\/\*/&\n */
# Insert new copyright header
/SPDX-License-Identifier/ i\
$header1
# Delete old copyright header
/SPDX-License-Identifier/,$ {
# Delete lines until the one preceding the mbedtls declaration
N
1,/This file is part of/ {
/This file is part of/! D
}
}
"
# Format copyright header for inclusion into scripts
header2=$(echo "$header1" | sed 's/^\\\? \* \?/#/')
find -path './.git' -prune -o '(' -name '*.gdb' -o -name '*.pl' -o -name '*.py' -o -name '*.sh' ')' -print | xargs sed -i "
# Insert new copyright header
/SPDX-License-Identifier/ i\
$header2
# Delete old copyright header
/SPDX-License-Identifier/,$ {
# Delete lines until the one preceding the mbedtls declaration
N
1,/This file is part of/ {
/This file is part of/! D
}
}
"
# ========================
Signed-off-by: Bence Szépkúti <bence.szepkuti@arm.com>
2020-06-05 11:02:18 +00:00
|
|
|
* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
|
|
|
|
*
|
|
|
|
* This file is provided under the Apache License 2.0, or the
|
|
|
|
* GNU General Public License v2.0 or later.
|
|
|
|
*
|
|
|
|
* **********
|
|
|
|
* Apache License 2.0:
|
2012-09-25 21:55:46 +00:00
|
|
|
*
|
2015-09-04 12:21:07 +00:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
|
|
* not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2012-09-25 21:55:46 +00:00
|
|
|
*
|
2015-09-04 12:21:07 +00:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2012-09-25 21:55:46 +00:00
|
|
|
*
|
2015-09-04 12:21:07 +00:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
2012-09-25 21:55:46 +00:00
|
|
|
*
|
Update license headers to Apache-2.0 OR GPL-2.0-or-later
This will allow us to ship the LTS branches in a single archive
This commit was generated using the following script:
# ========================
#!/bin/sh
header1='\ * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later\
*\
* This file is provided under the Apache License 2.0, or the\
* GNU General Public License v2.0 or later.\
*\
* **********\
* Apache License 2.0:\
*\
* Licensed under the Apache License, Version 2.0 (the "License"); you may\
* not use this file except in compliance with the License.\
* You may obtain a copy of the License at\
*\
* http://www.apache.org/licenses/LICENSE-2.0\
*\
* Unless required by applicable law or agreed to in writing, software\
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT\
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\
* See the License for the specific language governing permissions and\
* limitations under the License.\
*\
* **********\
*\
* **********\
* GNU General Public License v2.0 or later:\
*\
* This program is free software; you can redistribute it and/or modify\
* it under the terms of the GNU General Public License as published by\
* the Free Software Foundation; either version 2 of the License, or\
* (at your option) any later version.\
*\
* This program is distributed in the hope that it will be useful,\
* but WITHOUT ANY WARRANTY; without even the implied warranty of\
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\
* GNU General Public License for more details.\
*\
* You should have received a copy of the GNU General Public License along\
* with this program; if not, write to the Free Software Foundation, Inc.,\
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.\
*\
* **********'
find -path './.git' -prune -o '(' -name '*.c' -o -name '*.cpp' -o -name '*.fmt' -o -name '*.h' ')' -print | xargs sed -i "
# Normalize the first line of the copyright headers (no text on the first line of a block comment)
/^\/\*.*Copyright.*Arm/I s/\/\*/&\n */
# Insert new copyright header
/SPDX-License-Identifier/ i\
$header1
# Delete old copyright header
/SPDX-License-Identifier/,$ {
# Delete lines until the one preceding the mbedtls declaration
N
1,/This file is part of/ {
/This file is part of/! D
}
}
"
# Format copyright header for inclusion into scripts
header2=$(echo "$header1" | sed 's/^\\\? \* \?/#/')
find -path './.git' -prune -o '(' -name '*.gdb' -o -name '*.pl' -o -name '*.py' -o -name '*.sh' ')' -print | xargs sed -i "
# Insert new copyright header
/SPDX-License-Identifier/ i\
$header2
# Delete old copyright header
/SPDX-License-Identifier/,$ {
# Delete lines until the one preceding the mbedtls declaration
N
1,/This file is part of/ {
/This file is part of/! D
}
}
"
# ========================
Signed-off-by: Bence Szépkúti <bence.szepkuti@arm.com>
2020-06-05 11:02:18 +00:00
|
|
|
* **********
|
|
|
|
*
|
|
|
|
* **********
|
|
|
|
* GNU General Public License v2.0 or later:
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*
|
|
|
|
* **********
|
2012-09-25 21:55:46 +00:00
|
|
|
*/
|
|
|
|
/*
|
|
|
|
* These session callbacks use a simple chained list
|
|
|
|
* to store and retrieve the session information.
|
|
|
|
*/
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if !defined(MBEDTLS_CONFIG_FILE)
|
2015-03-09 17:05:11 +00:00
|
|
|
#include "mbedtls/config.h"
|
2014-04-29 10:39:06 +00:00
|
|
|
#else
|
2015-04-08 10:49:31 +00:00
|
|
|
#include MBEDTLS_CONFIG_FILE
|
2014-04-29 10:39:06 +00:00
|
|
|
#endif
|
2012-09-25 21:55:46 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_SSL_CACHE_C)
|
2012-09-25 21:55:46 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_PLATFORM_C)
|
2015-03-09 17:05:11 +00:00
|
|
|
#include "mbedtls/platform.h"
|
2013-07-03 11:37:05 +00:00
|
|
|
#else
|
2015-02-06 13:43:58 +00:00
|
|
|
#include <stdlib.h>
|
2015-05-26 14:04:06 +00:00
|
|
|
#define mbedtls_calloc calloc
|
2016-04-26 06:43:27 +00:00
|
|
|
#define mbedtls_free free
|
2013-07-03 11:37:05 +00:00
|
|
|
#endif
|
|
|
|
|
2016-04-26 06:43:27 +00:00
|
|
|
#include "mbedtls/ssl_cache.h"
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
void mbedtls_ssl_cache_init( mbedtls_ssl_cache_context *cache )
|
2012-09-25 21:55:46 +00:00
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
memset( cache, 0, sizeof( mbedtls_ssl_cache_context ) );
|
2012-09-25 21:55:46 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
cache->timeout = MBEDTLS_SSL_CACHE_DEFAULT_TIMEOUT;
|
|
|
|
cache->max_entries = MBEDTLS_SSL_CACHE_DEFAULT_MAX_ENTRIES;
|
2013-09-28 13:01:27 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_THREADING_C)
|
|
|
|
mbedtls_mutex_init( &cache->mutex );
|
2013-09-28 13:01:27 +00:00
|
|
|
#endif
|
2012-09-25 21:55:46 +00:00
|
|
|
}
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
int mbedtls_ssl_cache_get( void *data, mbedtls_ssl_session *session )
|
2012-09-25 21:55:46 +00:00
|
|
|
{
|
2013-09-28 13:01:27 +00:00
|
|
|
int ret = 1;
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_HAVE_TIME)
|
2016-04-26 06:43:27 +00:00
|
|
|
mbedtls_time_t t = mbedtls_time( NULL );
|
2013-07-03 13:31:03 +00:00
|
|
|
#endif
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_ssl_cache_context *cache = (mbedtls_ssl_cache_context *) data;
|
|
|
|
mbedtls_ssl_cache_entry *cur, *entry;
|
2012-09-25 21:55:46 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_THREADING_C)
|
|
|
|
if( mbedtls_mutex_lock( &cache->mutex ) != 0 )
|
2013-09-28 13:01:27 +00:00
|
|
|
return( 1 );
|
|
|
|
#endif
|
|
|
|
|
2012-09-25 21:55:46 +00:00
|
|
|
cur = cache->chain;
|
|
|
|
entry = NULL;
|
|
|
|
|
|
|
|
while( cur != NULL )
|
|
|
|
{
|
|
|
|
entry = cur;
|
|
|
|
cur = cur->next;
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_HAVE_TIME)
|
2012-09-25 21:55:46 +00:00
|
|
|
if( cache->timeout != 0 &&
|
|
|
|
(int) ( t - entry->timestamp ) > cache->timeout )
|
|
|
|
continue;
|
2013-07-03 13:31:03 +00:00
|
|
|
#endif
|
2012-09-25 21:55:46 +00:00
|
|
|
|
|
|
|
if( session->ciphersuite != entry->session.ciphersuite ||
|
|
|
|
session->compression != entry->session.compression ||
|
2015-06-18 13:50:37 +00:00
|
|
|
session->id_len != entry->session.id_len )
|
2012-09-25 21:55:46 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
if( memcmp( session->id, entry->session.id,
|
2015-06-18 13:50:37 +00:00
|
|
|
entry->session.id_len ) != 0 )
|
2012-09-25 21:55:46 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
memcpy( session->master, entry->session.master, 48 );
|
2013-03-06 16:40:46 +00:00
|
|
|
|
2013-08-23 08:44:29 +00:00
|
|
|
session->verify_result = entry->session.verify_result;
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_X509_CRT_PARSE_C)
|
2013-03-06 16:40:46 +00:00
|
|
|
/*
|
|
|
|
* Restore peer certificate (without rest of the original chain)
|
|
|
|
*/
|
|
|
|
if( entry->peer_cert.p != NULL )
|
|
|
|
{
|
2015-05-26 14:04:06 +00:00
|
|
|
if( ( session->peer_cert = mbedtls_calloc( 1,
|
2015-04-08 10:49:31 +00:00
|
|
|
sizeof(mbedtls_x509_crt) ) ) == NULL )
|
2013-09-28 13:01:27 +00:00
|
|
|
{
|
|
|
|
ret = 1;
|
|
|
|
goto exit;
|
|
|
|
}
|
2013-03-06 16:40:46 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_x509_crt_init( session->peer_cert );
|
|
|
|
if( mbedtls_x509_crt_parse( session->peer_cert, entry->peer_cert.p,
|
2013-09-18 11:46:23 +00:00
|
|
|
entry->peer_cert.len ) != 0 )
|
2013-03-06 16:40:46 +00:00
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_free( session->peer_cert );
|
2013-03-06 16:40:46 +00:00
|
|
|
session->peer_cert = NULL;
|
2013-09-28 13:01:27 +00:00
|
|
|
ret = 1;
|
|
|
|
goto exit;
|
2013-03-06 16:40:46 +00:00
|
|
|
}
|
|
|
|
}
|
2015-04-08 10:49:31 +00:00
|
|
|
#endif /* MBEDTLS_X509_CRT_PARSE_C */
|
2013-03-06 16:40:46 +00:00
|
|
|
|
2013-09-28 13:01:27 +00:00
|
|
|
ret = 0;
|
|
|
|
goto exit;
|
2012-09-25 21:55:46 +00:00
|
|
|
}
|
|
|
|
|
2013-09-28 13:01:27 +00:00
|
|
|
exit:
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_THREADING_C)
|
|
|
|
if( mbedtls_mutex_unlock( &cache->mutex ) != 0 )
|
2013-09-28 13:01:27 +00:00
|
|
|
ret = 1;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return( ret );
|
2012-09-25 21:55:46 +00:00
|
|
|
}
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
int mbedtls_ssl_cache_set( void *data, const mbedtls_ssl_session *session )
|
2012-09-25 21:55:46 +00:00
|
|
|
{
|
2013-09-28 13:01:27 +00:00
|
|
|
int ret = 1;
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_HAVE_TIME)
|
2017-07-28 22:46:43 +00:00
|
|
|
mbedtls_time_t t = mbedtls_time( NULL ), oldest = 0;
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_ssl_cache_entry *old = NULL;
|
2013-07-03 13:31:03 +00:00
|
|
|
#endif
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_ssl_cache_context *cache = (mbedtls_ssl_cache_context *) data;
|
|
|
|
mbedtls_ssl_cache_entry *cur, *prv;
|
2012-10-23 22:18:28 +00:00
|
|
|
int count = 0;
|
2012-09-25 21:55:46 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_THREADING_C)
|
|
|
|
if( ( ret = mbedtls_mutex_lock( &cache->mutex ) ) != 0 )
|
2013-09-28 13:01:27 +00:00
|
|
|
return( ret );
|
|
|
|
#endif
|
|
|
|
|
2012-09-25 21:55:46 +00:00
|
|
|
cur = cache->chain;
|
|
|
|
prv = NULL;
|
|
|
|
|
|
|
|
while( cur != NULL )
|
|
|
|
{
|
2012-10-23 22:18:28 +00:00
|
|
|
count++;
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_HAVE_TIME)
|
2012-09-25 21:55:46 +00:00
|
|
|
if( cache->timeout != 0 &&
|
|
|
|
(int) ( t - cur->timestamp ) > cache->timeout )
|
|
|
|
{
|
|
|
|
cur->timestamp = t;
|
|
|
|
break; /* expired, reuse this slot, update timestamp */
|
|
|
|
}
|
2013-07-03 13:31:03 +00:00
|
|
|
#endif
|
2012-09-25 21:55:46 +00:00
|
|
|
|
2015-06-18 13:50:37 +00:00
|
|
|
if( memcmp( session->id, cur->session.id, cur->session.id_len ) == 0 )
|
2012-09-25 21:55:46 +00:00
|
|
|
break; /* client reconnected, keep timestamp for session id */
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_HAVE_TIME)
|
2012-10-23 22:18:28 +00:00
|
|
|
if( oldest == 0 || cur->timestamp < oldest )
|
|
|
|
{
|
|
|
|
oldest = cur->timestamp;
|
|
|
|
old = cur;
|
|
|
|
}
|
2013-07-03 13:31:03 +00:00
|
|
|
#endif
|
2012-10-23 22:18:28 +00:00
|
|
|
|
2012-09-25 21:55:46 +00:00
|
|
|
prv = cur;
|
|
|
|
cur = cur->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( cur == NULL )
|
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_HAVE_TIME)
|
2012-10-23 22:18:28 +00:00
|
|
|
/*
|
|
|
|
* Reuse oldest entry if max_entries reached
|
|
|
|
*/
|
2014-02-20 20:32:08 +00:00
|
|
|
if( count >= cache->max_entries )
|
2012-10-23 22:18:28 +00:00
|
|
|
{
|
2014-02-20 20:32:08 +00:00
|
|
|
if( old == NULL )
|
|
|
|
{
|
|
|
|
ret = 1;
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2012-10-23 22:18:28 +00:00
|
|
|
cur = old;
|
|
|
|
}
|
2015-04-08 10:49:31 +00:00
|
|
|
#else /* MBEDTLS_HAVE_TIME */
|
2013-07-03 13:31:03 +00:00
|
|
|
/*
|
|
|
|
* Reuse first entry in chain if max_entries reached,
|
|
|
|
* but move to last place
|
|
|
|
*/
|
|
|
|
if( count >= cache->max_entries )
|
|
|
|
{
|
|
|
|
if( cache->chain == NULL )
|
2013-09-28 13:01:27 +00:00
|
|
|
{
|
|
|
|
ret = 1;
|
|
|
|
goto exit;
|
|
|
|
}
|
2013-07-03 13:31:03 +00:00
|
|
|
|
|
|
|
cur = cache->chain;
|
|
|
|
cache->chain = cur->next;
|
2014-02-26 16:38:55 +00:00
|
|
|
cur->next = NULL;
|
2013-07-03 13:31:03 +00:00
|
|
|
prv->next = cur;
|
|
|
|
}
|
2015-04-08 10:49:31 +00:00
|
|
|
#endif /* MBEDTLS_HAVE_TIME */
|
2012-10-23 22:18:28 +00:00
|
|
|
else
|
|
|
|
{
|
2014-02-20 20:32:08 +00:00
|
|
|
/*
|
|
|
|
* max_entries not reached, create new entry
|
|
|
|
*/
|
2015-05-26 14:04:06 +00:00
|
|
|
cur = mbedtls_calloc( 1, sizeof(mbedtls_ssl_cache_entry) );
|
2012-10-23 22:18:28 +00:00
|
|
|
if( cur == NULL )
|
2013-09-28 13:01:27 +00:00
|
|
|
{
|
|
|
|
ret = 1;
|
|
|
|
goto exit;
|
|
|
|
}
|
2012-09-25 21:55:46 +00:00
|
|
|
|
2012-10-23 22:18:28 +00:00
|
|
|
if( prv == NULL )
|
|
|
|
cache->chain = cur;
|
|
|
|
else
|
|
|
|
prv->next = cur;
|
|
|
|
}
|
2012-09-25 21:55:46 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_HAVE_TIME)
|
2012-09-25 21:55:46 +00:00
|
|
|
cur->timestamp = t;
|
2013-07-03 13:31:03 +00:00
|
|
|
#endif
|
2012-09-25 21:55:46 +00:00
|
|
|
}
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
memcpy( &cur->session, session, sizeof( mbedtls_ssl_session ) );
|
2013-04-18 20:46:23 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_X509_CRT_PARSE_C)
|
2014-02-26 16:38:55 +00:00
|
|
|
/*
|
|
|
|
* If we're reusing an entry, free its certificate first
|
|
|
|
*/
|
|
|
|
if( cur->peer_cert.p != NULL )
|
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_free( cur->peer_cert.p );
|
|
|
|
memset( &cur->peer_cert, 0, sizeof(mbedtls_x509_buf) );
|
2014-02-26 16:38:55 +00:00
|
|
|
}
|
|
|
|
|
2013-03-06 16:40:46 +00:00
|
|
|
/*
|
|
|
|
* Store peer certificate
|
|
|
|
*/
|
|
|
|
if( session->peer_cert != NULL )
|
|
|
|
{
|
2015-05-26 14:04:06 +00:00
|
|
|
cur->peer_cert.p = mbedtls_calloc( 1, session->peer_cert->raw.len );
|
2013-03-06 16:40:46 +00:00
|
|
|
if( cur->peer_cert.p == NULL )
|
2013-09-28 13:01:27 +00:00
|
|
|
{
|
|
|
|
ret = 1;
|
|
|
|
goto exit;
|
|
|
|
}
|
2013-03-06 16:40:46 +00:00
|
|
|
|
|
|
|
memcpy( cur->peer_cert.p, session->peer_cert->raw.p,
|
|
|
|
session->peer_cert->raw.len );
|
|
|
|
cur->peer_cert.len = session->peer_cert->raw.len;
|
|
|
|
|
|
|
|
cur->session.peer_cert = NULL;
|
|
|
|
}
|
2015-04-08 10:49:31 +00:00
|
|
|
#endif /* MBEDTLS_X509_CRT_PARSE_C */
|
2012-09-25 21:55:46 +00:00
|
|
|
|
2013-09-28 13:01:27 +00:00
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
exit:
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_THREADING_C)
|
|
|
|
if( mbedtls_mutex_unlock( &cache->mutex ) != 0 )
|
2013-09-28 13:01:27 +00:00
|
|
|
ret = 1;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return( ret );
|
2012-09-25 21:55:46 +00:00
|
|
|
}
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_HAVE_TIME)
|
|
|
|
void mbedtls_ssl_cache_set_timeout( mbedtls_ssl_cache_context *cache, int timeout )
|
2012-09-25 21:55:46 +00:00
|
|
|
{
|
|
|
|
if( timeout < 0 ) timeout = 0;
|
|
|
|
|
|
|
|
cache->timeout = timeout;
|
|
|
|
}
|
2015-04-08 10:49:31 +00:00
|
|
|
#endif /* MBEDTLS_HAVE_TIME */
|
2012-09-25 21:55:46 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
void mbedtls_ssl_cache_set_max_entries( mbedtls_ssl_cache_context *cache, int max )
|
2012-10-23 22:18:28 +00:00
|
|
|
{
|
|
|
|
if( max < 0 ) max = 0;
|
|
|
|
|
|
|
|
cache->max_entries = max;
|
|
|
|
}
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
void mbedtls_ssl_cache_free( mbedtls_ssl_cache_context *cache )
|
2012-09-25 21:55:46 +00:00
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_ssl_cache_entry *cur, *prv;
|
2012-09-25 21:55:46 +00:00
|
|
|
|
|
|
|
cur = cache->chain;
|
|
|
|
|
|
|
|
while( cur != NULL )
|
|
|
|
{
|
|
|
|
prv = cur;
|
|
|
|
cur = cur->next;
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_ssl_session_free( &prv->session );
|
2013-03-06 16:40:46 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_X509_CRT_PARSE_C)
|
|
|
|
mbedtls_free( prv->peer_cert.p );
|
|
|
|
#endif /* MBEDTLS_X509_CRT_PARSE_C */
|
2013-03-06 16:40:46 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_free( prv );
|
2012-09-25 21:55:46 +00:00
|
|
|
}
|
2013-09-28 13:01:27 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_THREADING_C)
|
|
|
|
mbedtls_mutex_free( &cache->mutex );
|
2013-09-28 13:01:27 +00:00
|
|
|
#endif
|
2017-10-29 15:53:52 +00:00
|
|
|
cache->chain = NULL;
|
2012-09-25 21:55:46 +00:00
|
|
|
}
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#endif /* MBEDTLS_SSL_CACHE_C */
|