mirror of
https://github.com/Mbed-TLS/mbedtls.git
synced 2025-08-07 06:42:56 +03:00
Also compiles / runs without time-based functions in OS
Can now run without need of time() / localtime() and gettimeofday()
This commit is contained in:
@@ -42,7 +42,6 @@
|
||||
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
/*
|
||||
* ASN.1 DER decoding routines
|
||||
|
@@ -38,7 +38,6 @@
|
||||
#include "polarssl/timing.h"
|
||||
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
|
||||
/* ------------------------------------------------------------------------
|
||||
* On average, one iteration accesses two 8-word blocks in the havege WALK
|
||||
|
@@ -1,7 +1,7 @@
|
||||
/*
|
||||
* TCP networking functions
|
||||
*
|
||||
* Copyright (C) 2006-2010, Brainspark B.V.
|
||||
* Copyright (C) 2006-2013, Brainspark B.V.
|
||||
*
|
||||
* This file is part of PolarSSL (http://www.polarssl.org)
|
||||
* Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
|
||||
@@ -52,7 +52,9 @@ static int wsa_init_done = 0;
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
#if defined(POLARSSL_HAVE_TIME)
|
||||
#include <sys/time.h>
|
||||
#endif
|
||||
#include <unistd.h>
|
||||
#include <signal.h>
|
||||
#include <fcntl.h>
|
||||
@@ -74,7 +76,10 @@ static int wsa_init_done = 0;
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#if defined(POLARSSL_HAVE_TIME)
|
||||
#include <time.h>
|
||||
#endif
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#include <basetsd.h>
|
||||
@@ -293,6 +298,7 @@ int net_set_nonblock( int fd )
|
||||
#endif
|
||||
}
|
||||
|
||||
#if defined(POLARSSL_HAVE_TIME)
|
||||
/*
|
||||
* Portable usleep helper
|
||||
*/
|
||||
@@ -303,6 +309,7 @@ void net_usleep( unsigned long usec )
|
||||
tv.tv_usec = usec;
|
||||
select( 0, NULL, NULL, NULL, &tv );
|
||||
}
|
||||
#endif /* POLARSSL_HAVE_TIME */
|
||||
|
||||
/*
|
||||
* Read at most 'len' characters
|
||||
|
@@ -52,7 +52,9 @@ void ssl_cache_init( ssl_cache_context *cache )
|
||||
|
||||
int ssl_cache_get( void *data, ssl_session *session )
|
||||
{
|
||||
#if defined(POLARSSL_HAVE_TIME)
|
||||
time_t t = time( NULL );
|
||||
#endif
|
||||
ssl_cache_context *cache = (ssl_cache_context *) data;
|
||||
ssl_cache_entry *cur, *entry;
|
||||
|
||||
@@ -64,9 +66,11 @@ int ssl_cache_get( void *data, ssl_session *session )
|
||||
entry = cur;
|
||||
cur = cur->next;
|
||||
|
||||
#if defined(POLARSSL_HAVE_TIME)
|
||||
if( cache->timeout != 0 &&
|
||||
(int) ( t - entry->timestamp ) > cache->timeout )
|
||||
continue;
|
||||
#endif
|
||||
|
||||
if( session->ciphersuite != entry->session.ciphersuite ||
|
||||
session->compression != entry->session.compression ||
|
||||
@@ -108,9 +112,12 @@ int ssl_cache_get( void *data, ssl_session *session )
|
||||
|
||||
int ssl_cache_set( void *data, const ssl_session *session )
|
||||
{
|
||||
#if defined(POLARSSL_HAVE_TIME)
|
||||
time_t t = time( NULL ), oldest = 0;
|
||||
ssl_cache_entry *old = NULL;
|
||||
#endif
|
||||
ssl_cache_context *cache = (ssl_cache_context *) data;
|
||||
ssl_cache_entry *cur, *prv, *old = NULL;
|
||||
ssl_cache_entry *cur, *prv;
|
||||
int count = 0;
|
||||
|
||||
cur = cache->chain;
|
||||
@@ -120,21 +127,25 @@ int ssl_cache_set( void *data, const ssl_session *session )
|
||||
{
|
||||
count++;
|
||||
|
||||
#if defined(POLARSSL_HAVE_TIME)
|
||||
if( cache->timeout != 0 &&
|
||||
(int) ( t - cur->timestamp ) > cache->timeout )
|
||||
{
|
||||
cur->timestamp = t;
|
||||
break; /* expired, reuse this slot, update timestamp */
|
||||
}
|
||||
#endif
|
||||
|
||||
if( memcmp( session->id, cur->session.id, cur->session.length ) == 0 )
|
||||
break; /* client reconnected, keep timestamp for session id */
|
||||
|
||||
#if defined(POLARSSL_HAVE_TIME)
|
||||
if( oldest == 0 || cur->timestamp < oldest )
|
||||
{
|
||||
oldest = cur->timestamp;
|
||||
old = cur;
|
||||
}
|
||||
#endif
|
||||
|
||||
prv = cur;
|
||||
cur = cur->next;
|
||||
@@ -142,6 +153,7 @@ int ssl_cache_set( void *data, const ssl_session *session )
|
||||
|
||||
if( cur == NULL )
|
||||
{
|
||||
#if defined(POLARSSL_HAVE_TIME)
|
||||
/*
|
||||
* Reuse oldest entry if max_entries reached
|
||||
*/
|
||||
@@ -157,6 +169,31 @@ int ssl_cache_set( void *data, const ssl_session *session )
|
||||
}
|
||||
#endif /* POLARSSL_X509_PARSE_C */
|
||||
}
|
||||
#else /* POLARSSL_HAVE_TIME */
|
||||
/*
|
||||
* Reuse first entry in chain if max_entries reached,
|
||||
* but move to last place
|
||||
*/
|
||||
if( count >= cache->max_entries )
|
||||
{
|
||||
if( cache->chain == NULL )
|
||||
return( 1 );
|
||||
|
||||
cur = cache->chain;
|
||||
cache->chain = cur->next;
|
||||
|
||||
#if defined(POLARSSL_X509_PARSE_C)
|
||||
if( cur->peer_cert.p != NULL )
|
||||
{
|
||||
polarssl_free( cur->peer_cert.p );
|
||||
memset( &cur->peer_cert, 0, sizeof(x509_buf) );
|
||||
}
|
||||
#endif /* POLARSSL_X509_PARSE_C */
|
||||
|
||||
memset( cur, 0, sizeof(ssl_cache_entry) );
|
||||
prv->next = cur;
|
||||
}
|
||||
#endif /* POLARSSL_HAVE_TIME */
|
||||
else
|
||||
{
|
||||
cur = (ssl_cache_entry *) polarssl_malloc( sizeof(ssl_cache_entry) );
|
||||
@@ -171,7 +208,9 @@ int ssl_cache_set( void *data, const ssl_session *session )
|
||||
prv->next = cur;
|
||||
}
|
||||
|
||||
#if defined(POLARSSL_HAVE_TIME)
|
||||
cur->timestamp = t;
|
||||
#endif
|
||||
}
|
||||
|
||||
memcpy( &cur->session, session, sizeof( ssl_session ) );
|
||||
@@ -197,12 +236,14 @@ int ssl_cache_set( void *data, const ssl_session *session )
|
||||
return( 0 );
|
||||
}
|
||||
|
||||
#if defined(POLARSSL_HAVE_TIME)
|
||||
void ssl_cache_set_timeout( ssl_cache_context *cache, int timeout )
|
||||
{
|
||||
if( timeout < 0 ) timeout = 0;
|
||||
|
||||
cache->timeout = timeout;
|
||||
}
|
||||
#endif /* POLARSSL_HAVE_TIME */
|
||||
|
||||
void ssl_cache_set_max_entries( ssl_cache_context *cache, int max )
|
||||
{
|
||||
|
@@ -32,7 +32,17 @@
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#include <basetsd.h>
|
||||
typedef UINT32 uint32_t;
|
||||
#else
|
||||
#include <inttypes.h>
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_HAVE_TIME)
|
||||
#include <time.h>
|
||||
#endif
|
||||
|
||||
static void ssl_write_hostname_ext( ssl_context *ssl,
|
||||
unsigned char *buf,
|
||||
@@ -265,7 +275,9 @@ static int ssl_write_client_hello( ssl_context *ssl )
|
||||
size_t i, n, olen, ext_len = 0;
|
||||
unsigned char *buf;
|
||||
unsigned char *p, *q;
|
||||
#if defined(POLARSSL_HAVE_TIME)
|
||||
time_t t;
|
||||
#endif
|
||||
const int *ciphersuites;
|
||||
const ssl_ciphersuite_t *ciphersuite_info;
|
||||
|
||||
@@ -299,6 +311,7 @@ static int ssl_write_client_hello( ssl_context *ssl )
|
||||
SSL_DEBUG_MSG( 3, ( "client hello, max version: [%d:%d]",
|
||||
buf[4], buf[5] ) );
|
||||
|
||||
#if defined(POLARSSL_HAVE_TIME)
|
||||
t = time( NULL );
|
||||
*p++ = (unsigned char)( t >> 24 );
|
||||
*p++ = (unsigned char)( t >> 16 );
|
||||
@@ -306,6 +319,12 @@ static int ssl_write_client_hello( ssl_context *ssl )
|
||||
*p++ = (unsigned char)( t );
|
||||
|
||||
SSL_DEBUG_MSG( 3, ( "client hello, current time: %lu", t ) );
|
||||
#else
|
||||
if( ( ret = ssl->f_rng( ssl->p_rng, p, 4 ) ) != 0 )
|
||||
return( ret );
|
||||
|
||||
p += 4;
|
||||
#endif
|
||||
|
||||
if( ( ret = ssl->f_rng( ssl->p_rng, p, 28 ) ) != 0 )
|
||||
return( ret );
|
||||
@@ -483,9 +502,7 @@ static int ssl_parse_renegotiation_info( ssl_context *ssl,
|
||||
|
||||
static int ssl_parse_server_hello( ssl_context *ssl )
|
||||
{
|
||||
#if defined(POLARSSL_DEBUG_C)
|
||||
time_t t;
|
||||
#endif
|
||||
uint32_t t;
|
||||
int ret, i, comp;
|
||||
size_t n;
|
||||
size_t ext_len = 0;
|
||||
@@ -548,10 +565,10 @@ static int ssl_parse_server_hello( ssl_context *ssl )
|
||||
}
|
||||
|
||||
#if defined(POLARSSL_DEBUG_C)
|
||||
t = ( (time_t) buf[6] << 24 )
|
||||
| ( (time_t) buf[7] << 16 )
|
||||
| ( (time_t) buf[8] << 8 )
|
||||
| ( (time_t) buf[9] );
|
||||
t = ( (uint32_t) buf[6] << 24 )
|
||||
| ( (uint32_t) buf[7] << 16 )
|
||||
| ( (uint32_t) buf[8] << 8 )
|
||||
| ( (uint32_t) buf[9] );
|
||||
#endif
|
||||
|
||||
memcpy( ssl->handshake->randbytes + 32, buf + 6, 32 );
|
||||
@@ -619,7 +636,9 @@ static int ssl_parse_server_hello( ssl_context *ssl )
|
||||
{
|
||||
ssl->state++;
|
||||
ssl->handshake->resume = 0;
|
||||
#if defined(POLARSSL_HAVE_TIME)
|
||||
ssl->session_negotiate->start = time( NULL );
|
||||
#endif
|
||||
ssl->session_negotiate->ciphersuite = i;
|
||||
ssl->session_negotiate->compression = comp;
|
||||
ssl->session_negotiate->length = n;
|
||||
|
@@ -35,7 +35,10 @@
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#if defined(POLARSSL_HAVE_TIME)
|
||||
#include <time.h>
|
||||
#endif
|
||||
|
||||
static int ssl_parse_servername_ext( ssl_context *ssl,
|
||||
const unsigned char *buf,
|
||||
@@ -933,7 +936,9 @@ have_ciphersuite:
|
||||
|
||||
static int ssl_write_server_hello( ssl_context *ssl )
|
||||
{
|
||||
#if defined(POLARSSL_HAVE_TIME)
|
||||
time_t t;
|
||||
#endif
|
||||
int ret, n;
|
||||
size_t ext_len = 0;
|
||||
unsigned char *buf, *p;
|
||||
@@ -956,6 +961,7 @@ static int ssl_write_server_hello( ssl_context *ssl )
|
||||
SSL_DEBUG_MSG( 3, ( "server hello, chosen version: [%d:%d]",
|
||||
buf[4], buf[5] ) );
|
||||
|
||||
#if defined(POLARSSL_HAVE_TIME)
|
||||
t = time( NULL );
|
||||
*p++ = (unsigned char)( t >> 24 );
|
||||
*p++ = (unsigned char)( t >> 16 );
|
||||
@@ -963,6 +969,12 @@ static int ssl_write_server_hello( ssl_context *ssl )
|
||||
*p++ = (unsigned char)( t );
|
||||
|
||||
SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu", t ) );
|
||||
#else
|
||||
if( ( ret = ssl->f_rng( ssl->p_rng, p, 4 ) ) != 0 )
|
||||
return( ret );
|
||||
|
||||
p += 4;
|
||||
#endif
|
||||
|
||||
if( ( ret = ssl->f_rng( ssl->p_rng, p, 28 ) ) != 0 )
|
||||
return( ret );
|
||||
|
@@ -54,7 +54,6 @@
|
||||
#endif
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
#if defined _MSC_VER && !defined strcasecmp
|
||||
#define strcasecmp _stricmp
|
||||
|
@@ -2875,6 +2875,7 @@ int x509parse_crl_info( char *buf, size_t size, const char *prefix,
|
||||
/*
|
||||
* Return 0 if the x509_time is still valid, or 1 otherwise.
|
||||
*/
|
||||
#if defined(POLARSSL_HAVE_TIME)
|
||||
int x509parse_time_expired( const x509_time *to )
|
||||
{
|
||||
int year, mon, day;
|
||||
@@ -2941,6 +2942,13 @@ int x509parse_time_expired( const x509_time *to )
|
||||
|
||||
return( 0 );
|
||||
}
|
||||
#else /* POLARSSL_HAVE_TIME */
|
||||
int x509parse_time_expired( const x509_time *to )
|
||||
{
|
||||
((void) to);
|
||||
return( 0 );
|
||||
}
|
||||
#endif /* POLARSSL_HAVE_TIME */
|
||||
|
||||
/*
|
||||
* Return 1 if the certificate is revoked, or 0 otherwise.
|
||||
|
Reference in New Issue
Block a user