mirror of
https://github.com/Mbed-TLS/mbedtls.git
synced 2025-12-24 17:41:01 +03:00
Merge branch 'iotssl-1770' into development_thomas_dee
This commit is contained in:
1
programs/.gitignore
vendored
1
programs/.gitignore
vendored
@@ -45,6 +45,7 @@ ssl/mini_client
|
||||
test/benchmark
|
||||
test/ecp-bench
|
||||
test/selftest
|
||||
test/cpp_dummy_build
|
||||
test/ssl_cert_test
|
||||
test/udp_proxy
|
||||
test/zeroize
|
||||
|
||||
@@ -4,9 +4,11 @@
|
||||
|
||||
CFLAGS ?= -O2
|
||||
WARNING_CFLAGS ?= -Wall -W -Wdeclaration-after-statement
|
||||
WARNING_CXXFLAGS ?= -Wall -W
|
||||
LDFLAGS ?=
|
||||
|
||||
LOCAL_CFLAGS = $(WARNING_CFLAGS) -I../include -D_FILE_OFFSET_BITS=64
|
||||
LOCAL_CXXFLAGS = $(WARNING_CXXFLAGS) -I../include -D_FILE_OFFSET_BITS=64
|
||||
LOCAL_LDFLAGS = -L../library \
|
||||
-lmbedtls$(SHARED_SUFFIX) \
|
||||
-lmbedx509$(SHARED_SUFFIX) \
|
||||
@@ -77,6 +79,10 @@ ifdef PTHREAD
|
||||
APPS += ssl/ssl_pthread_server$(EXEXT)
|
||||
endif
|
||||
|
||||
ifdef TEST_CPP
|
||||
APPS += test/cpp_dummy_build$(EXEXT)
|
||||
endif
|
||||
|
||||
.SILENT:
|
||||
|
||||
.PHONY: all clean list
|
||||
@@ -242,6 +248,10 @@ test/benchmark$(EXEXT): test/benchmark.c $(DEP)
|
||||
echo " CC test/benchmark.c"
|
||||
$(CC) $(LOCAL_CFLAGS) $(CFLAGS) test/benchmark.c $(LOCAL_LDFLAGS) $(LDFLAGS) -o $@
|
||||
|
||||
test/cpp_dummy_build$(EXEXT): test/cpp_dummy_build.cpp $(DEP)
|
||||
echo " CXX test/cpp_dummy_build.cpp"
|
||||
$(CXX) $(LOCAL_CXXFLAGS) $(CXXFLAGS) test/cpp_dummy_build.cpp $(LOCAL_LDFLAGS) $(LDFLAGS) -o $@
|
||||
|
||||
test/selftest$(EXEXT): test/selftest.c $(DEP)
|
||||
echo " CC test/selftest.c"
|
||||
$(CC) $(LOCAL_CFLAGS) $(CFLAGS) test/selftest.c $(LOCAL_LDFLAGS) $(LDFLAGS) -o $@
|
||||
|
||||
125
programs/README.md
Normal file
125
programs/README.md
Normal file
@@ -0,0 +1,125 @@
|
||||
Mbed TLS sample programs
|
||||
========================
|
||||
|
||||
This subdirectory mostly contains sample programs that illustrate specific features of the library, as well as a few test and support programs.
|
||||
|
||||
## Symmetric cryptography (AES) examples
|
||||
|
||||
* [`aes/aescrypt2.c`](aes/aescrypt2.c): file encryption and authentication with a key derived from a low-entropy secret, demonstrating the low-level AES interface, the digest interface and HMAC.
|
||||
Warning: this program illustrates how to use low-level functions in the library. It should not be taken as an example of how to build a secure encryption mechanism. To derive a key from a low-entropy secret such as a password, use a standard key stretching mechanism such as PBKDF2 (provided by the `pkcs5` module). To encrypt and authenticate data, use a standard mode such as GCM or CCM (both available as library module).
|
||||
|
||||
* [`aes/crypt_and_hash.c`](aes/crypt_and_hash.c): file encryption and authentication, demonstrating the generic cipher interface and the generic hash interface.
|
||||
|
||||
## Hash (digest) examples
|
||||
|
||||
* [`hash/generic_sum.c`](hash/generic_sum.c): file hash calculator and verifier, demonstrating the message digest (`md`) interface.
|
||||
|
||||
* [`hash/hello.c`](hash/hello.c): hello-world program for MD5.
|
||||
|
||||
## Public-key cryptography examples
|
||||
|
||||
### Generic public-key cryptography (`pk`) examples
|
||||
|
||||
* [`pkey/gen_key.c`](pkey/gen_key.c): generates a key for any of the supported public-key algorithms (RSA or ECC) and writes it to a file that can be used by the other pk sample programs.
|
||||
|
||||
* [`pkey/key_app.c`](pkey/key_app.c): loads a PEM or DER public key or private key file and dumps its content.
|
||||
|
||||
* [`pkey/key_app_writer.c`](pkey/key_app_writer.c): loads a PEM or DER public key or private key file and writes it to a new PEM or DER file.
|
||||
|
||||
* [`pkey/pk_encrypt.c`](pkey/pk_encrypt.c), [`pkey/pk_decrypt.c`](pkey/pk_decrypt.c): loads a PEM or DER public/private key file and uses the key to encrypt/decrypt a short string through the generic public-key interface.
|
||||
|
||||
* [`pkey/pk_sign.c`](pkey/pk_sign.c), [`pkey/pk_verify.c`](pkey/pk_verify.c): loads a PEM or DER private/public key file and uses the key to sign/verify a short string.
|
||||
|
||||
### ECDSA and RSA signature examples
|
||||
|
||||
* [`pkey/ecdsa.c`](pkey/ecdsa.c): generates an ECDSA key, signs a fixed message and verifies the signature.
|
||||
|
||||
* [`pkey/rsa_encrypt.c`](pkey/rsa_encrypt.c), [`pkey/rsa_decrypt.c`](pkey/rsa_decrypt.c): loads an RSA public/private key and uses it to encrypt/decrypt a short string through the low-level RSA interface.
|
||||
|
||||
* [`pkey/rsa_genkey.c`](pkey/rsa_genkey.c): generates an RSA key and writes it to a file that can be used with the other RSA sample programs.
|
||||
|
||||
* [`pkey/rsa_sign.c`](pkey/rsa_sign.c), [`pkey/rsa_verify.c`](pkey/rsa_verify.c): loads an RSA private/public key and uses it to sign/verify a short string with the RSA PKCS#1 v1.5 algorithm.
|
||||
|
||||
* [`pkey/rsa_sign_pss.c`](pkey/rsa_sign_pss.c), [`pkey/rsa_verify_pss.c`](pkey/rsa_verify_pss.c): loads an RSA private/public key and uses it to sign/verify a short string with the RSASSA-PSS algorithm.
|
||||
|
||||
### Diffie-Hellman key exchange examples
|
||||
|
||||
* [`pkey/dh_client.c`](pkey/dh_client.c), [`pkey/dh_server.c`](pkey/dh_server.c): secure channel demonstrators (client, server). This pair of programs illustrates how to set up a secure channel using RSA for authentication and Diffie-Hellman to generate a shared AES session key.
|
||||
|
||||
* [`pkey/ecdh_curve25519.c`](pkey/ecdh_curve25519.c): demonstration of a elliptic curve Diffie-Hellman (ECDH) key agreement.
|
||||
|
||||
### Bignum (`mpi`) usage examples
|
||||
|
||||
* [`pkey/dh_genprime.c`](pkey/dh_genprime.c): shows how to use the bignum (`mpi`) interface to generate Diffie-Hellman parameters.
|
||||
|
||||
* [`pkey/mpi_demo.c`](pkey/mpi_demo.c): demonstrates operations on big integers.
|
||||
|
||||
## Random number generator (RNG) examples
|
||||
|
||||
* [`random/gen_entropy.c`](random/gen_entropy.c): shows how to use the default entropy sources to generate random data.
|
||||
Note: most applications should only use the entropy generator to seed a cryptographic pseudorandom generator, as illustrated by `random/gen_random_ctr_drbg.c`.
|
||||
|
||||
* [`random/gen_random_ctr_drbg.c`](random/gen_random_ctr_drbg.c): shows how to use the default entropy sources to seed a pseudorandom generator, and how to use the resulting random generator to generate random data.
|
||||
|
||||
* [`random/gen_random_havege.c`](random/gen_random_havege.c): demonstrates the HAVEGE entropy collector.
|
||||
|
||||
## SSL/TLS examples
|
||||
|
||||
### SSL/TLS sample applications
|
||||
|
||||
* [`ssl/dtls_client.c`](ssl/dtls_client.c): a simple DTLS client program, which sends one datagram to the server and reads one datagram in response.
|
||||
|
||||
* [`ssl/dtls_server.c`](ssl/dtls_server.c): a simple DTLS server program, which expects one datagram from the client and writes one datagram in response. This program supports DTLS cookies for hello verification.
|
||||
|
||||
* [`ssl/mini_client.c`](ssl/mini_client.c): a minimalistic SSL client, which sends a short string and disconnects. This is primarily intended as a benchmark; for a better example of a typical TLS client, see `ssl/ssl_client1.c`.
|
||||
|
||||
* [`ssl/ssl_client1.c`](ssl/ssl_client1.c): a simple HTTPS client that sends a fixed request and displays the response.
|
||||
|
||||
* [`ssl/ssl_fork_server.c`](ssl/ssl_fork_server.c): a simple HTTPS server using one process per client to send a fixed response. This program requires a Unix/POSIX environment implementing the `fork` system call.
|
||||
|
||||
* [`ssl/ssl_mail_client.c`](ssl/ssl_mail_client.c): a simple SMTP-over-TLS or SMTP-STARTTLS client. This client sends an email with fixed content.
|
||||
|
||||
* [`ssl/ssl_pthread_server.c`](ssl/ssl_pthread_server.c): a simple HTTPS server using one thread per client to send a fixed response. This program requires the pthread library.
|
||||
|
||||
* [`ssl/ssl_server.c`](ssl/ssl_server.c): a simple HTTPS server that sends a fixed response. It serves a single client at a time.
|
||||
|
||||
### SSL/TLS feature demonstrators
|
||||
|
||||
Note: unlike most of the other programs under the `programs/` directory, these two programs are not intended as a basis for writing an application. They combine most of the features supported by the library, and most applications require only a few features. To write a new application, we recommended that you start with `ssl_client1.c` or `ssl_server.c`, and then look inside `ssl/ssl_client2.c` or `ssl/ssl_server2.c` to see how to use the specific features that your application needs.
|
||||
|
||||
* [`ssl/ssl_client2.c`](ssl/ssl_client2.c): an HTTPS client that sends a fixed request and displays the response, with options to select TLS protocol features and Mbed TLS library features.
|
||||
|
||||
* [`ssl/ssl_server2.c`](ssl/ssl_server2.c): an HTTPS server that sends a fixed response, with options to select TLS protocol features and Mbed TLS library features.
|
||||
|
||||
In addition to providing options for testing client-side features, the `ssl_client2` program has options that allow you to trigger certain behaviors in the server. For example, there are options to select ciphersuites, or to force a renegotiation. These options are useful for testing the corresponding features in a TLS server. Likewise, `ssl_server2` has options to activate certain behaviors that are useful for testing a TLS client.
|
||||
|
||||
## Test utilities
|
||||
|
||||
* [`test/benchmark.c`](test/benchmark.c): benchmark for cryptographic algorithms.
|
||||
|
||||
* [`test/selftest.c`](test/selftest.c): runs the self-test function in each library module.
|
||||
|
||||
* [`test/ssl_cert_test.c`](test/ssl_cert_test.c): demonstrates how to verify X.509 certificates, and (for RSA keys only) how to check that each certificate matches the corresponding private key. This program requires some test data which is not provided.
|
||||
|
||||
* [`test/udp_proxy.c`](test/udp_proxy.c): a UDP proxy that can inject certain failures (delay, duplicate, drop). Useful for testing DTLS.
|
||||
|
||||
* [`test/zeroize.c`](test/zeroize.c): a test program for `mbedtls_platform_zeroize`, used by [`tests/scripts/test_zeroize.gdb`](tests/scripts/test_zeroize.gdb).
|
||||
|
||||
## Development utilities
|
||||
|
||||
* [`util/pem2der.c`](util/pem2der.c): a PEM to DER converter. Mbed TLS can read PEM files directly, but this utility can be useful for interacting with other tools or with minimal Mbed TLS builds that lack PEM support.
|
||||
|
||||
* [`util/strerror.c`](util/strerror.c): prints the error description corresponding to an integer status returned by an Mbed TLS function.
|
||||
|
||||
## X.509 certificate examples
|
||||
|
||||
* [`x509/cert_app.c`](x509/cert_app.c): connects to a TLS server and verifies its certificate chain.
|
||||
|
||||
* [`x509/cert_req.c`](x509/cert_req.c): generates a certificate signing request (CSR) for a private key.
|
||||
|
||||
* [`x509/cert_write.c`](x509/cert_write.c): signs a certificate signing request, or self-signs a certificate.
|
||||
|
||||
* [`x509/crl_app.c`](x509/crl_app.c): loads and dumps a certificate revocation list (CRL).
|
||||
|
||||
* [`x509/req_app.c`](x509/req_app.c): loads and dumps a certificate signing request (CSR).
|
||||
|
||||
@@ -19,6 +19,11 @@
|
||||
* This file is part of mbed TLS (https://tls.mbed.org)
|
||||
*/
|
||||
|
||||
/* Enable definition of fileno() even when compiling with -std=c99. Must be
|
||||
* set before config.h, which pulls in glibc's features.h indirectly.
|
||||
* Harmless on other platforms. */
|
||||
#define _POSIX_C_SOURCE 1
|
||||
|
||||
#if !defined(MBEDTLS_CONFIG_FILE)
|
||||
#include "mbedtls/config.h"
|
||||
#else
|
||||
@@ -29,9 +34,12 @@
|
||||
#include "mbedtls/platform.h"
|
||||
#else
|
||||
#include <stdio.h>
|
||||
#define mbedtls_fprintf fprintf
|
||||
#define mbedtls_printf printf
|
||||
#endif
|
||||
#include <stdlib.h>
|
||||
#define mbedtls_fprintf fprintf
|
||||
#define mbedtls_printf printf
|
||||
#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
|
||||
#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
|
||||
#endif /* MBEDTLS_PLATFORM_C */
|
||||
|
||||
#include "mbedtls/aes.h"
|
||||
#include "mbedtls/md.h"
|
||||
@@ -71,7 +79,8 @@ int main( void )
|
||||
#else
|
||||
int main( int argc, char *argv[] )
|
||||
{
|
||||
int ret = 1;
|
||||
int ret = 0;
|
||||
int exit_code = MBEDTLS_EXIT_FAILURE;
|
||||
|
||||
unsigned int i, n;
|
||||
int mode, lastn;
|
||||
@@ -429,7 +438,7 @@ int main( int argc, char *argv[] )
|
||||
}
|
||||
}
|
||||
|
||||
ret = 0;
|
||||
exit_code = MBEDTLS_EXIT_SUCCESS;
|
||||
|
||||
exit:
|
||||
if( fin )
|
||||
@@ -452,6 +461,6 @@ exit:
|
||||
mbedtls_aes_free( &aes_ctx );
|
||||
mbedtls_md_free( &sha_ctx );
|
||||
|
||||
return( ret );
|
||||
return( exit_code );
|
||||
}
|
||||
#endif /* MBEDTLS_AES_C && MBEDTLS_SHA256_C && MBEDTLS_FS_IO */
|
||||
|
||||
@@ -20,6 +20,11 @@
|
||||
* This file is part of mbed TLS (https://tls.mbed.org)
|
||||
*/
|
||||
|
||||
/* Enable definition of fileno() even when compiling with -std=c99. Must be
|
||||
* set before config.h, which pulls in glibc's features.h indirectly.
|
||||
* Harmless on other platforms. */
|
||||
#define _POSIX_C_SOURCE 1
|
||||
|
||||
#if !defined(MBEDTLS_CONFIG_FILE)
|
||||
#include "mbedtls/config.h"
|
||||
#else
|
||||
@@ -30,9 +35,12 @@
|
||||
#include "mbedtls/platform.h"
|
||||
#else
|
||||
#include <stdio.h>
|
||||
#define mbedtls_fprintf fprintf
|
||||
#define mbedtls_printf printf
|
||||
#endif
|
||||
#include <stdlib.h>
|
||||
#define mbedtls_fprintf fprintf
|
||||
#define mbedtls_printf printf
|
||||
#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
|
||||
#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
|
||||
#endif /* MBEDTLS_PLATFORM_C */
|
||||
|
||||
#if defined(MBEDTLS_CIPHER_C) && defined(MBEDTLS_MD_C) && \
|
||||
defined(MBEDTLS_FS_IO)
|
||||
@@ -74,6 +82,7 @@ int main( void )
|
||||
int main( int argc, char *argv[] )
|
||||
{
|
||||
int ret = 1, i, n;
|
||||
int exit_code = MBEDTLS_EXIT_FAILURE;
|
||||
int mode;
|
||||
size_t keylen, ilen, olen;
|
||||
FILE *fkey, *fin = NULL, *fout = NULL;
|
||||
@@ -526,7 +535,7 @@ int main( int argc, char *argv[] )
|
||||
}
|
||||
}
|
||||
|
||||
ret = 0;
|
||||
exit_code = MBEDTLS_EXIT_SUCCESS;
|
||||
|
||||
exit:
|
||||
if( fin )
|
||||
@@ -549,6 +558,6 @@ exit:
|
||||
mbedtls_cipher_free( &cipher_ctx );
|
||||
mbedtls_md_free( &md_ctx );
|
||||
|
||||
return( ret );
|
||||
return( exit_code );
|
||||
}
|
||||
#endif /* MBEDTLS_CIPHER_C && MBEDTLS_MD_C && MBEDTLS_FS_IO */
|
||||
|
||||
@@ -29,9 +29,12 @@
|
||||
#include "mbedtls/platform.h"
|
||||
#else
|
||||
#include <stdio.h>
|
||||
#define mbedtls_fprintf fprintf
|
||||
#define mbedtls_printf printf
|
||||
#endif
|
||||
#include <stdlib.h>
|
||||
#define mbedtls_fprintf fprintf
|
||||
#define mbedtls_printf printf
|
||||
#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
|
||||
#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
|
||||
#endif /* MBEDTLS_PLATFORM_C */
|
||||
|
||||
#if defined(MBEDTLS_MD_C) && defined(MBEDTLS_FS_IO)
|
||||
#include "mbedtls/md.h"
|
||||
@@ -169,7 +172,8 @@ static int generic_check( const mbedtls_md_info_t *md_info, char *filename )
|
||||
|
||||
int main( int argc, char *argv[] )
|
||||
{
|
||||
int ret, i;
|
||||
int ret = 1, i;
|
||||
int exit_code = MBEDTLS_EXIT_FAILURE;
|
||||
const mbedtls_md_info_t *md_info;
|
||||
mbedtls_md_context_t md_ctx;
|
||||
|
||||
@@ -196,7 +200,7 @@ int main( int argc, char *argv[] )
|
||||
fflush( stdout ); getchar();
|
||||
#endif
|
||||
|
||||
return( 1 );
|
||||
return( exit_code );
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -206,12 +210,12 @@ int main( int argc, char *argv[] )
|
||||
if( md_info == NULL )
|
||||
{
|
||||
mbedtls_fprintf( stderr, "Message Digest '%s' not found\n", argv[1] );
|
||||
return( 1 );
|
||||
return( exit_code );
|
||||
}
|
||||
if( mbedtls_md_setup( &md_ctx, md_info, 0 ) )
|
||||
{
|
||||
mbedtls_fprintf( stderr, "Failed to initialize context.\n" );
|
||||
return( 1 );
|
||||
return( exit_code );
|
||||
}
|
||||
|
||||
ret = 0;
|
||||
@@ -224,9 +228,12 @@ int main( int argc, char *argv[] )
|
||||
for( i = 2; i < argc; i++ )
|
||||
ret |= generic_print( md_info, argv[i] );
|
||||
|
||||
if ( ret == 0 )
|
||||
exit_code = MBEDTLS_EXIT_SUCCESS;
|
||||
|
||||
exit:
|
||||
mbedtls_md_free( &md_ctx );
|
||||
|
||||
return( ret );
|
||||
return( exit_code );
|
||||
}
|
||||
#endif /* MBEDTLS_MD_C && MBEDTLS_FS_IO */
|
||||
|
||||
@@ -29,9 +29,12 @@
|
||||
#include "mbedtls/platform.h"
|
||||
#else
|
||||
#include <stdio.h>
|
||||
#define mbedtls_printf printf
|
||||
#define mbedtls_time_t time_t
|
||||
#endif
|
||||
#include <stdlib.h>
|
||||
#define mbedtls_printf printf
|
||||
#define mbedtls_time_t time_t
|
||||
#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
|
||||
#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
|
||||
#endif /* MBEDTLS_PLATFORM_C */
|
||||
|
||||
#if defined(MBEDTLS_AES_C) && defined(MBEDTLS_DHM_C) && \
|
||||
defined(MBEDTLS_ENTROPY_C) && defined(MBEDTLS_NET_C) && \
|
||||
@@ -71,7 +74,8 @@ int main( void )
|
||||
{
|
||||
FILE *f;
|
||||
|
||||
int ret;
|
||||
int ret = 1;
|
||||
int exit_code = MBEDTLS_EXIT_FAILURE;
|
||||
size_t n, buflen;
|
||||
mbedtls_net_context server_fd;
|
||||
|
||||
@@ -115,7 +119,6 @@ int main( void )
|
||||
|
||||
if( ( f = fopen( "rsa_pub.txt", "rb" ) ) == NULL )
|
||||
{
|
||||
ret = 1;
|
||||
mbedtls_printf( " failed\n ! Could not open rsa_pub.txt\n" \
|
||||
" ! Please run rsa_genkey first\n\n" );
|
||||
goto exit;
|
||||
@@ -191,7 +194,6 @@ int main( void )
|
||||
|
||||
if( dhm.len < 64 || dhm.len > 512 )
|
||||
{
|
||||
ret = 1;
|
||||
mbedtls_printf( " failed\n ! Invalid DHM modulus size\n\n" );
|
||||
goto exit;
|
||||
}
|
||||
@@ -207,7 +209,6 @@ int main( void )
|
||||
|
||||
if( ( n = (size_t) ( end - p ) ) != rsa.len )
|
||||
{
|
||||
ret = 1;
|
||||
mbedtls_printf( " failed\n ! Invalid RSA signature size\n\n" );
|
||||
goto exit;
|
||||
}
|
||||
@@ -286,6 +287,8 @@ int main( void )
|
||||
buf[16] = '\0';
|
||||
mbedtls_printf( "\n . Plaintext is \"%s\"\n\n", (char *) buf );
|
||||
|
||||
exit_code = MBEDTLS_EXIT_SUCCESS;
|
||||
|
||||
exit:
|
||||
|
||||
mbedtls_net_free( &server_fd );
|
||||
@@ -301,7 +304,7 @@ exit:
|
||||
fflush( stdout ); getchar();
|
||||
#endif
|
||||
|
||||
return( ret );
|
||||
return( exit_code );
|
||||
}
|
||||
#endif /* MBEDTLS_AES_C && MBEDTLS_DHM_C && MBEDTLS_ENTROPY_C &&
|
||||
MBEDTLS_NET_C && MBEDTLS_RSA_C && MBEDTLS_SHA256_C &&
|
||||
|
||||
@@ -30,9 +30,11 @@
|
||||
#else
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#define mbedtls_printf printf
|
||||
#define mbedtls_time_t time_t
|
||||
#endif
|
||||
#define mbedtls_printf printf
|
||||
#define mbedtls_time_t time_t
|
||||
#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
|
||||
#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
|
||||
#endif /* MBEDTLS_PLATFORM_C */
|
||||
|
||||
#if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_ENTROPY_C) || \
|
||||
!defined(MBEDTLS_FS_IO) || !defined(MBEDTLS_CTR_DRBG_C) || \
|
||||
@@ -69,6 +71,7 @@ int main( void )
|
||||
int main( int argc, char **argv )
|
||||
{
|
||||
int ret = 1;
|
||||
int exit_code = MBEDTLS_EXIT_FAILURE;
|
||||
mbedtls_mpi G, P, Q;
|
||||
mbedtls_entropy_context entropy;
|
||||
mbedtls_ctr_drbg_context ctr_drbg;
|
||||
@@ -86,7 +89,7 @@ int main( int argc, char **argv )
|
||||
{
|
||||
usage:
|
||||
mbedtls_printf( USAGE );
|
||||
return( 1 );
|
||||
return( exit_code );
|
||||
}
|
||||
|
||||
for( i = 1; i < argc; i++ )
|
||||
@@ -164,7 +167,6 @@ int main( int argc, char **argv )
|
||||
|
||||
if( ( fout = fopen( "dh_prime.txt", "wb+" ) ) == NULL )
|
||||
{
|
||||
ret = 1;
|
||||
mbedtls_printf( " failed\n ! Could not create dh_prime.txt\n\n" );
|
||||
goto exit;
|
||||
}
|
||||
@@ -180,6 +182,8 @@ int main( int argc, char **argv )
|
||||
mbedtls_printf( " ok\n\n" );
|
||||
fclose( fout );
|
||||
|
||||
exit_code = MBEDTLS_EXIT_SUCCESS;
|
||||
|
||||
exit:
|
||||
|
||||
mbedtls_mpi_free( &G ); mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
|
||||
@@ -191,7 +195,7 @@ exit:
|
||||
fflush( stdout ); getchar();
|
||||
#endif
|
||||
|
||||
return( ret );
|
||||
return( exit_code );
|
||||
}
|
||||
#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_FS_IO &&
|
||||
MBEDTLS_CTR_DRBG_C && MBEDTLS_GENPRIME */
|
||||
|
||||
@@ -29,9 +29,12 @@
|
||||
#include "mbedtls/platform.h"
|
||||
#else
|
||||
#include <stdio.h>
|
||||
#define mbedtls_printf printf
|
||||
#define mbedtls_time_t time_t
|
||||
#endif
|
||||
#include <stdlib.h>
|
||||
#define mbedtls_printf printf
|
||||
#define mbedtls_time_t time_t
|
||||
#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
|
||||
#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
|
||||
#endif /* MBEDTLS_PLATFORM_C */
|
||||
|
||||
#if defined(MBEDTLS_AES_C) && defined(MBEDTLS_DHM_C) && \
|
||||
defined(MBEDTLS_ENTROPY_C) && defined(MBEDTLS_NET_C) && \
|
||||
@@ -71,7 +74,8 @@ int main( void )
|
||||
{
|
||||
FILE *f;
|
||||
|
||||
int ret;
|
||||
int ret = 1;
|
||||
int exit_code = MBEDTLS_EXIT_FAILURE;
|
||||
size_t n, buflen;
|
||||
mbedtls_net_context listen_fd, client_fd;
|
||||
|
||||
@@ -121,7 +125,6 @@ int main( void )
|
||||
|
||||
if( ( f = fopen( "rsa_priv.txt", "rb" ) ) == NULL )
|
||||
{
|
||||
ret = 1;
|
||||
mbedtls_printf( " failed\n ! Could not open rsa_priv.txt\n" \
|
||||
" ! Please run rsa_genkey first\n\n" );
|
||||
goto exit;
|
||||
@@ -164,7 +167,6 @@ int main( void )
|
||||
|
||||
if( ( f = fopen( "dh_prime.txt", "rb" ) ) == NULL )
|
||||
{
|
||||
ret = 1;
|
||||
mbedtls_printf( " failed\n ! Could not open dh_prime.txt\n" \
|
||||
" ! Please run dh_genprime first\n\n" );
|
||||
goto exit;
|
||||
@@ -304,6 +306,8 @@ int main( void )
|
||||
|
||||
mbedtls_printf( "\n\n" );
|
||||
|
||||
exit_code = MBEDTLS_EXIT_SUCCESS;
|
||||
|
||||
exit:
|
||||
|
||||
mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
|
||||
@@ -323,7 +327,7 @@ exit:
|
||||
fflush( stdout ); getchar();
|
||||
#endif
|
||||
|
||||
return( ret );
|
||||
return( exit_code );
|
||||
}
|
||||
#endif /* MBEDTLS_AES_C && MBEDTLS_DHM_C && MBEDTLS_ENTROPY_C &&
|
||||
MBEDTLS_NET_C && MBEDTLS_RSA_C && MBEDTLS_SHA256_C &&
|
||||
|
||||
@@ -29,8 +29,11 @@
|
||||
#include "mbedtls/platform.h"
|
||||
#else
|
||||
#include <stdio.h>
|
||||
#define mbedtls_printf printf
|
||||
#endif
|
||||
#include <stdlib.h>
|
||||
#define mbedtls_printf printf
|
||||
#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
|
||||
#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
|
||||
#endif /* MBEDTLS_PLATFORM_C */
|
||||
|
||||
#if !defined(MBEDTLS_ECDH_C) || \
|
||||
!defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED) || \
|
||||
@@ -51,7 +54,8 @@ int main( void )
|
||||
|
||||
int main( int argc, char *argv[] )
|
||||
{
|
||||
int ret;
|
||||
int ret = 1;
|
||||
int exit_code = MBEDTLS_EXIT_FAILURE;
|
||||
mbedtls_ecdh_context ctx_cli, ctx_srv;
|
||||
mbedtls_entropy_context entropy;
|
||||
mbedtls_ctr_drbg_context ctr_drbg;
|
||||
@@ -218,6 +222,7 @@ int main( int argc, char *argv[] )
|
||||
|
||||
mbedtls_printf( " ok\n" );
|
||||
|
||||
exit_code = MBEDTLS_EXIT_SUCCESS;
|
||||
|
||||
exit:
|
||||
|
||||
@@ -231,7 +236,7 @@ exit:
|
||||
mbedtls_ctr_drbg_free( &ctr_drbg );
|
||||
mbedtls_entropy_free( &entropy );
|
||||
|
||||
return( ret != 0 );
|
||||
return( exit_code );
|
||||
}
|
||||
#endif /* MBEDTLS_ECDH_C && MBEDTLS_ECP_DP_CURVE25519_ENABLED &&
|
||||
MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C */
|
||||
|
||||
@@ -29,8 +29,11 @@
|
||||
#include "mbedtls/platform.h"
|
||||
#else
|
||||
#include <stdio.h>
|
||||
#define mbedtls_printf printf
|
||||
#endif
|
||||
#include <stdlib.h>
|
||||
#define mbedtls_printf printf
|
||||
#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
|
||||
#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
|
||||
#endif /* MBEDTLS_PLATFORM_C */
|
||||
|
||||
#if defined(MBEDTLS_ECDSA_C) && \
|
||||
defined(MBEDTLS_ENTROPY_C) && defined(MBEDTLS_CTR_DRBG_C)
|
||||
@@ -98,7 +101,8 @@ static void dump_pubkey( const char *title, mbedtls_ecdsa_context *key )
|
||||
|
||||
int main( int argc, char *argv[] )
|
||||
{
|
||||
int ret;
|
||||
int ret = 1;
|
||||
int exit_code = MBEDTLS_EXIT_FAILURE;
|
||||
mbedtls_ecdsa_context ctx_sign, ctx_verify;
|
||||
mbedtls_entropy_context entropy;
|
||||
mbedtls_ctr_drbg_context ctr_drbg;
|
||||
@@ -115,7 +119,6 @@ int main( int argc, char *argv[] )
|
||||
|
||||
memset( sig, 0, sizeof( sig ) );
|
||||
memset( message, 0x25, sizeof( message ) );
|
||||
ret = 1;
|
||||
|
||||
if( argc != 1 )
|
||||
{
|
||||
@@ -213,8 +216,6 @@ int main( int argc, char *argv[] )
|
||||
goto exit;
|
||||
}
|
||||
|
||||
ret = 0;
|
||||
|
||||
/*
|
||||
* Verify signature
|
||||
*/
|
||||
@@ -231,6 +232,8 @@ int main( int argc, char *argv[] )
|
||||
|
||||
mbedtls_printf( " ok\n" );
|
||||
|
||||
exit_code = MBEDTLS_EXIT_SUCCESS;
|
||||
|
||||
exit:
|
||||
|
||||
#if defined(_WIN32)
|
||||
@@ -243,7 +246,7 @@ exit:
|
||||
mbedtls_ctr_drbg_free( &ctr_drbg );
|
||||
mbedtls_entropy_free( &entropy );
|
||||
|
||||
return( ret );
|
||||
return( exit_code );
|
||||
}
|
||||
#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C &&
|
||||
ECPARAMS */
|
||||
|
||||
@@ -29,8 +29,11 @@
|
||||
#include "mbedtls/platform.h"
|
||||
#else
|
||||
#include <stdio.h>
|
||||
#define mbedtls_printf printf
|
||||
#endif
|
||||
#include <stdlib.h>
|
||||
#define mbedtls_printf printf
|
||||
#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
|
||||
#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
|
||||
#endif /* MBEDTLS_PLATFORM_C */
|
||||
|
||||
#if defined(MBEDTLS_PK_WRITE_C) && defined(MBEDTLS_FS_IO) && \
|
||||
defined(MBEDTLS_ENTROPY_C) && defined(MBEDTLS_CTR_DRBG_C)
|
||||
@@ -186,7 +189,8 @@ static int write_private_key( mbedtls_pk_context *key, const char *output_file )
|
||||
|
||||
int main( int argc, char *argv[] )
|
||||
{
|
||||
int ret = 0;
|
||||
int ret = 1;
|
||||
int exit_code = MBEDTLS_EXIT_FAILURE;
|
||||
mbedtls_pk_context key;
|
||||
char buf[1024];
|
||||
int i;
|
||||
@@ -214,7 +218,6 @@ int main( int argc, char *argv[] )
|
||||
if( argc == 0 )
|
||||
{
|
||||
usage:
|
||||
ret = 1;
|
||||
mbedtls_printf( USAGE );
|
||||
#if defined(MBEDTLS_ECP_C)
|
||||
mbedtls_printf( " available ec_curve values:\n" );
|
||||
@@ -222,7 +225,7 @@ int main( int argc, char *argv[] )
|
||||
mbedtls_printf( " %s (default)\n", curve_info->name );
|
||||
while( ( ++curve_info )->name != NULL )
|
||||
mbedtls_printf( " %s\n", curve_info->name );
|
||||
#endif
|
||||
#endif /* MBEDTLS_ECP_C */
|
||||
goto exit;
|
||||
}
|
||||
|
||||
@@ -411,9 +414,11 @@ int main( int argc, char *argv[] )
|
||||
|
||||
mbedtls_printf( " ok\n" );
|
||||
|
||||
exit_code = MBEDTLS_EXIT_SUCCESS;
|
||||
|
||||
exit:
|
||||
|
||||
if( ret != 0 && ret != 1)
|
||||
if( exit_code != MBEDTLS_EXIT_SUCCESS )
|
||||
{
|
||||
#ifdef MBEDTLS_ERROR_C
|
||||
mbedtls_strerror( ret, buf, sizeof( buf ) );
|
||||
@@ -436,7 +441,7 @@ exit:
|
||||
fflush( stdout ); getchar();
|
||||
#endif
|
||||
|
||||
return( ret );
|
||||
return( exit_code );
|
||||
}
|
||||
#endif /* MBEDTLS_PK_WRITE_C && MBEDTLS_PEM_WRITE_C && MBEDTLS_FS_IO &&
|
||||
* MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C */
|
||||
|
||||
@@ -29,8 +29,11 @@
|
||||
#include "mbedtls/platform.h"
|
||||
#else
|
||||
#include <stdio.h>
|
||||
#define mbedtls_printf printf
|
||||
#endif
|
||||
#include <stdlib.h>
|
||||
#define mbedtls_printf printf
|
||||
#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
|
||||
#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
|
||||
#endif /* MBEDTLS_PLATFORM_C */
|
||||
|
||||
#if defined(MBEDTLS_BIGNUM_C) && \
|
||||
defined(MBEDTLS_PK_PARSE_C) && defined(MBEDTLS_FS_IO)
|
||||
@@ -83,7 +86,8 @@ struct options
|
||||
|
||||
int main( int argc, char *argv[] )
|
||||
{
|
||||
int ret = 0;
|
||||
int ret = 1;
|
||||
int exit_code = MBEDTLS_EXIT_FAILURE;
|
||||
char buf[1024];
|
||||
int i;
|
||||
char *p, *q;
|
||||
@@ -105,7 +109,7 @@ int main( int argc, char *argv[] )
|
||||
{
|
||||
usage:
|
||||
mbedtls_printf( USAGE );
|
||||
goto exit;
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
opt.mode = DFL_MODE;
|
||||
@@ -155,13 +159,13 @@ int main( int argc, char *argv[] )
|
||||
if( ( f = fopen( opt.password_file, "rb" ) ) == NULL )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! fopen returned NULL\n" );
|
||||
goto exit;
|
||||
goto cleanup;
|
||||
}
|
||||
if( fgets( buf, sizeof(buf), f ) == NULL )
|
||||
{
|
||||
fclose( f );
|
||||
mbedtls_printf( "Error: fgets() failed to retrieve password\n" );
|
||||
goto exit;
|
||||
goto cleanup;
|
||||
}
|
||||
fclose( f );
|
||||
|
||||
@@ -182,7 +186,7 @@ int main( int argc, char *argv[] )
|
||||
if( ret != 0 )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile returned -0x%04x\n", -ret );
|
||||
goto exit;
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
mbedtls_printf( " ok\n" );
|
||||
@@ -200,17 +204,17 @@ int main( int argc, char *argv[] )
|
||||
( ret = mbedtls_rsa_export_crt( rsa, &DP, &DQ, &QP ) ) != 0 )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! could not export RSA parameters\n\n" );
|
||||
goto exit;
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
mbedtls_mpi_write_file( "N: ", &N, 16, NULL );
|
||||
mbedtls_mpi_write_file( "E: ", &E, 16, NULL );
|
||||
mbedtls_mpi_write_file( "D: ", &D, 16, NULL );
|
||||
mbedtls_mpi_write_file( "P: ", &P, 16, NULL );
|
||||
mbedtls_mpi_write_file( "Q: ", &Q, 16, NULL );
|
||||
mbedtls_mpi_write_file( "DP: ", &DP, 16, NULL );
|
||||
mbedtls_mpi_write_file( "DQ: ", &DQ, 16, NULL );
|
||||
mbedtls_mpi_write_file( "QP: ", &QP, 16, NULL );
|
||||
MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "N: ", &N, 16, NULL ) );
|
||||
MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "E: ", &E, 16, NULL ) );
|
||||
MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "D: ", &D, 16, NULL ) );
|
||||
MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "P: ", &P, 16, NULL ) );
|
||||
MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q: ", &Q, 16, NULL ) );
|
||||
MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "DP: ", &DP, 16, NULL ) );
|
||||
MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "DQ: ", &DQ, 16, NULL ) );
|
||||
MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "QP: ", &QP, 16, NULL ) );
|
||||
}
|
||||
else
|
||||
#endif
|
||||
@@ -218,16 +222,16 @@ int main( int argc, char *argv[] )
|
||||
if( mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_ECKEY )
|
||||
{
|
||||
mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( pk );
|
||||
mbedtls_mpi_write_file( "Q(X): ", &ecp->Q.X, 16, NULL );
|
||||
mbedtls_mpi_write_file( "Q(Y): ", &ecp->Q.Y, 16, NULL );
|
||||
mbedtls_mpi_write_file( "Q(Z): ", &ecp->Q.Z, 16, NULL );
|
||||
mbedtls_mpi_write_file( "D : ", &ecp->d , 16, NULL );
|
||||
MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q(X): ", &ecp->Q.X, 16, NULL ) );
|
||||
MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q(Y): ", &ecp->Q.Y, 16, NULL ) );
|
||||
MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q(Z): ", &ecp->Q.Z, 16, NULL ) );
|
||||
MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "D : ", &ecp->d , 16, NULL ) );
|
||||
}
|
||||
else
|
||||
#endif
|
||||
{
|
||||
mbedtls_printf("Do not know how to print key information for this type\n" );
|
||||
goto exit;
|
||||
goto cleanup;
|
||||
}
|
||||
}
|
||||
else if( opt.mode == MODE_PUBLIC )
|
||||
@@ -243,7 +247,7 @@ int main( int argc, char *argv[] )
|
||||
if( ret != 0 )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! mbedtls_pk_parse_public_keyfile returned -0x%04x\n", -ret );
|
||||
goto exit;
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
mbedtls_printf( " ok\n" );
|
||||
@@ -258,10 +262,10 @@ int main( int argc, char *argv[] )
|
||||
NULL, &E ) ) != 0 )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! could not export RSA parameters\n\n" );
|
||||
goto exit;
|
||||
goto cleanup;
|
||||
}
|
||||
mbedtls_mpi_write_file( "N: ", &N, 16, NULL );
|
||||
mbedtls_mpi_write_file( "E: ", &E, 16, NULL );
|
||||
MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "N: ", &N, 16, NULL ) );
|
||||
MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "E: ", &E, 16, NULL ) );
|
||||
}
|
||||
else
|
||||
#endif
|
||||
@@ -269,26 +273,28 @@ int main( int argc, char *argv[] )
|
||||
if( mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_ECKEY )
|
||||
{
|
||||
mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( pk );
|
||||
mbedtls_mpi_write_file( "Q(X): ", &ecp->Q.X, 16, NULL );
|
||||
mbedtls_mpi_write_file( "Q(Y): ", &ecp->Q.Y, 16, NULL );
|
||||
mbedtls_mpi_write_file( "Q(Z): ", &ecp->Q.Z, 16, NULL );
|
||||
MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q(X): ", &ecp->Q.X, 16, NULL ) );
|
||||
MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q(Y): ", &ecp->Q.Y, 16, NULL ) );
|
||||
MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q(Z): ", &ecp->Q.Z, 16, NULL ) );
|
||||
}
|
||||
else
|
||||
#endif
|
||||
{
|
||||
mbedtls_printf("Do not know how to print key information for this type\n" );
|
||||
goto exit;
|
||||
goto cleanup;
|
||||
}
|
||||
}
|
||||
else
|
||||
goto usage;
|
||||
|
||||
exit:
|
||||
exit_code = MBEDTLS_EXIT_SUCCESS;
|
||||
|
||||
cleanup:
|
||||
|
||||
#if defined(MBEDTLS_ERROR_C)
|
||||
if( ret != 0 )
|
||||
if( exit_code != MBEDTLS_EXIT_SUCCESS )
|
||||
{
|
||||
mbedtls_strerror( ret, buf, sizeof(buf) );
|
||||
mbedtls_strerror( ret, buf, sizeof( buf ) );
|
||||
mbedtls_printf( " ! Last error was: %s\n", buf );
|
||||
}
|
||||
#endif
|
||||
@@ -303,6 +309,6 @@ exit:
|
||||
fflush( stdout ); getchar();
|
||||
#endif
|
||||
|
||||
return( ret );
|
||||
return( exit_code );
|
||||
}
|
||||
#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_PK_PARSE_C && MBEDTLS_FS_IO */
|
||||
|
||||
@@ -29,8 +29,11 @@
|
||||
#include "mbedtls/platform.h"
|
||||
#else
|
||||
#include <stdio.h>
|
||||
#define mbedtls_printf printf
|
||||
#endif
|
||||
#include <stdlib.h>
|
||||
#define mbedtls_printf printf
|
||||
#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
|
||||
#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
|
||||
#endif /* MBEDTLS_PLATFORM_C */
|
||||
|
||||
#if defined(MBEDTLS_PK_WRITE_C) && defined(MBEDTLS_FS_IO)
|
||||
#include "mbedtls/error.h"
|
||||
@@ -128,7 +131,7 @@ static int write_public_key( mbedtls_pk_context *key, const char *output_file )
|
||||
return( ret );
|
||||
|
||||
len = ret;
|
||||
c = output_buf + sizeof(output_buf) - len - 1;
|
||||
c = output_buf + sizeof(output_buf) - len;
|
||||
}
|
||||
|
||||
if( ( f = fopen( output_file, "w" ) ) == NULL )
|
||||
@@ -189,7 +192,8 @@ static int write_private_key( mbedtls_pk_context *key, const char *output_file )
|
||||
|
||||
int main( int argc, char *argv[] )
|
||||
{
|
||||
int ret = 0;
|
||||
int ret = 1;
|
||||
int exit_code = MBEDTLS_EXIT_FAILURE;
|
||||
char buf[1024];
|
||||
int i;
|
||||
char *p, *q;
|
||||
@@ -210,7 +214,6 @@ int main( int argc, char *argv[] )
|
||||
if( argc == 0 )
|
||||
{
|
||||
usage:
|
||||
ret = 1;
|
||||
mbedtls_printf( USAGE );
|
||||
goto exit;
|
||||
}
|
||||
@@ -403,9 +406,11 @@ int main( int argc, char *argv[] )
|
||||
write_private_key( &key, opt.output_file );
|
||||
}
|
||||
|
||||
exit_code = MBEDTLS_EXIT_SUCCESS;
|
||||
|
||||
exit:
|
||||
|
||||
if( ret != 0 && ret != 1)
|
||||
if( exit_code != MBEDTLS_EXIT_SUCCESS )
|
||||
{
|
||||
#ifdef MBEDTLS_ERROR_C
|
||||
mbedtls_strerror( ret, buf, sizeof( buf ) );
|
||||
@@ -426,6 +431,6 @@ exit:
|
||||
fflush( stdout ); getchar();
|
||||
#endif
|
||||
|
||||
return( ret );
|
||||
return( exit_code );
|
||||
}
|
||||
#endif /* MBEDTLS_PK_WRITE_C && MBEDTLS_FS_IO */
|
||||
|
||||
@@ -29,8 +29,11 @@
|
||||
#include "mbedtls/platform.h"
|
||||
#else
|
||||
#include <stdio.h>
|
||||
#define mbedtls_printf printf
|
||||
#endif
|
||||
#include <stdlib.h>
|
||||
#define mbedtls_printf printf
|
||||
#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
|
||||
#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
|
||||
#endif /* MBEDTLS_PLATFORM_C */
|
||||
|
||||
#if defined(MBEDTLS_BIGNUM_C) && defined(MBEDTLS_FS_IO)
|
||||
#include "mbedtls/bignum.h"
|
||||
@@ -47,7 +50,8 @@ int main( void )
|
||||
#else
|
||||
int main( void )
|
||||
{
|
||||
int ret;
|
||||
int ret = 1;
|
||||
int exit_code = MBEDTLS_EXIT_FAILURE;
|
||||
mbedtls_mpi E, P, Q, N, H, D, X, Y, Z;
|
||||
|
||||
mbedtls_mpi_init( &E ); mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &N );
|
||||
@@ -88,15 +92,16 @@ int main( void )
|
||||
MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( " Z (decrypted) = Y^D mod N = ", &Z, 10, NULL ) );
|
||||
mbedtls_printf( "\n" );
|
||||
|
||||
exit_code = MBEDTLS_EXIT_SUCCESS;
|
||||
|
||||
cleanup:
|
||||
mbedtls_mpi_free( &E ); mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &N );
|
||||
mbedtls_mpi_free( &H ); mbedtls_mpi_free( &D ); mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
|
||||
mbedtls_mpi_free( &Z );
|
||||
|
||||
if( ret != 0 )
|
||||
if( exit_code != MBEDTLS_EXIT_SUCCESS )
|
||||
{
|
||||
mbedtls_printf( "\nAn error occurred.\n" );
|
||||
ret = 1;
|
||||
}
|
||||
|
||||
#if defined(_WIN32)
|
||||
@@ -104,6 +109,6 @@ cleanup:
|
||||
fflush( stdout ); getchar();
|
||||
#endif
|
||||
|
||||
return( ret );
|
||||
return( exit_code );
|
||||
}
|
||||
#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_FS_IO */
|
||||
|
||||
@@ -29,8 +29,11 @@
|
||||
#include "mbedtls/platform.h"
|
||||
#else
|
||||
#include <stdio.h>
|
||||
#define mbedtls_printf printf
|
||||
#endif
|
||||
#include <stdlib.h>
|
||||
#define mbedtls_printf printf
|
||||
#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
|
||||
#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
|
||||
#endif /* MBEDTLS_PLATFORM_C */
|
||||
|
||||
#if defined(MBEDTLS_BIGNUM_C) && defined(MBEDTLS_PK_PARSE_C) && \
|
||||
defined(MBEDTLS_FS_IO) && defined(MBEDTLS_ENTROPY_C) && \
|
||||
@@ -59,7 +62,8 @@ int main( void )
|
||||
int main( int argc, char *argv[] )
|
||||
{
|
||||
FILE *f;
|
||||
int ret, c;
|
||||
int ret = 1, c;
|
||||
int exit_code = MBEDTLS_EXIT_FAILURE;
|
||||
size_t i, olen = 0;
|
||||
mbedtls_pk_context pk;
|
||||
mbedtls_entropy_context entropy;
|
||||
@@ -69,9 +73,11 @@ int main( int argc, char *argv[] )
|
||||
const char *pers = "mbedtls_pk_decrypt";
|
||||
((void) argv);
|
||||
|
||||
mbedtls_pk_init( &pk );
|
||||
mbedtls_entropy_init( &entropy );
|
||||
mbedtls_ctr_drbg_init( &ctr_drbg );
|
||||
|
||||
memset(result, 0, sizeof( result ) );
|
||||
ret = 1;
|
||||
|
||||
if( argc != 2 )
|
||||
{
|
||||
@@ -87,20 +93,18 @@ int main( int argc, char *argv[] )
|
||||
mbedtls_printf( "\n . Seeding the random number generator..." );
|
||||
fflush( stdout );
|
||||
|
||||
mbedtls_entropy_init( &entropy );
|
||||
if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
|
||||
(const unsigned char *) pers,
|
||||
strlen( pers ) ) ) != 0 )
|
||||
if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
|
||||
&entropy, (const unsigned char *) pers,
|
||||
strlen( pers ) ) ) != 0 )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret );
|
||||
mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned -0x%04x\n",
|
||||
-ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
mbedtls_printf( "\n . Reading private key from '%s'", argv[1] );
|
||||
fflush( stdout );
|
||||
|
||||
mbedtls_pk_init( &pk );
|
||||
|
||||
if( ( ret = mbedtls_pk_parse_keyfile( &pk, argv[1], "" ) ) != 0 )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile returned -0x%04x\n", -ret );
|
||||
@@ -110,19 +114,19 @@ int main( int argc, char *argv[] )
|
||||
/*
|
||||
* Extract the RSA encrypted value from the text file
|
||||
*/
|
||||
ret = 1;
|
||||
|
||||
if( ( f = fopen( "result-enc.txt", "rb" ) ) == NULL )
|
||||
{
|
||||
mbedtls_printf( "\n ! Could not open %s\n\n", "result-enc.txt" );
|
||||
ret = 1;
|
||||
goto exit;
|
||||
}
|
||||
|
||||
i = 0;
|
||||
|
||||
while( fscanf( f, "%02X", &c ) > 0 &&
|
||||
i < (int) sizeof( buf ) )
|
||||
{
|
||||
buf[i++] = (unsigned char) c;
|
||||
}
|
||||
|
||||
fclose( f );
|
||||
|
||||
@@ -135,7 +139,8 @@ int main( int argc, char *argv[] )
|
||||
if( ( ret = mbedtls_pk_decrypt( &pk, buf, i, result, &olen, sizeof(result),
|
||||
mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! mbedtls_pk_decrypt returned -0x%04x\n", -ret );
|
||||
mbedtls_printf( " failed\n ! mbedtls_pk_decrypt returned -0x%04x\n",
|
||||
-ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
@@ -143,16 +148,18 @@ int main( int argc, char *argv[] )
|
||||
|
||||
mbedtls_printf( "The decrypted result is: '%s'\n\n", result );
|
||||
|
||||
ret = 0;
|
||||
exit_code = MBEDTLS_EXIT_SUCCESS;
|
||||
|
||||
exit:
|
||||
mbedtls_ctr_drbg_free( &ctr_drbg );
|
||||
|
||||
mbedtls_pk_free( &pk );
|
||||
mbedtls_entropy_free( &entropy );
|
||||
mbedtls_ctr_drbg_free( &ctr_drbg );
|
||||
|
||||
#if defined(MBEDTLS_ERROR_C)
|
||||
if( ret != 0 )
|
||||
if( exit_code != MBEDTLS_EXIT_SUCCESS )
|
||||
{
|
||||
mbedtls_strerror( ret, (char *) buf, sizeof(buf) );
|
||||
mbedtls_strerror( ret, (char *) buf, sizeof( buf ) );
|
||||
mbedtls_printf( " ! Last error was: %s\n", buf );
|
||||
}
|
||||
#endif
|
||||
@@ -162,7 +169,7 @@ exit:
|
||||
fflush( stdout ); getchar();
|
||||
#endif
|
||||
|
||||
return( ret );
|
||||
return( exit_code );
|
||||
}
|
||||
#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_PK_PARSE_C && MBEDTLS_FS_IO &&
|
||||
MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C */
|
||||
|
||||
@@ -29,9 +29,12 @@
|
||||
#include "mbedtls/platform.h"
|
||||
#else
|
||||
#include <stdio.h>
|
||||
#define mbedtls_fprintf fprintf
|
||||
#define mbedtls_printf printf
|
||||
#endif
|
||||
#include <stdlib.h>
|
||||
#define mbedtls_fprintf fprintf
|
||||
#define mbedtls_printf printf
|
||||
#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
|
||||
#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
|
||||
#endif /* MBEDTLS_PLATFORM_C */
|
||||
|
||||
#if defined(MBEDTLS_BIGNUM_C) && defined(MBEDTLS_PK_PARSE_C) && \
|
||||
defined(MBEDTLS_ENTROPY_C) && defined(MBEDTLS_FS_IO) && \
|
||||
@@ -59,7 +62,8 @@ int main( void )
|
||||
int main( int argc, char *argv[] )
|
||||
{
|
||||
FILE *f;
|
||||
int ret;
|
||||
int ret = 1;
|
||||
int exit_code = MBEDTLS_EXIT_FAILURE;
|
||||
size_t i, olen = 0;
|
||||
mbedtls_pk_context pk;
|
||||
mbedtls_entropy_context entropy;
|
||||
@@ -68,8 +72,9 @@ int main( int argc, char *argv[] )
|
||||
unsigned char buf[512];
|
||||
const char *pers = "mbedtls_pk_encrypt";
|
||||
|
||||
ret = 1;
|
||||
mbedtls_ctr_drbg_init( &ctr_drbg );
|
||||
mbedtls_entropy_init( &entropy );
|
||||
mbedtls_pk_init( &pk );
|
||||
|
||||
if( argc != 3 )
|
||||
{
|
||||
@@ -85,20 +90,18 @@ int main( int argc, char *argv[] )
|
||||
mbedtls_printf( "\n . Seeding the random number generator..." );
|
||||
fflush( stdout );
|
||||
|
||||
mbedtls_entropy_init( &entropy );
|
||||
if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
|
||||
(const unsigned char *) pers,
|
||||
strlen( pers ) ) ) != 0 )
|
||||
if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
|
||||
&entropy, (const unsigned char *) pers,
|
||||
strlen( pers ) ) ) != 0 )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned -0x%04x\n", -ret );
|
||||
mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned -0x%04x\n",
|
||||
-ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
mbedtls_printf( "\n . Reading public key from '%s'", argv[1] );
|
||||
fflush( stdout );
|
||||
|
||||
mbedtls_pk_init( &pk );
|
||||
|
||||
if( ( ret = mbedtls_pk_parse_public_keyfile( &pk, argv[1] ) ) != 0 )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! mbedtls_pk_parse_public_keyfile returned -0x%04x\n", -ret );
|
||||
@@ -123,7 +126,8 @@ int main( int argc, char *argv[] )
|
||||
buf, &olen, sizeof(buf),
|
||||
mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! mbedtls_pk_encrypt returned -0x%04x\n", -ret );
|
||||
mbedtls_printf( " failed\n ! mbedtls_pk_encrypt returned -0x%04x\n",
|
||||
-ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
@@ -132,27 +136,34 @@ int main( int argc, char *argv[] )
|
||||
*/
|
||||
if( ( f = fopen( "result-enc.txt", "wb+" ) ) == NULL )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! Could not create %s\n\n",
|
||||
"result-enc.txt" );
|
||||
ret = 1;
|
||||
mbedtls_printf( " failed\n ! Could not create %s\n\n", "result-enc.txt" );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
for( i = 0; i < olen; i++ )
|
||||
{
|
||||
mbedtls_fprintf( f, "%02X%s", buf[i],
|
||||
( i + 1 ) % 16 == 0 ? "\r\n" : " " );
|
||||
}
|
||||
|
||||
fclose( f );
|
||||
|
||||
mbedtls_printf( "\n . Done (created \"%s\")\n\n", "result-enc.txt" );
|
||||
|
||||
exit_code = MBEDTLS_EXIT_SUCCESS;
|
||||
|
||||
exit:
|
||||
mbedtls_ctr_drbg_free( &ctr_drbg );
|
||||
|
||||
mbedtls_pk_free( &pk );
|
||||
mbedtls_entropy_free( &entropy );
|
||||
mbedtls_ctr_drbg_free( &ctr_drbg );
|
||||
|
||||
#if defined(MBEDTLS_ERROR_C)
|
||||
if( ret != 0 )
|
||||
if( exit_code != MBEDTLS_EXIT_SUCCESS )
|
||||
{
|
||||
mbedtls_strerror( ret, (char *) buf, sizeof(buf) );
|
||||
mbedtls_strerror( ret, (char *) buf, sizeof( buf ) );
|
||||
mbedtls_printf( " ! Last error was: %s\n", buf );
|
||||
}
|
||||
#endif
|
||||
@@ -162,7 +173,7 @@ exit:
|
||||
fflush( stdout ); getchar();
|
||||
#endif
|
||||
|
||||
return( ret );
|
||||
return( exit_code );
|
||||
}
|
||||
#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_PK_PARSE_C && MBEDTLS_ENTROPY_C &&
|
||||
MBEDTLS_FS_IO && MBEDTLS_CTR_DRBG_C */
|
||||
|
||||
@@ -30,9 +30,11 @@
|
||||
#else
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#define mbedtls_snprintf snprintf
|
||||
#define mbedtls_printf printf
|
||||
#endif
|
||||
#define mbedtls_snprintf snprintf
|
||||
#define mbedtls_printf printf
|
||||
#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
|
||||
#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
|
||||
#endif /* MBEDTLS_PLATFORM_C */
|
||||
|
||||
#if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_ENTROPY_C) || \
|
||||
!defined(MBEDTLS_SHA256_C) || !defined(MBEDTLS_MD_C) || \
|
||||
@@ -61,6 +63,7 @@ int main( int argc, char *argv[] )
|
||||
{
|
||||
FILE *f;
|
||||
int ret = 1;
|
||||
int exit_code = MBEDTLS_EXIT_FAILURE;
|
||||
mbedtls_pk_context pk;
|
||||
mbedtls_entropy_context entropy;
|
||||
mbedtls_ctr_drbg_context ctr_drbg;
|
||||
@@ -134,14 +137,12 @@ int main( int argc, char *argv[] )
|
||||
|
||||
if( ( f = fopen( filename, "wb+" ) ) == NULL )
|
||||
{
|
||||
ret = 1;
|
||||
mbedtls_printf( " failed\n ! Could not create %s\n\n", filename );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if( fwrite( buf, 1, olen, f ) != olen )
|
||||
{
|
||||
ret = 1;
|
||||
mbedtls_printf( "failed\n ! fwrite failed\n\n" );
|
||||
fclose( f );
|
||||
goto exit;
|
||||
@@ -151,13 +152,15 @@ int main( int argc, char *argv[] )
|
||||
|
||||
mbedtls_printf( "\n . Done (created \"%s\")\n\n", filename );
|
||||
|
||||
exit_code = MBEDTLS_EXIT_SUCCESS;
|
||||
|
||||
exit:
|
||||
mbedtls_pk_free( &pk );
|
||||
mbedtls_ctr_drbg_free( &ctr_drbg );
|
||||
mbedtls_entropy_free( &entropy );
|
||||
|
||||
#if defined(MBEDTLS_ERROR_C)
|
||||
if( ret != 0 )
|
||||
if( exit_code != MBEDTLS_EXIT_SUCCESS )
|
||||
{
|
||||
mbedtls_strerror( ret, (char *) buf, sizeof(buf) );
|
||||
mbedtls_printf( " ! Last error was: %s\n", buf );
|
||||
@@ -169,7 +172,7 @@ exit:
|
||||
fflush( stdout ); getchar();
|
||||
#endif
|
||||
|
||||
return( ret ? EXIT_FAILURE : EXIT_SUCCESS );
|
||||
return( exit_code );
|
||||
}
|
||||
#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C &&
|
||||
MBEDTLS_SHA256_C && MBEDTLS_PK_PARSE_C && MBEDTLS_FS_IO &&
|
||||
|
||||
@@ -29,9 +29,12 @@
|
||||
#include "mbedtls/platform.h"
|
||||
#else
|
||||
#include <stdio.h>
|
||||
#define mbedtls_snprintf snprintf
|
||||
#define mbedtls_printf printf
|
||||
#endif
|
||||
#include <stdlib.h>
|
||||
#define mbedtls_snprintf snprintf
|
||||
#define mbedtls_printf printf
|
||||
#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
|
||||
#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
|
||||
#endif /* MBEDTLS_PLATFORM_C */
|
||||
|
||||
#if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_MD_C) || \
|
||||
!defined(MBEDTLS_SHA256_C) || !defined(MBEDTLS_PK_PARSE_C) || \
|
||||
@@ -56,6 +59,7 @@ int main( int argc, char *argv[] )
|
||||
{
|
||||
FILE *f;
|
||||
int ret = 1;
|
||||
int exit_code = MBEDTLS_EXIT_FAILURE;
|
||||
size_t i;
|
||||
mbedtls_pk_context pk;
|
||||
unsigned char hash[32];
|
||||
@@ -87,7 +91,6 @@ int main( int argc, char *argv[] )
|
||||
/*
|
||||
* Extract the signature from the file
|
||||
*/
|
||||
ret = 1;
|
||||
mbedtls_snprintf( filename, sizeof(filename), "%s.sig", argv[2] );
|
||||
|
||||
if( ( f = fopen( filename, "rb" ) ) == NULL )
|
||||
@@ -125,13 +128,13 @@ int main( int argc, char *argv[] )
|
||||
|
||||
mbedtls_printf( "\n . OK (the signature is valid)\n\n" );
|
||||
|
||||
ret = 0;
|
||||
exit_code = MBEDTLS_EXIT_SUCCESS;
|
||||
|
||||
exit:
|
||||
mbedtls_pk_free( &pk );
|
||||
|
||||
#if defined(MBEDTLS_ERROR_C)
|
||||
if( ret != 0 )
|
||||
if( exit_code != MBEDTLS_EXIT_SUCCESS )
|
||||
{
|
||||
mbedtls_strerror( ret, (char *) buf, sizeof(buf) );
|
||||
mbedtls_printf( " ! Last error was: %s\n", buf );
|
||||
@@ -143,7 +146,7 @@ exit:
|
||||
fflush( stdout ); getchar();
|
||||
#endif
|
||||
|
||||
return( ret );
|
||||
return( exit_code );
|
||||
}
|
||||
#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_SHA256_C &&
|
||||
MBEDTLS_PK_PARSE_C && MBEDTLS_FS_IO */
|
||||
|
||||
@@ -30,11 +30,11 @@
|
||||
#else
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#define mbedtls_printf printf
|
||||
#define mbedtls_exit exit
|
||||
#define mbedtls_printf printf
|
||||
#define mbedtls_exit exit
|
||||
#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
|
||||
#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
|
||||
#endif
|
||||
#endif /* MBEDTLS_PLATFORM_C */
|
||||
|
||||
#if defined(MBEDTLS_BIGNUM_C) && defined(MBEDTLS_RSA_C) && \
|
||||
defined(MBEDTLS_FS_IO) && defined(MBEDTLS_ENTROPY_C) && \
|
||||
@@ -61,7 +61,9 @@ int main( void )
|
||||
int main( int argc, char *argv[] )
|
||||
{
|
||||
FILE *f;
|
||||
int return_val, exit_val, c;
|
||||
int ret = 1;
|
||||
int exit_code = MBEDTLS_EXIT_FAILURE;
|
||||
int c;
|
||||
size_t i;
|
||||
mbedtls_rsa_context rsa;
|
||||
mbedtls_mpi N, P, Q, D, E, DP, DQ, QP;
|
||||
@@ -73,7 +75,6 @@ int main( int argc, char *argv[] )
|
||||
((void) argv);
|
||||
|
||||
memset(result, 0, sizeof( result ) );
|
||||
exit_val = MBEDTLS_EXIT_SUCCESS;
|
||||
|
||||
if( argc != 1 )
|
||||
{
|
||||
@@ -83,7 +84,7 @@ int main( int argc, char *argv[] )
|
||||
mbedtls_printf( "\n" );
|
||||
#endif
|
||||
|
||||
mbedtls_exit( MBEDTLS_EXIT_FAILURE );
|
||||
mbedtls_exit( exit_code );
|
||||
}
|
||||
|
||||
mbedtls_printf( "\n . Seeding the random number generator..." );
|
||||
@@ -96,14 +97,13 @@ int main( int argc, char *argv[] )
|
||||
mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &DP );
|
||||
mbedtls_mpi_init( &DQ ); mbedtls_mpi_init( &QP );
|
||||
|
||||
return_val = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
|
||||
ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
|
||||
&entropy, (const unsigned char *) pers,
|
||||
strlen( pers ) );
|
||||
if( return_val != 0 )
|
||||
if( ret != 0 )
|
||||
{
|
||||
exit_val = MBEDTLS_EXIT_FAILURE;
|
||||
mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d\n",
|
||||
return_val );
|
||||
ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
@@ -112,40 +112,38 @@ int main( int argc, char *argv[] )
|
||||
|
||||
if( ( f = fopen( "rsa_priv.txt", "rb" ) ) == NULL )
|
||||
{
|
||||
exit_val = MBEDTLS_EXIT_FAILURE;
|
||||
mbedtls_printf( " failed\n ! Could not open rsa_priv.txt\n" \
|
||||
" ! Please run rsa_genkey first\n\n" );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if( ( return_val = mbedtls_mpi_read_file( &N , 16, f ) ) != 0 ||
|
||||
( return_val = mbedtls_mpi_read_file( &E , 16, f ) ) != 0 ||
|
||||
( return_val = mbedtls_mpi_read_file( &D , 16, f ) ) != 0 ||
|
||||
( return_val = mbedtls_mpi_read_file( &P , 16, f ) ) != 0 ||
|
||||
( return_val = mbedtls_mpi_read_file( &Q , 16, f ) ) != 0 ||
|
||||
( return_val = mbedtls_mpi_read_file( &DP , 16, f ) ) != 0 ||
|
||||
( return_val = mbedtls_mpi_read_file( &DQ , 16, f ) ) != 0 ||
|
||||
( return_val = mbedtls_mpi_read_file( &QP , 16, f ) ) != 0 )
|
||||
if( ( ret = mbedtls_mpi_read_file( &N , 16, f ) ) != 0 ||
|
||||
( ret = mbedtls_mpi_read_file( &E , 16, f ) ) != 0 ||
|
||||
( ret = mbedtls_mpi_read_file( &D , 16, f ) ) != 0 ||
|
||||
( ret = mbedtls_mpi_read_file( &P , 16, f ) ) != 0 ||
|
||||
( ret = mbedtls_mpi_read_file( &Q , 16, f ) ) != 0 ||
|
||||
( ret = mbedtls_mpi_read_file( &DP , 16, f ) ) != 0 ||
|
||||
( ret = mbedtls_mpi_read_file( &DQ , 16, f ) ) != 0 ||
|
||||
( ret = mbedtls_mpi_read_file( &QP , 16, f ) ) != 0 )
|
||||
{
|
||||
exit_val = MBEDTLS_EXIT_FAILURE;
|
||||
mbedtls_printf( " failed\n ! mbedtls_mpi_read_file returned %d\n\n",
|
||||
return_val );
|
||||
ret );
|
||||
fclose( f );
|
||||
goto exit;
|
||||
}
|
||||
fclose( f );
|
||||
|
||||
if( ( return_val = mbedtls_rsa_import( &rsa, &N, &P, &Q, &D, &E ) ) != 0 )
|
||||
if( ( ret = mbedtls_rsa_import( &rsa, &N, &P, &Q, &D, &E ) ) != 0 )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! mbedtls_rsa_import returned %d\n\n",
|
||||
return_val );
|
||||
ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if( ( return_val = mbedtls_rsa_complete( &rsa ) ) != 0 )
|
||||
if( ( ret = mbedtls_rsa_complete( &rsa ) ) != 0 )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! mbedtls_rsa_complete returned %d\n\n",
|
||||
return_val );
|
||||
ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
@@ -154,7 +152,6 @@ int main( int argc, char *argv[] )
|
||||
*/
|
||||
if( ( f = fopen( "result-enc.txt", "rb" ) ) == NULL )
|
||||
{
|
||||
exit_val = MBEDTLS_EXIT_FAILURE;
|
||||
mbedtls_printf( "\n ! Could not open %s\n\n", "result-enc.txt" );
|
||||
goto exit;
|
||||
}
|
||||
@@ -169,7 +166,6 @@ int main( int argc, char *argv[] )
|
||||
|
||||
if( i != rsa.len )
|
||||
{
|
||||
exit_val = MBEDTLS_EXIT_FAILURE;
|
||||
mbedtls_printf( "\n ! Invalid RSA signature format\n\n" );
|
||||
goto exit;
|
||||
}
|
||||
@@ -180,14 +176,13 @@ int main( int argc, char *argv[] )
|
||||
mbedtls_printf( "\n . Decrypting the encrypted data" );
|
||||
fflush( stdout );
|
||||
|
||||
return_val = mbedtls_rsa_pkcs1_decrypt( &rsa, mbedtls_ctr_drbg_random,
|
||||
ret = mbedtls_rsa_pkcs1_decrypt( &rsa, mbedtls_ctr_drbg_random,
|
||||
&ctr_drbg, MBEDTLS_RSA_PRIVATE, &i,
|
||||
buf, result, 1024 );
|
||||
if( return_val != 0 )
|
||||
if( ret != 0 )
|
||||
{
|
||||
exit_val = MBEDTLS_EXIT_FAILURE;
|
||||
mbedtls_printf( " failed\n ! mbedtls_rsa_pkcs1_decrypt returned %d\n\n",
|
||||
return_val );
|
||||
ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
@@ -195,6 +190,8 @@ int main( int argc, char *argv[] )
|
||||
|
||||
mbedtls_printf( "The decrypted result is: '%s'\n\n", result );
|
||||
|
||||
exit_code = MBEDTLS_EXIT_SUCCESS;
|
||||
|
||||
exit:
|
||||
mbedtls_ctr_drbg_free( &ctr_drbg );
|
||||
mbedtls_entropy_free( &entropy );
|
||||
@@ -208,6 +205,6 @@ exit:
|
||||
fflush( stdout ); getchar();
|
||||
#endif
|
||||
|
||||
return( exit_val );
|
||||
return( exit_code );
|
||||
}
|
||||
#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_RSA_C && MBEDTLS_FS_IO */
|
||||
|
||||
@@ -30,12 +30,12 @@
|
||||
#else
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#define mbedtls_fprintf fprintf
|
||||
#define mbedtls_printf printf
|
||||
#define mbedtls_exit exit
|
||||
#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
|
||||
#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
|
||||
#endif
|
||||
#define mbedtls_fprintf fprintf
|
||||
#define mbedtls_printf printf
|
||||
#define mbedtls_exit exit
|
||||
#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
|
||||
#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
|
||||
#endif /* MBEDTLS_PLATFORM_C */
|
||||
|
||||
#if defined(MBEDTLS_BIGNUM_C) && defined(MBEDTLS_RSA_C) && \
|
||||
defined(MBEDTLS_ENTROPY_C) && defined(MBEDTLS_FS_IO) && \
|
||||
@@ -61,7 +61,8 @@ int main( void )
|
||||
int main( int argc, char *argv[] )
|
||||
{
|
||||
FILE *f;
|
||||
int return_val, exit_val;
|
||||
int ret = 1;
|
||||
int exit_code = MBEDTLS_EXIT_FAILURE;
|
||||
size_t i;
|
||||
mbedtls_rsa_context rsa;
|
||||
mbedtls_entropy_context entropy;
|
||||
@@ -71,8 +72,6 @@ int main( int argc, char *argv[] )
|
||||
const char *pers = "rsa_encrypt";
|
||||
mbedtls_mpi N, E;
|
||||
|
||||
exit_val = MBEDTLS_EXIT_SUCCESS;
|
||||
|
||||
if( argc != 2 )
|
||||
{
|
||||
mbedtls_printf( "usage: rsa_encrypt <string of max 100 characters>\n" );
|
||||
@@ -81,7 +80,7 @@ int main( int argc, char *argv[] )
|
||||
mbedtls_printf( "\n" );
|
||||
#endif
|
||||
|
||||
mbedtls_exit( MBEDTLS_EXIT_FAILURE );
|
||||
mbedtls_exit( exit_code );
|
||||
}
|
||||
|
||||
mbedtls_printf( "\n . Seeding the random number generator..." );
|
||||
@@ -92,14 +91,13 @@ int main( int argc, char *argv[] )
|
||||
mbedtls_ctr_drbg_init( &ctr_drbg );
|
||||
mbedtls_entropy_init( &entropy );
|
||||
|
||||
return_val = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
|
||||
&entropy, (const unsigned char *) pers,
|
||||
strlen( pers ) );
|
||||
if( return_val != 0 )
|
||||
ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
|
||||
&entropy, (const unsigned char *) pers,
|
||||
strlen( pers ) );
|
||||
if( ret != 0 )
|
||||
{
|
||||
exit_val = MBEDTLS_EXIT_FAILURE;
|
||||
mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d\n",
|
||||
return_val );
|
||||
ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
@@ -108,35 +106,30 @@ int main( int argc, char *argv[] )
|
||||
|
||||
if( ( f = fopen( "rsa_pub.txt", "rb" ) ) == NULL )
|
||||
{
|
||||
exit_val = MBEDTLS_EXIT_FAILURE;
|
||||
mbedtls_printf( " failed\n ! Could not open rsa_pub.txt\n" \
|
||||
" ! Please run rsa_genkey first\n\n" );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if( ( return_val = mbedtls_mpi_read_file( &N, 16, f ) ) != 0 ||
|
||||
( return_val = mbedtls_mpi_read_file( &E, 16, f ) ) != 0 )
|
||||
if( ( ret = mbedtls_mpi_read_file( &N, 16, f ) ) != 0 ||
|
||||
( ret = mbedtls_mpi_read_file( &E, 16, f ) ) != 0 )
|
||||
{
|
||||
exit_val = MBEDTLS_EXIT_FAILURE;
|
||||
mbedtls_printf( " failed\n ! mbedtls_mpi_read_file returned %d\n\n",
|
||||
return_val );
|
||||
ret );
|
||||
fclose( f );
|
||||
goto exit;
|
||||
}
|
||||
fclose( f );
|
||||
|
||||
if( ( return_val = mbedtls_rsa_import( &rsa, &N, NULL,
|
||||
NULL, NULL, &E ) ) != 0 )
|
||||
if( ( ret = mbedtls_rsa_import( &rsa, &N, NULL, NULL, NULL, &E ) ) != 0 )
|
||||
{
|
||||
exit_val = MBEDTLS_EXIT_FAILURE;
|
||||
mbedtls_printf( " failed\n ! mbedtls_rsa_import returned %d\n\n",
|
||||
return_val );
|
||||
ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if( strlen( argv[1] ) > 100 )
|
||||
{
|
||||
exit_val = MBEDTLS_EXIT_FAILURE;
|
||||
mbedtls_printf( " Input data larger than 100 characters.\n\n" );
|
||||
goto exit;
|
||||
}
|
||||
@@ -149,14 +142,13 @@ int main( int argc, char *argv[] )
|
||||
mbedtls_printf( "\n . Generating the RSA encrypted value" );
|
||||
fflush( stdout );
|
||||
|
||||
return_val = mbedtls_rsa_pkcs1_encrypt( &rsa, mbedtls_ctr_drbg_random,
|
||||
&ctr_drbg, MBEDTLS_RSA_PUBLIC,
|
||||
strlen( argv[1] ), input, buf );
|
||||
if( return_val != 0 )
|
||||
ret = mbedtls_rsa_pkcs1_encrypt( &rsa, mbedtls_ctr_drbg_random,
|
||||
&ctr_drbg, MBEDTLS_RSA_PUBLIC,
|
||||
strlen( argv[1] ), input, buf );
|
||||
if( ret != 0 )
|
||||
{
|
||||
exit_val = MBEDTLS_EXIT_FAILURE;
|
||||
mbedtls_printf( " failed\n ! mbedtls_rsa_pkcs1_encrypt returned %d\n\n",
|
||||
return_val );
|
||||
ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
@@ -165,7 +157,6 @@ int main( int argc, char *argv[] )
|
||||
*/
|
||||
if( ( f = fopen( "result-enc.txt", "wb+" ) ) == NULL )
|
||||
{
|
||||
exit_val = MBEDTLS_EXIT_FAILURE;
|
||||
mbedtls_printf( " failed\n ! Could not create %s\n\n", "result-enc.txt" );
|
||||
goto exit;
|
||||
}
|
||||
@@ -178,6 +169,8 @@ int main( int argc, char *argv[] )
|
||||
|
||||
mbedtls_printf( "\n . Done (created \"%s\")\n\n", "result-enc.txt" );
|
||||
|
||||
exit_code = MBEDTLS_EXIT_SUCCESS;
|
||||
|
||||
exit:
|
||||
mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
|
||||
mbedtls_ctr_drbg_free( &ctr_drbg );
|
||||
@@ -189,7 +182,7 @@ exit:
|
||||
fflush( stdout ); getchar();
|
||||
#endif
|
||||
|
||||
return( exit_val );
|
||||
return( exit_code );
|
||||
}
|
||||
#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_RSA_C && MBEDTLS_ENTROPY_C &&
|
||||
MBEDTLS_FS_IO && MBEDTLS_CTR_DRBG_C */
|
||||
|
||||
@@ -29,8 +29,11 @@
|
||||
#include "mbedtls/platform.h"
|
||||
#else
|
||||
#include <stdio.h>
|
||||
#define mbedtls_printf printf
|
||||
#endif
|
||||
#include <stdlib.h>
|
||||
#define mbedtls_printf printf
|
||||
#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
|
||||
#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
|
||||
#endif /* MBEDTLS_PLATFORM_C */
|
||||
|
||||
#if defined(MBEDTLS_BIGNUM_C) && defined(MBEDTLS_ENTROPY_C) && \
|
||||
defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME) && \
|
||||
@@ -61,7 +64,8 @@ int main( void )
|
||||
#else
|
||||
int main( void )
|
||||
{
|
||||
int ret;
|
||||
int ret = 1;
|
||||
int exit_code = MBEDTLS_EXIT_FAILURE;
|
||||
mbedtls_rsa_context rsa;
|
||||
mbedtls_entropy_context entropy;
|
||||
mbedtls_ctr_drbg_context ctr_drbg;
|
||||
@@ -105,14 +109,12 @@ int main( void )
|
||||
( ret = mbedtls_rsa_export_crt( &rsa, &DP, &DQ, &QP ) ) != 0 )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! could not export RSA parameters\n\n" );
|
||||
ret = 1;
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if( ( fpub = fopen( "rsa_pub.txt", "wb+" ) ) == NULL )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! could not open rsa_pub.txt for writing\n\n" );
|
||||
ret = 1;
|
||||
goto exit;
|
||||
}
|
||||
|
||||
@@ -129,7 +131,6 @@ int main( void )
|
||||
if( ( fpriv = fopen( "rsa_priv.txt", "wb+" ) ) == NULL )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! could not open rsa_priv.txt for writing\n" );
|
||||
ret = 1;
|
||||
goto exit;
|
||||
}
|
||||
|
||||
@@ -160,6 +161,8 @@ int main( void )
|
||||
*/
|
||||
mbedtls_printf( " ok\n\n" );
|
||||
|
||||
exit_code = MBEDTLS_EXIT_SUCCESS;
|
||||
|
||||
exit:
|
||||
|
||||
if( fpub != NULL )
|
||||
@@ -180,7 +183,7 @@ exit:
|
||||
fflush( stdout ); getchar();
|
||||
#endif
|
||||
|
||||
return( ret );
|
||||
return( exit_code );
|
||||
}
|
||||
#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_RSA_C &&
|
||||
MBEDTLS_GENPRIME && MBEDTLS_FS_IO && MBEDTLS_CTR_DRBG_C */
|
||||
|
||||
@@ -29,10 +29,13 @@
|
||||
#include "mbedtls/platform.h"
|
||||
#else
|
||||
#include <stdio.h>
|
||||
#define mbedtls_fprintf fprintf
|
||||
#define mbedtls_printf printf
|
||||
#define mbedtls_snprintf snprintf
|
||||
#endif
|
||||
#include <stdlib.h>
|
||||
#define mbedtls_fprintf fprintf
|
||||
#define mbedtls_printf printf
|
||||
#define mbedtls_snprintf snprintf
|
||||
#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
|
||||
#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
|
||||
#endif /* MBEDTLS_PLATFORM_C */
|
||||
|
||||
#if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_RSA_C) || \
|
||||
!defined(MBEDTLS_SHA256_C) || !defined(MBEDTLS_MD_C) || \
|
||||
@@ -55,7 +58,8 @@ int main( void )
|
||||
int main( int argc, char *argv[] )
|
||||
{
|
||||
FILE *f;
|
||||
int ret;
|
||||
int ret = 1;
|
||||
int exit_code = MBEDTLS_EXIT_FAILURE;
|
||||
size_t i;
|
||||
mbedtls_rsa_context rsa;
|
||||
unsigned char hash[32];
|
||||
@@ -69,8 +73,6 @@ int main( int argc, char *argv[] )
|
||||
mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &DP );
|
||||
mbedtls_mpi_init( &DQ ); mbedtls_mpi_init( &QP );
|
||||
|
||||
ret = 1;
|
||||
|
||||
if( argc != 2 )
|
||||
{
|
||||
mbedtls_printf( "usage: rsa_sign <filename>\n" );
|
||||
@@ -87,7 +89,6 @@ int main( int argc, char *argv[] )
|
||||
|
||||
if( ( f = fopen( "rsa_priv.txt", "rb" ) ) == NULL )
|
||||
{
|
||||
ret = 1;
|
||||
mbedtls_printf( " failed\n ! Could not open rsa_priv.txt\n" \
|
||||
" ! Please run rsa_genkey first\n\n" );
|
||||
goto exit;
|
||||
@@ -159,7 +160,6 @@ int main( int argc, char *argv[] )
|
||||
|
||||
if( ( f = fopen( filename, "wb+" ) ) == NULL )
|
||||
{
|
||||
ret = 1;
|
||||
mbedtls_printf( " failed\n ! Could not create %s\n\n", argv[1] );
|
||||
goto exit;
|
||||
}
|
||||
@@ -172,6 +172,8 @@ int main( int argc, char *argv[] )
|
||||
|
||||
mbedtls_printf( "\n . Done (created \"%s\")\n\n", filename );
|
||||
|
||||
exit_code = MBEDTLS_EXIT_SUCCESS;
|
||||
|
||||
exit:
|
||||
|
||||
mbedtls_rsa_free( &rsa );
|
||||
@@ -184,7 +186,7 @@ exit:
|
||||
fflush( stdout ); getchar();
|
||||
#endif
|
||||
|
||||
return( ret );
|
||||
return( exit_code );
|
||||
}
|
||||
#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_RSA_C && MBEDTLS_SHA256_C &&
|
||||
MBEDTLS_FS_IO */
|
||||
|
||||
@@ -29,9 +29,12 @@
|
||||
#include "mbedtls/platform.h"
|
||||
#else
|
||||
#include <stdio.h>
|
||||
#define mbedtls_snprintf snprintf
|
||||
#define mbedtls_printf printf
|
||||
#endif
|
||||
#include <stdlib.h>
|
||||
#define mbedtls_snprintf snprintf
|
||||
#define mbedtls_printf printf
|
||||
#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
|
||||
#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
|
||||
#endif /* MBEDTLS_PLATFORM_C */
|
||||
|
||||
#if !defined(MBEDTLS_MD_C) || !defined(MBEDTLS_ENTROPY_C) || \
|
||||
!defined(MBEDTLS_RSA_C) || !defined(MBEDTLS_SHA256_C) || \
|
||||
@@ -61,6 +64,7 @@ int main( int argc, char *argv[] )
|
||||
{
|
||||
FILE *f;
|
||||
int ret = 1;
|
||||
int exit_code = MBEDTLS_EXIT_FAILURE;
|
||||
mbedtls_pk_context pk;
|
||||
mbedtls_entropy_context entropy;
|
||||
mbedtls_ctr_drbg_context ctr_drbg;
|
||||
@@ -101,7 +105,6 @@ int main( int argc, char *argv[] )
|
||||
|
||||
if( ( ret = mbedtls_pk_parse_keyfile( &pk, argv[1], "" ) ) != 0 )
|
||||
{
|
||||
ret = 1;
|
||||
mbedtls_printf( " failed\n ! Could not read key from '%s'\n", argv[1] );
|
||||
mbedtls_printf( " ! mbedtls_pk_parse_public_keyfile returned %d\n\n", ret );
|
||||
goto exit;
|
||||
@@ -109,7 +112,6 @@ int main( int argc, char *argv[] )
|
||||
|
||||
if( !mbedtls_pk_can_do( &pk, MBEDTLS_PK_RSA ) )
|
||||
{
|
||||
ret = 1;
|
||||
mbedtls_printf( " failed\n ! Key is not an RSA key\n" );
|
||||
goto exit;
|
||||
}
|
||||
@@ -145,7 +147,6 @@ int main( int argc, char *argv[] )
|
||||
|
||||
if( ( f = fopen( filename, "wb+" ) ) == NULL )
|
||||
{
|
||||
ret = 1;
|
||||
mbedtls_printf( " failed\n ! Could not create %s\n\n", filename );
|
||||
goto exit;
|
||||
}
|
||||
@@ -161,6 +162,8 @@ int main( int argc, char *argv[] )
|
||||
|
||||
mbedtls_printf( "\n . Done (created \"%s\")\n\n", filename );
|
||||
|
||||
exit_code = MBEDTLS_EXIT_SUCCESS;
|
||||
|
||||
exit:
|
||||
mbedtls_pk_free( &pk );
|
||||
mbedtls_ctr_drbg_free( &ctr_drbg );
|
||||
@@ -171,7 +174,7 @@ exit:
|
||||
fflush( stdout ); getchar();
|
||||
#endif
|
||||
|
||||
return( ret );
|
||||
return( exit_code );
|
||||
}
|
||||
#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_RSA_C &&
|
||||
MBEDTLS_SHA256_C && MBEDTLS_PK_PARSE_C && MBEDTLS_FS_IO &&
|
||||
|
||||
@@ -29,9 +29,12 @@
|
||||
#include "mbedtls/platform.h"
|
||||
#else
|
||||
#include <stdio.h>
|
||||
#define mbedtls_printf printf
|
||||
#define mbedtls_snprintf snprintf
|
||||
#endif
|
||||
#include <stdlib.h>
|
||||
#define mbedtls_printf printf
|
||||
#define mbedtls_snprintf snprintf
|
||||
#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
|
||||
#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
|
||||
#endif /* MBEDTLS_PLATFORM_C */
|
||||
|
||||
#if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_RSA_C) || \
|
||||
!defined(MBEDTLS_SHA256_C) || !defined(MBEDTLS_MD_C) || \
|
||||
@@ -54,7 +57,8 @@ int main( void )
|
||||
int main( int argc, char *argv[] )
|
||||
{
|
||||
FILE *f;
|
||||
int ret, c;
|
||||
int ret = 1, c;
|
||||
int exit_code = MBEDTLS_EXIT_FAILURE;
|
||||
size_t i;
|
||||
mbedtls_rsa_context rsa;
|
||||
unsigned char hash[32];
|
||||
@@ -62,7 +66,6 @@ int main( int argc, char *argv[] )
|
||||
char filename[512];
|
||||
|
||||
mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
|
||||
ret = 1;
|
||||
|
||||
if( argc != 2 )
|
||||
{
|
||||
@@ -100,7 +103,6 @@ int main( int argc, char *argv[] )
|
||||
/*
|
||||
* Extract the RSA signature from the text file
|
||||
*/
|
||||
ret = 1;
|
||||
mbedtls_snprintf( filename, sizeof(filename), "%s.sig", argv[1] );
|
||||
|
||||
if( ( f = fopen( filename, "rb" ) ) == NULL )
|
||||
@@ -146,7 +148,7 @@ int main( int argc, char *argv[] )
|
||||
|
||||
mbedtls_printf( "\n . OK (the signature is valid)\n\n" );
|
||||
|
||||
ret = 0;
|
||||
exit_code = MBEDTLS_EXIT_SUCCESS;
|
||||
|
||||
exit:
|
||||
|
||||
@@ -157,7 +159,7 @@ exit:
|
||||
fflush( stdout ); getchar();
|
||||
#endif
|
||||
|
||||
return( ret );
|
||||
return( exit_code );
|
||||
}
|
||||
#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_RSA_C && MBEDTLS_SHA256_C &&
|
||||
MBEDTLS_FS_IO */
|
||||
|
||||
@@ -29,9 +29,12 @@
|
||||
#include "mbedtls/platform.h"
|
||||
#else
|
||||
#include <stdio.h>
|
||||
#define mbedtls_snprintf snprintf
|
||||
#define mbedtls_printf printf
|
||||
#endif
|
||||
#include <stdlib.h>
|
||||
#define mbedtls_snprintf snprintf
|
||||
#define mbedtls_printf printf
|
||||
#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
|
||||
#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
|
||||
#endif /* MBEDTLS_PLATFORM_C */
|
||||
|
||||
#if !defined(MBEDTLS_MD_C) || !defined(MBEDTLS_ENTROPY_C) || \
|
||||
!defined(MBEDTLS_RSA_C) || !defined(MBEDTLS_SHA256_C) || \
|
||||
@@ -60,6 +63,7 @@ int main( int argc, char *argv[] )
|
||||
{
|
||||
FILE *f;
|
||||
int ret = 1;
|
||||
int exit_code = MBEDTLS_EXIT_FAILURE;
|
||||
size_t i;
|
||||
mbedtls_pk_context pk;
|
||||
unsigned char hash[32];
|
||||
@@ -91,7 +95,6 @@ int main( int argc, char *argv[] )
|
||||
|
||||
if( !mbedtls_pk_can_do( &pk, MBEDTLS_PK_RSA ) )
|
||||
{
|
||||
ret = 1;
|
||||
mbedtls_printf( " failed\n ! Key is not an RSA key\n" );
|
||||
goto exit;
|
||||
}
|
||||
@@ -101,7 +104,6 @@ int main( int argc, char *argv[] )
|
||||
/*
|
||||
* Extract the RSA signature from the file
|
||||
*/
|
||||
ret = 1;
|
||||
mbedtls_snprintf( filename, 512, "%s.sig", argv[2] );
|
||||
|
||||
if( ( f = fopen( filename, "rb" ) ) == NULL )
|
||||
@@ -139,7 +141,7 @@ int main( int argc, char *argv[] )
|
||||
|
||||
mbedtls_printf( "\n . OK (the signature is valid)\n\n" );
|
||||
|
||||
ret = 0;
|
||||
exit_code = MBEDTLS_EXIT_SUCCESS;
|
||||
|
||||
exit:
|
||||
mbedtls_pk_free( &pk );
|
||||
@@ -149,7 +151,7 @@ exit:
|
||||
fflush( stdout ); getchar();
|
||||
#endif
|
||||
|
||||
return( ret );
|
||||
return( exit_code );
|
||||
}
|
||||
#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_RSA_C && MBEDTLS_SHA256_C &&
|
||||
MBEDTLS_PK_PARSE_C && MBEDTLS_FS_IO */
|
||||
|
||||
@@ -29,9 +29,12 @@
|
||||
#include "mbedtls/platform.h"
|
||||
#else
|
||||
#include <stdio.h>
|
||||
#define mbedtls_fprintf fprintf
|
||||
#define mbedtls_printf printf
|
||||
#endif
|
||||
#include <stdlib.h>
|
||||
#define mbedtls_fprintf fprintf
|
||||
#define mbedtls_printf printf
|
||||
#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
|
||||
#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
|
||||
#endif /* MBEDTLS_PLATFORM_C */
|
||||
|
||||
#if defined(MBEDTLS_ENTROPY_C) && defined(MBEDTLS_FS_IO)
|
||||
#include "mbedtls/entropy.h"
|
||||
@@ -49,20 +52,21 @@ int main( void )
|
||||
int main( int argc, char *argv[] )
|
||||
{
|
||||
FILE *f;
|
||||
int i, k, ret;
|
||||
int i, k, ret = 1;
|
||||
int exit_code = MBEDTLS_EXIT_FAILURE;
|
||||
mbedtls_entropy_context entropy;
|
||||
unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
|
||||
|
||||
if( argc < 2 )
|
||||
{
|
||||
mbedtls_fprintf( stderr, "usage: %s <output filename>\n", argv[0] );
|
||||
return( 1 );
|
||||
return( exit_code );
|
||||
}
|
||||
|
||||
if( ( f = fopen( argv[1], "wb+" ) ) == NULL )
|
||||
{
|
||||
mbedtls_printf( "failed to open '%s' for writing.\n", argv[1] );
|
||||
return( 1 );
|
||||
return( exit_code );
|
||||
}
|
||||
|
||||
mbedtls_entropy_init( &entropy );
|
||||
@@ -72,7 +76,8 @@ int main( int argc, char *argv[] )
|
||||
ret = mbedtls_entropy_func( &entropy, buf, sizeof( buf ) );
|
||||
if( ret != 0 )
|
||||
{
|
||||
mbedtls_printf("failed!\n");
|
||||
mbedtls_printf( " failed\n ! mbedtls_entropy_func returned -%04X\n",
|
||||
ret );
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
@@ -83,7 +88,7 @@ int main( int argc, char *argv[] )
|
||||
fflush( stdout );
|
||||
}
|
||||
|
||||
ret = 0;
|
||||
exit_code = MBEDTLS_EXIT_SUCCESS;
|
||||
|
||||
cleanup:
|
||||
mbedtls_printf( "\n" );
|
||||
@@ -91,6 +96,6 @@ cleanup:
|
||||
fclose( f );
|
||||
mbedtls_entropy_free( &entropy );
|
||||
|
||||
return( ret );
|
||||
return( exit_code );
|
||||
}
|
||||
#endif /* MBEDTLS_ENTROPY_C */
|
||||
|
||||
@@ -29,9 +29,12 @@
|
||||
#include "mbedtls/platform.h"
|
||||
#else
|
||||
#include <stdio.h>
|
||||
#define mbedtls_fprintf fprintf
|
||||
#define mbedtls_printf printf
|
||||
#endif
|
||||
#include <stdlib.h>
|
||||
#define mbedtls_fprintf fprintf
|
||||
#define mbedtls_printf printf
|
||||
#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
|
||||
#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
|
||||
#endif /* MBEDTLS_PLATFORM_C */
|
||||
|
||||
#if defined(MBEDTLS_CTR_DRBG_C) && defined(MBEDTLS_ENTROPY_C) && \
|
||||
defined(MBEDTLS_FS_IO)
|
||||
@@ -52,7 +55,8 @@ int main( void )
|
||||
int main( int argc, char *argv[] )
|
||||
{
|
||||
FILE *f;
|
||||
int i, k, ret;
|
||||
int i, k, ret = 1;
|
||||
int exit_code = MBEDTLS_EXIT_FAILURE;
|
||||
mbedtls_ctr_drbg_context ctr_drbg;
|
||||
mbedtls_entropy_context entropy;
|
||||
unsigned char buf[1024];
|
||||
@@ -62,13 +66,13 @@ int main( int argc, char *argv[] )
|
||||
if( argc < 2 )
|
||||
{
|
||||
mbedtls_fprintf( stderr, "usage: %s <output filename>\n", argv[0] );
|
||||
return( 1 );
|
||||
return( exit_code );
|
||||
}
|
||||
|
||||
if( ( f = fopen( argv[1], "wb+" ) ) == NULL )
|
||||
{
|
||||
mbedtls_printf( "failed to open '%s' for writing.\n", argv[1] );
|
||||
return( 1 );
|
||||
return( exit_code );
|
||||
}
|
||||
|
||||
mbedtls_entropy_init( &entropy );
|
||||
@@ -116,7 +120,7 @@ int main( int argc, char *argv[] )
|
||||
fflush( stdout );
|
||||
}
|
||||
|
||||
ret = 0;
|
||||
exit_code = MBEDTLS_EXIT_SUCCESS;
|
||||
|
||||
cleanup:
|
||||
mbedtls_printf("\n");
|
||||
@@ -125,6 +129,6 @@ cleanup:
|
||||
mbedtls_ctr_drbg_free( &ctr_drbg );
|
||||
mbedtls_entropy_free( &entropy );
|
||||
|
||||
return( ret );
|
||||
return( exit_code );
|
||||
}
|
||||
#endif /* MBEDTLS_CTR_DRBG_C && MBEDTLS_ENTROPY_C */
|
||||
|
||||
@@ -29,9 +29,12 @@
|
||||
#include "mbedtls/platform.h"
|
||||
#else
|
||||
#include <stdio.h>
|
||||
#define mbedtls_fprintf fprintf
|
||||
#define mbedtls_printf printf
|
||||
#endif
|
||||
#include <stdlib.h>
|
||||
#define mbedtls_fprintf fprintf
|
||||
#define mbedtls_printf printf
|
||||
#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
|
||||
#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
|
||||
#endif /* MBEDTLS_PLATFORM_C */
|
||||
|
||||
#if defined(MBEDTLS_HAVEGE_C) && defined(MBEDTLS_FS_IO)
|
||||
#include "mbedtls/havege.h"
|
||||
@@ -51,20 +54,21 @@ int main( int argc, char *argv[] )
|
||||
{
|
||||
FILE *f;
|
||||
time_t t;
|
||||
int i, k, ret = 0;
|
||||
int i, k, ret = 1;
|
||||
int exit_code = MBEDTLS_EXIT_FAILURE;
|
||||
mbedtls_havege_state hs;
|
||||
unsigned char buf[1024];
|
||||
|
||||
if( argc < 2 )
|
||||
{
|
||||
mbedtls_fprintf( stderr, "usage: %s <output filename>\n", argv[0] );
|
||||
return( 1 );
|
||||
return( exit_code );
|
||||
}
|
||||
|
||||
if( ( f = fopen( argv[1], "wb+" ) ) == NULL )
|
||||
{
|
||||
mbedtls_printf( "failed to open '%s' for writing.\n", argv[1] );
|
||||
return( 1 );
|
||||
return( exit_code );
|
||||
}
|
||||
|
||||
mbedtls_havege_init( &hs );
|
||||
@@ -73,11 +77,10 @@ int main( int argc, char *argv[] )
|
||||
|
||||
for( i = 0, k = 768; i < k; i++ )
|
||||
{
|
||||
if( mbedtls_havege_random( &hs, buf, sizeof( buf ) ) != 0 )
|
||||
if( ( ret = mbedtls_havege_random( &hs, buf, sizeof( buf ) ) ) != 0 )
|
||||
{
|
||||
mbedtls_printf( "Failed to get random from source.\n" );
|
||||
|
||||
ret = 1;
|
||||
mbedtls_printf( " failed\n ! mbedtls_havege_random returned -0x%04X",
|
||||
-ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
@@ -93,9 +96,11 @@ int main( int argc, char *argv[] )
|
||||
|
||||
mbedtls_printf(" \n ");
|
||||
|
||||
exit_code = MBEDTLS_EXIT_SUCCESS;
|
||||
|
||||
exit:
|
||||
mbedtls_havege_free( &hs );
|
||||
fclose( f );
|
||||
return( ret );
|
||||
return( exit_code );
|
||||
}
|
||||
#endif /* MBEDTLS_HAVEGE_C */
|
||||
|
||||
@@ -60,9 +60,18 @@ int main( void )
|
||||
#include "mbedtls/certs.h"
|
||||
#include "mbedtls/timing.h"
|
||||
|
||||
/* Uncomment out the following line to default to IPv4 and disable IPv6 */
|
||||
//#define FORCE_IPV4
|
||||
|
||||
#define SERVER_PORT "4433"
|
||||
#define SERVER_NAME "localhost"
|
||||
#define SERVER_ADDR "127.0.0.1" /* forces IPv4 */
|
||||
|
||||
#ifdef FORCE_IPV4
|
||||
#define SERVER_ADDR "127.0.0.1" /* Forces IPv4 */
|
||||
#else
|
||||
#define SERVER_ADDR "::1"
|
||||
#endif
|
||||
|
||||
#define MESSAGE "Echo this"
|
||||
|
||||
#define READ_TIMEOUT_MS 1000
|
||||
|
||||
@@ -34,6 +34,15 @@
|
||||
#define mbedtls_time_t time_t
|
||||
#endif
|
||||
|
||||
/* Uncomment out the following line to default to IPv4 and disable IPv6 */
|
||||
//#define FORCE_IPV4
|
||||
|
||||
#ifdef FORCE_IPV4
|
||||
#define BIND_IP "0.0.0.0" /* Forces IPv4 */
|
||||
#else
|
||||
#define BIND_IP "::"
|
||||
#endif
|
||||
|
||||
#if !defined(MBEDTLS_SSL_SRV_C) || !defined(MBEDTLS_SSL_PROTO_DTLS) || \
|
||||
!defined(MBEDTLS_SSL_COOKIE_C) || !defined(MBEDTLS_NET_C) || \
|
||||
!defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_CTR_DRBG_C) || \
|
||||
@@ -170,7 +179,7 @@ int main( void )
|
||||
printf( " . Bind on udp/*/4433 ..." );
|
||||
fflush( stdout );
|
||||
|
||||
if( ( ret = mbedtls_net_bind( &listen_fd, NULL, "4433", MBEDTLS_NET_PROTO_UDP ) ) != 0 )
|
||||
if( ( ret = mbedtls_net_bind( &listen_fd, BIND_IP, "4433", MBEDTLS_NET_PROTO_UDP ) ) != 0 )
|
||||
{
|
||||
printf( " failed\n ! mbedtls_net_bind returned %d\n\n", ret );
|
||||
goto exit;
|
||||
|
||||
@@ -30,11 +30,13 @@
|
||||
#else
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#define mbedtls_time time
|
||||
#define mbedtls_time_t time_t
|
||||
#define mbedtls_fprintf fprintf
|
||||
#define mbedtls_printf printf
|
||||
#endif
|
||||
#define mbedtls_time time
|
||||
#define mbedtls_time_t time_t
|
||||
#define mbedtls_fprintf fprintf
|
||||
#define mbedtls_printf printf
|
||||
#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
|
||||
#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
|
||||
#endif /* MBEDTLS_PLATFORM_C */
|
||||
|
||||
#if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_ENTROPY_C) || \
|
||||
!defined(MBEDTLS_SSL_TLS_C) || !defined(MBEDTLS_SSL_CLI_C) || \
|
||||
@@ -80,7 +82,8 @@ static void my_debug( void *ctx, int level,
|
||||
|
||||
int main( void )
|
||||
{
|
||||
int ret, len;
|
||||
int ret = 1, len;
|
||||
int exit_code = MBEDTLS_EXIT_FAILURE;
|
||||
mbedtls_net_context server_fd;
|
||||
uint32_t flags;
|
||||
unsigned char buf[1024];
|
||||
@@ -281,10 +284,12 @@ int main( void )
|
||||
|
||||
mbedtls_ssl_close_notify( &ssl );
|
||||
|
||||
exit_code = MBEDTLS_EXIT_SUCCESS;
|
||||
|
||||
exit:
|
||||
|
||||
#ifdef MBEDTLS_ERROR_C
|
||||
if( ret != 0 )
|
||||
if( exit_code != MBEDTLS_EXIT_SUCCESS )
|
||||
{
|
||||
char error_buf[100];
|
||||
mbedtls_strerror( ret, error_buf, 100 );
|
||||
@@ -305,7 +310,7 @@ exit:
|
||||
fflush( stdout ); getchar();
|
||||
#endif
|
||||
|
||||
return( ret );
|
||||
return( exit_code );
|
||||
}
|
||||
#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_SSL_TLS_C &&
|
||||
MBEDTLS_SSL_CLI_C && MBEDTLS_NET_C && MBEDTLS_RSA_C &&
|
||||
|
||||
@@ -83,6 +83,7 @@ int main( void )
|
||||
#define DFL_PSK ""
|
||||
#define DFL_PSK_IDENTITY "Client_identity"
|
||||
#define DFL_ECJPAKE_PW NULL
|
||||
#define DFL_EC_MAX_OPS -1
|
||||
#define DFL_FORCE_CIPHER 0
|
||||
#define DFL_RENEGOTIATION MBEDTLS_SSL_RENEGOTIATION_DISABLED
|
||||
#define DFL_ALLOW_LEGACY -2
|
||||
@@ -106,6 +107,8 @@ int main( void )
|
||||
#define DFL_TRANSPORT MBEDTLS_SSL_TRANSPORT_STREAM
|
||||
#define DFL_HS_TO_MIN 0
|
||||
#define DFL_HS_TO_MAX 0
|
||||
#define DFL_DTLS_MTU -1
|
||||
#define DFL_DGRAM_PACKING 1
|
||||
#define DFL_FALLBACK -1
|
||||
#define DFL_EXTENDED_MS -1
|
||||
#define DFL_ETM -1
|
||||
@@ -198,7 +201,11 @@ int main( void )
|
||||
#define USAGE_DTLS \
|
||||
" dtls=%%d default: 0 (TLS)\n" \
|
||||
" hs_timeout=%%d-%%d default: (library default: 1000-60000)\n" \
|
||||
" range of DTLS handshake timeouts in millisecs\n"
|
||||
" range of DTLS handshake timeouts in millisecs\n" \
|
||||
" mtu=%%d default: (library default: unlimited)\n" \
|
||||
" dgram_packing=%%d default: 1 (allowed)\n" \
|
||||
" allow or forbid packing of multiple\n" \
|
||||
" records within a single datgram.\n"
|
||||
#else
|
||||
#define USAGE_DTLS ""
|
||||
#endif
|
||||
@@ -239,6 +246,13 @@ int main( void )
|
||||
#define USAGE_ECJPAKE ""
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_ECP_RESTARTABLE)
|
||||
#define USAGE_ECRESTART \
|
||||
" ec_max_ops=%%s default: library default (restart disabled)\n"
|
||||
#else
|
||||
#define USAGE_ECRESTART ""
|
||||
#endif
|
||||
|
||||
#define USAGE \
|
||||
"\n usage: ssl_client2 param=<>...\n" \
|
||||
"\n acceptable parameters:\n" \
|
||||
@@ -246,8 +260,12 @@ int main( void )
|
||||
" server_addr=%%s default: given by name\n" \
|
||||
" server_port=%%d default: 4433\n" \
|
||||
" request_page=%%s default: \".\"\n" \
|
||||
" request_size=%%d default: about 34 (basic request)\n" \
|
||||
" (minimum: 0, max: " MAX_REQUEST_SIZE_STR " )\n" \
|
||||
" request_size=%%d default: about 34 (basic request)\n" \
|
||||
" (minimum: 0, max: " MAX_REQUEST_SIZE_STR ")\n" \
|
||||
" If 0, in the first exchange only an empty\n" \
|
||||
" application data message is sent followed by\n" \
|
||||
" a second non-empty message before attempting\n" \
|
||||
" to read a response from the server\n" \
|
||||
" debug_level=%%d default: 0 (disabled)\n" \
|
||||
" nbio=%%d default: 0 (blocking I/O)\n" \
|
||||
" options: 1 (non-blocking), 2 (added delays)\n" \
|
||||
@@ -264,6 +282,7 @@ int main( void )
|
||||
"\n" \
|
||||
USAGE_PSK \
|
||||
USAGE_ECJPAKE \
|
||||
USAGE_ECRESTART \
|
||||
"\n" \
|
||||
" allow_legacy=%%d default: (library default: no)\n" \
|
||||
USAGE_RENEGO \
|
||||
@@ -317,6 +336,7 @@ struct options
|
||||
const char *psk; /* the pre-shared key */
|
||||
const char *psk_identity; /* the pre-shared key identity */
|
||||
const char *ecjpake_pw; /* the EC J-PAKE password */
|
||||
int ec_max_ops; /* EC consecutive operations limit */
|
||||
int force_ciphersuite[2]; /* protocol/ciphersuite to use, or all */
|
||||
int renegotiation; /* enable / disable renegotiation */
|
||||
int allow_legacy; /* allow legacy renegotiation */
|
||||
@@ -341,7 +361,9 @@ struct options
|
||||
int transport; /* TLS or DTLS? */
|
||||
uint32_t hs_to_min; /* Initial value of DTLS handshake timer */
|
||||
uint32_t hs_to_max; /* Max value of DTLS handshake timer */
|
||||
int dtls_mtu; /* UDP Maximum tranport unit for DTLS */
|
||||
int fallback; /* is this a fallback connection? */
|
||||
int dgram_packing; /* allow/forbid datagram packing */
|
||||
int extended_ms; /* negotiate extended master secret? */
|
||||
int etm; /* negotiate encrypt then mac? */
|
||||
} opt;
|
||||
@@ -590,6 +612,7 @@ int main( int argc, char *argv[] )
|
||||
opt.psk = DFL_PSK;
|
||||
opt.psk_identity = DFL_PSK_IDENTITY;
|
||||
opt.ecjpake_pw = DFL_ECJPAKE_PW;
|
||||
opt.ec_max_ops = DFL_EC_MAX_OPS;
|
||||
opt.force_ciphersuite[0]= DFL_FORCE_CIPHER;
|
||||
opt.renegotiation = DFL_RENEGOTIATION;
|
||||
opt.allow_legacy = DFL_ALLOW_LEGACY;
|
||||
@@ -613,9 +636,11 @@ int main( int argc, char *argv[] )
|
||||
opt.transport = DFL_TRANSPORT;
|
||||
opt.hs_to_min = DFL_HS_TO_MIN;
|
||||
opt.hs_to_max = DFL_HS_TO_MAX;
|
||||
opt.dtls_mtu = DFL_DTLS_MTU;
|
||||
opt.fallback = DFL_FALLBACK;
|
||||
opt.extended_ms = DFL_EXTENDED_MS;
|
||||
opt.etm = DFL_ETM;
|
||||
opt.dgram_packing = DFL_DGRAM_PACKING;
|
||||
|
||||
for( i = 1; i < argc; i++ )
|
||||
{
|
||||
@@ -689,6 +714,8 @@ int main( int argc, char *argv[] )
|
||||
opt.psk_identity = q;
|
||||
else if( strcmp( p, "ecjpake_pw" ) == 0 )
|
||||
opt.ecjpake_pw = q;
|
||||
else if( strcmp( p, "ec_max_ops" ) == 0 )
|
||||
opt.ec_max_ops = atoi( q );
|
||||
else if( strcmp( p, "force_ciphersuite" ) == 0 )
|
||||
{
|
||||
opt.force_ciphersuite[0] = mbedtls_ssl_get_ciphersuite_id( q );
|
||||
@@ -923,6 +950,21 @@ int main( int argc, char *argv[] )
|
||||
if( opt.hs_to_min == 0 || opt.hs_to_max < opt.hs_to_min )
|
||||
goto usage;
|
||||
}
|
||||
else if( strcmp( p, "mtu" ) == 0 )
|
||||
{
|
||||
opt.dtls_mtu = atoi( q );
|
||||
if( opt.dtls_mtu < 0 )
|
||||
goto usage;
|
||||
}
|
||||
else if( strcmp( p, "dgram_packing" ) == 0 )
|
||||
{
|
||||
opt.dgram_packing = atoi( q );
|
||||
if( opt.dgram_packing != 0 &&
|
||||
opt.dgram_packing != 1 )
|
||||
{
|
||||
goto usage;
|
||||
}
|
||||
}
|
||||
else if( strcmp( p, "recsplit" ) == 0 )
|
||||
{
|
||||
opt.recsplit = atoi( q );
|
||||
@@ -1323,6 +1365,9 @@ int main( int argc, char *argv[] )
|
||||
if( opt.hs_to_min != DFL_HS_TO_MIN || opt.hs_to_max != DFL_HS_TO_MAX )
|
||||
mbedtls_ssl_conf_handshake_timeout( &conf, opt.hs_to_min,
|
||||
opt.hs_to_max );
|
||||
|
||||
if( opt.dgram_packing != DFL_DGRAM_PACKING )
|
||||
mbedtls_ssl_set_datagram_packing( &ssl, opt.dgram_packing );
|
||||
#endif /* MBEDTLS_SSL_PROTO_DTLS */
|
||||
|
||||
#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
|
||||
@@ -1481,11 +1526,21 @@ int main( int argc, char *argv[] )
|
||||
mbedtls_net_send, mbedtls_net_recv,
|
||||
opt.nbio == 0 ? mbedtls_net_recv_timeout : NULL );
|
||||
|
||||
#if defined(MBEDTLS_SSL_PROTO_DTLS)
|
||||
if( opt.dtls_mtu != DFL_DTLS_MTU )
|
||||
mbedtls_ssl_set_mtu( &ssl, opt.dtls_mtu );
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_TIMING_C)
|
||||
mbedtls_ssl_set_timer_cb( &ssl, &timer, mbedtls_timing_set_delay,
|
||||
mbedtls_timing_get_delay );
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_ECP_RESTARTABLE)
|
||||
if( opt.ec_max_ops != DFL_EC_MAX_OPS )
|
||||
mbedtls_ecp_set_max_ops( opt.ec_max_ops );
|
||||
#endif
|
||||
|
||||
mbedtls_printf( " ok\n" );
|
||||
|
||||
/*
|
||||
@@ -1497,7 +1552,8 @@ int main( int argc, char *argv[] )
|
||||
while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 )
|
||||
{
|
||||
if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
|
||||
ret != MBEDTLS_ERR_SSL_WANT_WRITE )
|
||||
ret != MBEDTLS_ERR_SSL_WANT_WRITE &&
|
||||
ret != MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! mbedtls_ssl_handshake returned -0x%x\n",
|
||||
-ret );
|
||||
@@ -1513,6 +1569,11 @@ int main( int argc, char *argv[] )
|
||||
goto exit;
|
||||
}
|
||||
|
||||
#if defined(MBEDTLS_ECP_RESTARTABLE)
|
||||
if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
|
||||
continue;
|
||||
#endif
|
||||
|
||||
/* For event-driven IO, wait for socket to become available */
|
||||
if( opt.event == 1 /* level triggered IO */ )
|
||||
{
|
||||
@@ -1605,13 +1666,19 @@ int main( int argc, char *argv[] )
|
||||
while( ( ret = mbedtls_ssl_renegotiate( &ssl ) ) != 0 )
|
||||
{
|
||||
if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
|
||||
ret != MBEDTLS_ERR_SSL_WANT_WRITE )
|
||||
ret != MBEDTLS_ERR_SSL_WANT_WRITE &&
|
||||
ret != MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! mbedtls_ssl_renegotiate returned %d\n\n",
|
||||
ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
#if defined(MBEDTLS_ECP_RESTARTABLE)
|
||||
if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
|
||||
continue;
|
||||
#endif
|
||||
|
||||
/* For event-driven IO, wait for socket to become available */
|
||||
if( opt.event == 1 /* level triggered IO */ )
|
||||
{
|
||||
@@ -1663,13 +1730,17 @@ send_request:
|
||||
|
||||
if( opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM )
|
||||
{
|
||||
for( written = 0, frags = 0; written < len; written += ret, frags++ )
|
||||
written = 0;
|
||||
frags = 0;
|
||||
|
||||
do
|
||||
{
|
||||
while( ( ret = mbedtls_ssl_write( &ssl, buf + written,
|
||||
len - written ) ) <= 0 )
|
||||
len - written ) ) < 0 )
|
||||
{
|
||||
if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
|
||||
ret != MBEDTLS_ERR_SSL_WANT_WRITE )
|
||||
ret != MBEDTLS_ERR_SSL_WANT_WRITE &&
|
||||
ret != MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! mbedtls_ssl_write returned -0x%x\n\n",
|
||||
-ret );
|
||||
@@ -1686,7 +1757,11 @@ send_request:
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
frags++;
|
||||
written += ret;
|
||||
}
|
||||
while( written < len );
|
||||
}
|
||||
else /* Not stream, so datagram */
|
||||
{
|
||||
@@ -1694,6 +1769,11 @@ send_request:
|
||||
{
|
||||
ret = mbedtls_ssl_write( &ssl, buf, len );
|
||||
|
||||
#if defined(MBEDTLS_ECP_RESTARTABLE)
|
||||
if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
|
||||
continue;
|
||||
#endif
|
||||
|
||||
if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
|
||||
ret != MBEDTLS_ERR_SSL_WANT_WRITE )
|
||||
break;
|
||||
@@ -1730,6 +1810,13 @@ send_request:
|
||||
mbedtls_printf( " %d bytes written in %d fragments\n\n%s\n",
|
||||
written, frags, (char *) buf );
|
||||
|
||||
/* Send a non-empty request if request_size == 0 */
|
||||
if ( len == 0 )
|
||||
{
|
||||
opt.request_size = DFL_REQUEST_SIZE;
|
||||
goto send_request;
|
||||
}
|
||||
|
||||
/*
|
||||
* 7. Read the HTTP response
|
||||
*/
|
||||
@@ -1747,6 +1834,11 @@ send_request:
|
||||
memset( buf, 0, sizeof( buf ) );
|
||||
ret = mbedtls_ssl_read( &ssl, buf, len );
|
||||
|
||||
#if defined(MBEDTLS_ECP_RESTARTABLE)
|
||||
if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
|
||||
continue;
|
||||
#endif
|
||||
|
||||
if( ret == MBEDTLS_ERR_SSL_WANT_READ ||
|
||||
ret == MBEDTLS_ERR_SSL_WANT_WRITE )
|
||||
{
|
||||
@@ -1807,6 +1899,11 @@ send_request:
|
||||
{
|
||||
ret = mbedtls_ssl_read( &ssl, buf, len );
|
||||
|
||||
#if defined(MBEDTLS_ECP_RESTARTABLE)
|
||||
if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
|
||||
continue;
|
||||
#endif
|
||||
|
||||
if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
|
||||
ret != MBEDTLS_ERR_SSL_WANT_WRITE )
|
||||
break;
|
||||
@@ -1869,7 +1966,8 @@ send_request:
|
||||
while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 )
|
||||
{
|
||||
if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
|
||||
ret != MBEDTLS_ERR_SSL_WANT_WRITE )
|
||||
ret != MBEDTLS_ERR_SSL_WANT_WRITE &&
|
||||
ret != MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! mbedtls_ssl_handshake returned -0x%x\n\n",
|
||||
-ret );
|
||||
@@ -1967,7 +2065,8 @@ reconnect:
|
||||
while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 )
|
||||
{
|
||||
if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
|
||||
ret != MBEDTLS_ERR_SSL_WANT_WRITE )
|
||||
ret != MBEDTLS_ERR_SSL_WANT_WRITE &&
|
||||
ret != MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! mbedtls_ssl_handshake returned -0x%x\n\n",
|
||||
-ret );
|
||||
|
||||
@@ -29,10 +29,13 @@
|
||||
#include "mbedtls/platform.h"
|
||||
#else
|
||||
#include <stdio.h>
|
||||
#define mbedtls_fprintf fprintf
|
||||
#define mbedtls_printf printf
|
||||
#define mbedtls_time_t time_t
|
||||
#endif
|
||||
#include <stdlib.h>
|
||||
#define mbedtls_fprintf fprintf
|
||||
#define mbedtls_printf printf
|
||||
#define mbedtls_time_t time_t
|
||||
#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
|
||||
#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
|
||||
#endif /* MBEDTLS_PLATFORM_C */
|
||||
|
||||
#if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_CERTS_C) || \
|
||||
!defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_SSL_TLS_C) || \
|
||||
@@ -95,7 +98,8 @@ static void my_debug( void *ctx, int level,
|
||||
|
||||
int main( void )
|
||||
{
|
||||
int ret, len, cnt = 0, pid;
|
||||
int ret = 1, len, cnt = 0, pid;
|
||||
int exit_code = MBEDTLS_EXIT_FAILURE;
|
||||
mbedtls_net_context listen_fd, client_fd;
|
||||
unsigned char buf[1024];
|
||||
const char *pers = "ssl_fork_server";
|
||||
@@ -392,6 +396,8 @@ int main( void )
|
||||
goto exit;
|
||||
}
|
||||
|
||||
exit_code = MBEDTLS_EXIT_SUCCESS;
|
||||
|
||||
exit:
|
||||
mbedtls_net_free( &client_fd );
|
||||
mbedtls_net_free( &listen_fd );
|
||||
@@ -408,7 +414,7 @@ exit:
|
||||
fflush( stdout ); getchar();
|
||||
#endif
|
||||
|
||||
return( ret );
|
||||
return( exit_code );
|
||||
}
|
||||
#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_CERTS_C && MBEDTLS_ENTROPY_C &&
|
||||
MBEDTLS_SSL_TLS_C && MBEDTLS_SSL_SRV_C && MBEDTLS_NET_C &&
|
||||
|
||||
@@ -19,6 +19,11 @@
|
||||
* This file is part of mbed TLS (https://tls.mbed.org)
|
||||
*/
|
||||
|
||||
/* Enable definition of gethostname() even when compiling with -std=c99. Must
|
||||
* be set before config.h, which pulls in glibc's features.h indirectly.
|
||||
* Harmless on other platforms. */
|
||||
#define _POSIX_C_SOURCE 200112L
|
||||
|
||||
#if !defined(MBEDTLS_CONFIG_FILE)
|
||||
#include "mbedtls/config.h"
|
||||
#else
|
||||
@@ -30,11 +35,13 @@
|
||||
#else
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#define mbedtls_time time
|
||||
#define mbedtls_time_t time_t
|
||||
#define mbedtls_fprintf fprintf
|
||||
#define mbedtls_printf printf
|
||||
#endif
|
||||
#define mbedtls_time time
|
||||
#define mbedtls_time_t time_t
|
||||
#define mbedtls_fprintf fprintf
|
||||
#define mbedtls_printf printf
|
||||
#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
|
||||
#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
|
||||
#endif /* MBEDTLS_PLATFORM_C */
|
||||
|
||||
#if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_ENTROPY_C) || \
|
||||
!defined(MBEDTLS_SSL_TLS_C) || !defined(MBEDTLS_SSL_CLI_C) || \
|
||||
@@ -346,11 +353,18 @@ static int write_and_get_response( mbedtls_net_context *sock_fd, unsigned char *
|
||||
|
||||
int main( int argc, char *argv[] )
|
||||
{
|
||||
int ret = 0, len;
|
||||
int ret = 1, len;
|
||||
int exit_code = MBEDTLS_EXIT_FAILURE;
|
||||
mbedtls_net_context server_fd;
|
||||
unsigned char buf[1024];
|
||||
#if defined(MBEDTLS_BASE64_C)
|
||||
unsigned char base[1024];
|
||||
/* buf is used as the destination buffer for printing base with the format:
|
||||
* "%s\r\n". Hence, the size of buf should be at least the size of base
|
||||
* plus 2 bytes for the \r and \n characters.
|
||||
*/
|
||||
unsigned char buf[sizeof( base ) + 2];
|
||||
#else
|
||||
unsigned char buf[1024];
|
||||
#endif
|
||||
char hostname[32];
|
||||
const char *pers = "ssl_mail_client";
|
||||
@@ -499,8 +513,8 @@ int main( int argc, char *argv[] )
|
||||
mbedtls_test_cas_pem_len );
|
||||
#else
|
||||
{
|
||||
ret = 1;
|
||||
mbedtls_printf("MBEDTLS_CERTS_C and/or MBEDTLS_PEM_PARSE_C not defined.");
|
||||
goto exit;
|
||||
}
|
||||
#endif
|
||||
if( ret < 0 )
|
||||
@@ -529,8 +543,8 @@ int main( int argc, char *argv[] )
|
||||
mbedtls_test_cli_crt_len );
|
||||
#else
|
||||
{
|
||||
ret = -1;
|
||||
mbedtls_printf("MBEDTLS_CERTS_C not defined.");
|
||||
goto exit;
|
||||
}
|
||||
#endif
|
||||
if( ret != 0 )
|
||||
@@ -549,8 +563,8 @@ int main( int argc, char *argv[] )
|
||||
mbedtls_test_cli_key_len, NULL, 0 );
|
||||
#else
|
||||
{
|
||||
ret = -1;
|
||||
mbedtls_printf("MBEDTLS_CERTS_C or MBEDTLS_PEM_PARSE_C not defined.");
|
||||
goto exit;
|
||||
}
|
||||
#endif
|
||||
if( ret != 0 )
|
||||
@@ -819,6 +833,8 @@ int main( int argc, char *argv[] )
|
||||
|
||||
mbedtls_ssl_close_notify( &ssl );
|
||||
|
||||
exit_code = MBEDTLS_EXIT_SUCCESS;
|
||||
|
||||
exit:
|
||||
|
||||
mbedtls_net_free( &server_fd );
|
||||
@@ -835,7 +851,7 @@ exit:
|
||||
fflush( stdout ); getchar();
|
||||
#endif
|
||||
|
||||
return( ret );
|
||||
return( exit_code );
|
||||
}
|
||||
#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_SSL_TLS_C &&
|
||||
MBEDTLS_SSL_CLI_C && MBEDTLS_NET_C && MBEDTLS_RSA_C **
|
||||
|
||||
@@ -97,8 +97,13 @@ int main( void )
|
||||
#include <windows.h>
|
||||
#endif
|
||||
|
||||
/* Size of memory to be allocated for the heap, when using the library's memory
|
||||
* management and MBEDTLS_MEMORY_BUFFER_ALLOC_C is enabled. */
|
||||
#define MEMORY_HEAP_SIZE 120000
|
||||
|
||||
#define DFL_SERVER_ADDR NULL
|
||||
#define DFL_SERVER_PORT "4433"
|
||||
#define DFL_RESPONSE_SIZE -1
|
||||
#define DFL_DEBUG_LEVEL 0
|
||||
#define DFL_NBIO 0
|
||||
#define DFL_EVENT 0
|
||||
@@ -109,6 +114,10 @@ int main( void )
|
||||
#define DFL_KEY_FILE ""
|
||||
#define DFL_CRT_FILE2 ""
|
||||
#define DFL_KEY_FILE2 ""
|
||||
#define DFL_ASYNC_OPERATIONS "-"
|
||||
#define DFL_ASYNC_PRIVATE_DELAY1 ( -1 )
|
||||
#define DFL_ASYNC_PRIVATE_DELAY2 ( -1 )
|
||||
#define DFL_ASYNC_PRIVATE_ERROR ( 0 )
|
||||
#define DFL_PSK ""
|
||||
#define DFL_PSK_IDENTITY "Client_identity"
|
||||
#define DFL_ECJPAKE_PW NULL
|
||||
@@ -142,7 +151,9 @@ int main( void )
|
||||
#define DFL_ANTI_REPLAY -1
|
||||
#define DFL_HS_TO_MIN 0
|
||||
#define DFL_HS_TO_MAX 0
|
||||
#define DFL_DTLS_MTU -1
|
||||
#define DFL_BADMAC_LIMIT -1
|
||||
#define DFL_DGRAM_PACKING 1
|
||||
#define DFL_EXTENDED_MS -1
|
||||
#define DFL_ETM -1
|
||||
|
||||
@@ -167,7 +178,7 @@ int main( void )
|
||||
* You will need to adapt the mbedtls_ssl_get_bytes_avail() test in ssl-opt.sh
|
||||
* if you change this value to something outside the range <= 100 or > 500
|
||||
*/
|
||||
#define IO_BUF_LEN 200
|
||||
#define DFL_IO_BUF_LEN 200
|
||||
|
||||
#if defined(MBEDTLS_X509_CRT_PARSE_C)
|
||||
#if defined(MBEDTLS_FS_IO)
|
||||
@@ -196,6 +207,18 @@ int main( void )
|
||||
#define USAGE_IO ""
|
||||
#endif /* MBEDTLS_X509_CRT_PARSE_C */
|
||||
|
||||
#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
|
||||
#define USAGE_SSL_ASYNC \
|
||||
" async_operations=%%c... d=decrypt, s=sign (default: -=off)\n" \
|
||||
" async_private_delay1=%%d Asynchronous delay for key_file or preloaded key\n" \
|
||||
" async_private_delay2=%%d Asynchronous delay for key_file2 and sni\n" \
|
||||
" default: -1 (not asynchronous)\n" \
|
||||
" async_private_error=%%d Async callback error injection (default=0=none,\n" \
|
||||
" 1=start, 2=cancel, 3=resume, negative=first time only)"
|
||||
#else
|
||||
#define USAGE_SSL_ASYNC ""
|
||||
#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
|
||||
|
||||
#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
|
||||
#define USAGE_PSK \
|
||||
" psk=%%s default: \"\" (in hex, without 0x)\n" \
|
||||
@@ -277,7 +300,11 @@ int main( void )
|
||||
#define USAGE_DTLS \
|
||||
" dtls=%%d default: 0 (TLS)\n" \
|
||||
" hs_timeout=%%d-%%d default: (library default: 1000-60000)\n" \
|
||||
" range of DTLS handshake timeouts in millisecs\n"
|
||||
" range of DTLS handshake timeouts in millisecs\n" \
|
||||
" mtu=%%d default: (library default: unlimited)\n" \
|
||||
" dgram_packing=%%d default: 1 (allowed)\n" \
|
||||
" allow or forbid packing of multiple\n" \
|
||||
" records within a single datgram.\n"
|
||||
#else
|
||||
#define USAGE_DTLS ""
|
||||
#endif
|
||||
@@ -330,6 +357,11 @@ int main( void )
|
||||
" server_addr=%%s default: (all interfaces)\n" \
|
||||
" server_port=%%d default: 4433\n" \
|
||||
" debug_level=%%d default: 0 (disabled)\n" \
|
||||
" buffer_size=%%d default: 200 \n" \
|
||||
" (minimum: 1, max: 16385)\n" \
|
||||
" response_size=%%d default: about 152 (basic response)\n" \
|
||||
" (minimum: 0, max: 16384)\n" \
|
||||
" increases buffer_size if bigger\n"\
|
||||
" nbio=%%d default: 0 (blocking I/O)\n" \
|
||||
" options: 1 (non-blocking), 2 (added delays)\n" \
|
||||
" event=%%d default: 0 (loop)\n" \
|
||||
@@ -346,6 +378,7 @@ int main( void )
|
||||
" cert_req_ca_list=%%d default: 1 (send ca list)\n" \
|
||||
" options: 1 (send ca list), 0 (don't send)\n" \
|
||||
USAGE_IO \
|
||||
USAGE_SSL_ASYNC \
|
||||
USAGE_SNI \
|
||||
"\n" \
|
||||
USAGE_PSK \
|
||||
@@ -404,12 +437,18 @@ struct options
|
||||
int nbio; /* should I/O be blocking? */
|
||||
int event; /* loop or event-driven IO? level or edge triggered? */
|
||||
uint32_t read_timeout; /* timeout on mbedtls_ssl_read() in milliseconds */
|
||||
int response_size; /* pad response with header to requested size */
|
||||
uint16_t buffer_size; /* IO buffer size */
|
||||
const char *ca_file; /* the file with the CA certificate(s) */
|
||||
const char *ca_path; /* the path with the CA certificate(s) reside */
|
||||
const char *crt_file; /* the file with the server certificate */
|
||||
const char *key_file; /* the file with the server key */
|
||||
const char *crt_file2; /* the file with the 2nd server certificate */
|
||||
const char *key_file2; /* the file with the 2nd server key */
|
||||
const char *async_operations; /* supported SSL asynchronous operations */
|
||||
int async_private_delay1; /* number of times f_async_resume needs to be called for key 1, or -1 for no async */
|
||||
int async_private_delay2; /* number of times f_async_resume needs to be called for key 2, or -1 for no async */
|
||||
int async_private_error; /* inject error in async private callback */
|
||||
const char *psk; /* the pre-shared key */
|
||||
const char *psk_identity; /* the pre-shared key identity */
|
||||
char *psk_list; /* list of PSK id/key pairs for callback */
|
||||
@@ -445,6 +484,8 @@ struct options
|
||||
int anti_replay; /* Use anti-replay for DTLS? -1 for default */
|
||||
uint32_t hs_to_min; /* Initial value of DTLS handshake timer */
|
||||
uint32_t hs_to_max; /* Max value of DTLS handshake timer */
|
||||
int dtls_mtu; /* UDP Maximum tranport unit for DTLS */
|
||||
int dgram_packing; /* allow/forbid datagram packing */
|
||||
int badmac_limit; /* Limit of records with bad MAC */
|
||||
} opt;
|
||||
|
||||
@@ -841,6 +882,244 @@ static int ssl_sig_hashes_for_test[] = {
|
||||
};
|
||||
#endif /* MBEDTLS_X509_CRT_PARSE_C */
|
||||
|
||||
/** Return true if \p ret is a status code indicating that there is an
|
||||
* operation in progress on an SSL connection, and false if it indicates
|
||||
* success or a fatal error.
|
||||
*
|
||||
* The possible operations in progress are:
|
||||
*
|
||||
* - A read, when the SSL input buffer does not contain a full message.
|
||||
* - A write, when the SSL output buffer contains some data that has not
|
||||
* been sent over the network yet.
|
||||
* - An asynchronous callback that has not completed yet. */
|
||||
static int mbedtls_status_is_ssl_in_progress( int ret )
|
||||
{
|
||||
return( ret == MBEDTLS_ERR_SSL_WANT_READ ||
|
||||
ret == MBEDTLS_ERR_SSL_WANT_WRITE ||
|
||||
ret == MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS );
|
||||
}
|
||||
|
||||
#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
|
||||
typedef struct
|
||||
{
|
||||
mbedtls_x509_crt *cert; /*!< Certificate corresponding to the key */
|
||||
mbedtls_pk_context *pk; /*!< Private key */
|
||||
unsigned delay; /*!< Number of resume steps to go through */
|
||||
unsigned pk_owned : 1; /*!< Whether to free the pk object on exit */
|
||||
} ssl_async_key_slot_t;
|
||||
|
||||
typedef enum {
|
||||
SSL_ASYNC_INJECT_ERROR_NONE = 0, /*!< Let the callbacks succeed */
|
||||
SSL_ASYNC_INJECT_ERROR_START, /*!< Inject error during start */
|
||||
SSL_ASYNC_INJECT_ERROR_CANCEL, /*!< Close the connection after async start */
|
||||
SSL_ASYNC_INJECT_ERROR_RESUME, /*!< Inject error during resume */
|
||||
#define SSL_ASYNC_INJECT_ERROR_MAX SSL_ASYNC_INJECT_ERROR_RESUME
|
||||
} ssl_async_inject_error_t;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
ssl_async_key_slot_t slots[4]; /* key, key2, sni1, sni2 */
|
||||
size_t slots_used;
|
||||
ssl_async_inject_error_t inject_error;
|
||||
int (*f_rng)(void *, unsigned char *, size_t);
|
||||
void *p_rng;
|
||||
} ssl_async_key_context_t;
|
||||
|
||||
int ssl_async_set_key( ssl_async_key_context_t *ctx,
|
||||
mbedtls_x509_crt *cert,
|
||||
mbedtls_pk_context *pk,
|
||||
int pk_take_ownership,
|
||||
unsigned delay )
|
||||
{
|
||||
if( ctx->slots_used >= sizeof( ctx->slots ) / sizeof( *ctx->slots ) )
|
||||
return( -1 );
|
||||
ctx->slots[ctx->slots_used].cert = cert;
|
||||
ctx->slots[ctx->slots_used].pk = pk;
|
||||
ctx->slots[ctx->slots_used].delay = delay;
|
||||
ctx->slots[ctx->slots_used].pk_owned = pk_take_ownership;
|
||||
++ctx->slots_used;
|
||||
return( 0 );
|
||||
}
|
||||
|
||||
#define SSL_ASYNC_INPUT_MAX_SIZE 512
|
||||
|
||||
typedef enum
|
||||
{
|
||||
ASYNC_OP_SIGN,
|
||||
ASYNC_OP_DECRYPT,
|
||||
} ssl_async_operation_type_t;
|
||||
/* Note that the enum above and the array below need to be kept in sync!
|
||||
* `ssl_async_operation_names[op]` is the name of op for each value `op`
|
||||
* of type `ssl_async_operation_type_t`. */
|
||||
static const char *const ssl_async_operation_names[] =
|
||||
{
|
||||
"sign",
|
||||
"decrypt",
|
||||
};
|
||||
|
||||
typedef struct
|
||||
{
|
||||
unsigned slot;
|
||||
ssl_async_operation_type_t operation_type;
|
||||
mbedtls_md_type_t md_alg;
|
||||
unsigned char input[SSL_ASYNC_INPUT_MAX_SIZE];
|
||||
size_t input_len;
|
||||
unsigned remaining_delay;
|
||||
} ssl_async_operation_context_t;
|
||||
|
||||
static int ssl_async_start( mbedtls_ssl_context *ssl,
|
||||
mbedtls_x509_crt *cert,
|
||||
ssl_async_operation_type_t op_type,
|
||||
mbedtls_md_type_t md_alg,
|
||||
const unsigned char *input,
|
||||
size_t input_len )
|
||||
{
|
||||
ssl_async_key_context_t *config_data =
|
||||
mbedtls_ssl_conf_get_async_config_data( ssl->conf );
|
||||
unsigned slot;
|
||||
ssl_async_operation_context_t *ctx = NULL;
|
||||
const char *op_name = ssl_async_operation_names[op_type];
|
||||
|
||||
{
|
||||
char dn[100];
|
||||
if( mbedtls_x509_dn_gets( dn, sizeof( dn ), &cert->subject ) > 0 )
|
||||
mbedtls_printf( "Async %s callback: looking for DN=%s\n",
|
||||
op_name, dn );
|
||||
}
|
||||
|
||||
/* Look for a private key that matches the public key in cert.
|
||||
* Since this test code has the private key inside Mbed TLS,
|
||||
* we call mbedtls_pk_check_pair to match a private key with the
|
||||
* public key. */
|
||||
for( slot = 0; slot < config_data->slots_used; slot++ )
|
||||
{
|
||||
if( mbedtls_pk_check_pair( &cert->pk,
|
||||
config_data->slots[slot].pk ) == 0 )
|
||||
break;
|
||||
}
|
||||
if( slot == config_data->slots_used )
|
||||
{
|
||||
mbedtls_printf( "Async %s callback: no key matches this certificate.\n",
|
||||
op_name );
|
||||
return( MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH );
|
||||
}
|
||||
mbedtls_printf( "Async %s callback: using key slot %u, delay=%u.\n",
|
||||
op_name, slot, config_data->slots[slot].delay );
|
||||
|
||||
if( config_data->inject_error == SSL_ASYNC_INJECT_ERROR_START )
|
||||
{
|
||||
mbedtls_printf( "Async %s callback: injected error\n", op_name );
|
||||
return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
|
||||
}
|
||||
|
||||
if( input_len > SSL_ASYNC_INPUT_MAX_SIZE )
|
||||
return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
|
||||
|
||||
ctx = mbedtls_calloc( 1, sizeof( *ctx ) );
|
||||
if( ctx == NULL )
|
||||
return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
|
||||
ctx->slot = slot;
|
||||
ctx->operation_type = op_type;
|
||||
ctx->md_alg = md_alg;
|
||||
memcpy( ctx->input, input, input_len );
|
||||
ctx->input_len = input_len;
|
||||
ctx->remaining_delay = config_data->slots[slot].delay;
|
||||
mbedtls_ssl_set_async_operation_data( ssl, ctx );
|
||||
|
||||
if( ctx->remaining_delay == 0 )
|
||||
return( 0 );
|
||||
else
|
||||
return( MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS );
|
||||
}
|
||||
|
||||
static int ssl_async_sign( mbedtls_ssl_context *ssl,
|
||||
mbedtls_x509_crt *cert,
|
||||
mbedtls_md_type_t md_alg,
|
||||
const unsigned char *hash,
|
||||
size_t hash_len )
|
||||
{
|
||||
return( ssl_async_start( ssl, cert,
|
||||
ASYNC_OP_SIGN, md_alg,
|
||||
hash, hash_len ) );
|
||||
}
|
||||
|
||||
static int ssl_async_decrypt( mbedtls_ssl_context *ssl,
|
||||
mbedtls_x509_crt *cert,
|
||||
const unsigned char *input,
|
||||
size_t input_len )
|
||||
{
|
||||
return( ssl_async_start( ssl, cert,
|
||||
ASYNC_OP_DECRYPT, MBEDTLS_MD_NONE,
|
||||
input, input_len ) );
|
||||
}
|
||||
|
||||
static int ssl_async_resume( mbedtls_ssl_context *ssl,
|
||||
unsigned char *output,
|
||||
size_t *output_len,
|
||||
size_t output_size )
|
||||
{
|
||||
ssl_async_operation_context_t *ctx = mbedtls_ssl_get_async_operation_data( ssl );
|
||||
ssl_async_key_context_t *config_data =
|
||||
mbedtls_ssl_conf_get_async_config_data( ssl->conf );
|
||||
ssl_async_key_slot_t *key_slot = &config_data->slots[ctx->slot];
|
||||
int ret;
|
||||
const char *op_name;
|
||||
|
||||
if( ctx->remaining_delay > 0 )
|
||||
{
|
||||
--ctx->remaining_delay;
|
||||
mbedtls_printf( "Async resume (slot %u): call %u more times.\n",
|
||||
ctx->slot, ctx->remaining_delay );
|
||||
return( MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS );
|
||||
}
|
||||
|
||||
switch( ctx->operation_type )
|
||||
{
|
||||
case ASYNC_OP_DECRYPT:
|
||||
ret = mbedtls_pk_decrypt( key_slot->pk,
|
||||
ctx->input, ctx->input_len,
|
||||
output, output_len, output_size,
|
||||
config_data->f_rng, config_data->p_rng );
|
||||
break;
|
||||
case ASYNC_OP_SIGN:
|
||||
ret = mbedtls_pk_sign( key_slot->pk,
|
||||
ctx->md_alg,
|
||||
ctx->input, ctx->input_len,
|
||||
output, output_len,
|
||||
config_data->f_rng, config_data->p_rng );
|
||||
break;
|
||||
default:
|
||||
mbedtls_printf( "Async resume (slot %u): unknown operation type %ld. This shouldn't happen.\n",
|
||||
ctx->slot, (long) ctx->operation_type );
|
||||
mbedtls_free( ctx );
|
||||
return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
|
||||
break;
|
||||
}
|
||||
|
||||
op_name = ssl_async_operation_names[ctx->operation_type];
|
||||
|
||||
if( config_data->inject_error == SSL_ASYNC_INJECT_ERROR_RESUME )
|
||||
{
|
||||
mbedtls_printf( "Async resume callback: %s done but injected error\n",
|
||||
op_name );
|
||||
mbedtls_free( ctx );
|
||||
return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
|
||||
}
|
||||
|
||||
mbedtls_printf( "Async resume (slot %u): %s done, status=%d.\n",
|
||||
ctx->slot, op_name, ret );
|
||||
mbedtls_free( ctx );
|
||||
return( ret );
|
||||
}
|
||||
|
||||
static void ssl_async_cancel( mbedtls_ssl_context *ssl )
|
||||
{
|
||||
ssl_async_operation_context_t *ctx = mbedtls_ssl_get_async_operation_data( ssl );
|
||||
mbedtls_printf( "Async cancel callback.\n" );
|
||||
mbedtls_free( ctx );
|
||||
}
|
||||
#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
|
||||
|
||||
/*
|
||||
* Wait for an event from the underlying transport or the timer
|
||||
* (Used in event-driven IO mode).
|
||||
@@ -895,7 +1174,7 @@ int main( int argc, char *argv[] )
|
||||
{
|
||||
int ret = 0, len, written, frags, exchanges_left;
|
||||
int version_suites[4][2];
|
||||
unsigned char buf[IO_BUF_LEN];
|
||||
unsigned char* buf = 0;
|
||||
#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
|
||||
unsigned char psk[MBEDTLS_PSK_MAX_LEN];
|
||||
size_t psk_len = 0;
|
||||
@@ -929,7 +1208,10 @@ int main( int argc, char *argv[] )
|
||||
mbedtls_x509_crt srvcert2;
|
||||
mbedtls_pk_context pkey2;
|
||||
int key_cert_init = 0, key_cert_init2 = 0;
|
||||
#endif
|
||||
#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
|
||||
ssl_async_key_context_t ssl_async_keys;
|
||||
#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
|
||||
#endif /* MBEDTLS_X509_CRT_PARSE_C */
|
||||
#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_FS_IO)
|
||||
mbedtls_dhm_context dhm;
|
||||
#endif
|
||||
@@ -950,7 +1232,7 @@ int main( int argc, char *argv[] )
|
||||
const char *alpn_list[ALPN_LIST_SIZE];
|
||||
#endif
|
||||
#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
|
||||
unsigned char alloc_buf[100000];
|
||||
unsigned char alloc_buf[MEMORY_HEAP_SIZE];
|
||||
#endif
|
||||
|
||||
int i;
|
||||
@@ -975,6 +1257,9 @@ int main( int argc, char *argv[] )
|
||||
mbedtls_pk_init( &pkey );
|
||||
mbedtls_x509_crt_init( &srvcert2 );
|
||||
mbedtls_pk_init( &pkey2 );
|
||||
#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
|
||||
memset( &ssl_async_keys, 0, sizeof( ssl_async_keys ) );
|
||||
#endif
|
||||
#endif
|
||||
#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_FS_IO)
|
||||
mbedtls_dhm_init( &dhm );
|
||||
@@ -1020,10 +1305,12 @@ int main( int argc, char *argv[] )
|
||||
goto exit;
|
||||
}
|
||||
|
||||
opt.buffer_size = DFL_IO_BUF_LEN;
|
||||
opt.server_addr = DFL_SERVER_ADDR;
|
||||
opt.server_port = DFL_SERVER_PORT;
|
||||
opt.debug_level = DFL_DEBUG_LEVEL;
|
||||
opt.event = DFL_EVENT;
|
||||
opt.response_size = DFL_RESPONSE_SIZE;
|
||||
opt.nbio = DFL_NBIO;
|
||||
opt.read_timeout = DFL_READ_TIMEOUT;
|
||||
opt.ca_file = DFL_CA_FILE;
|
||||
@@ -1032,6 +1319,10 @@ int main( int argc, char *argv[] )
|
||||
opt.key_file = DFL_KEY_FILE;
|
||||
opt.crt_file2 = DFL_CRT_FILE2;
|
||||
opt.key_file2 = DFL_KEY_FILE2;
|
||||
opt.async_operations = DFL_ASYNC_OPERATIONS;
|
||||
opt.async_private_delay1 = DFL_ASYNC_PRIVATE_DELAY1;
|
||||
opt.async_private_delay2 = DFL_ASYNC_PRIVATE_DELAY2;
|
||||
opt.async_private_error = DFL_ASYNC_PRIVATE_ERROR;
|
||||
opt.psk = DFL_PSK;
|
||||
opt.psk_identity = DFL_PSK_IDENTITY;
|
||||
opt.psk_list = DFL_PSK_LIST;
|
||||
@@ -1065,6 +1356,8 @@ int main( int argc, char *argv[] )
|
||||
opt.anti_replay = DFL_ANTI_REPLAY;
|
||||
opt.hs_to_min = DFL_HS_TO_MIN;
|
||||
opt.hs_to_max = DFL_HS_TO_MAX;
|
||||
opt.dtls_mtu = DFL_DTLS_MTU;
|
||||
opt.dgram_packing = DFL_DGRAM_PACKING;
|
||||
opt.badmac_limit = DFL_BADMAC_LIMIT;
|
||||
opt.extended_ms = DFL_EXTENDED_MS;
|
||||
opt.etm = DFL_ETM;
|
||||
@@ -1110,6 +1403,20 @@ int main( int argc, char *argv[] )
|
||||
}
|
||||
else if( strcmp( p, "read_timeout" ) == 0 )
|
||||
opt.read_timeout = atoi( q );
|
||||
else if( strcmp( p, "buffer_size" ) == 0 )
|
||||
{
|
||||
opt.buffer_size = atoi( q );
|
||||
if( opt.buffer_size < 1 || opt.buffer_size > MBEDTLS_SSL_MAX_CONTENT_LEN + 1 )
|
||||
goto usage;
|
||||
}
|
||||
else if( strcmp( p, "response_size" ) == 0 )
|
||||
{
|
||||
opt.response_size = atoi( q );
|
||||
if( opt.response_size < 0 || opt.response_size > MBEDTLS_SSL_MAX_CONTENT_LEN )
|
||||
goto usage;
|
||||
if( opt.buffer_size < opt.response_size )
|
||||
opt.buffer_size = opt.response_size;
|
||||
}
|
||||
else if( strcmp( p, "ca_file" ) == 0 )
|
||||
opt.ca_file = q;
|
||||
else if( strcmp( p, "ca_path" ) == 0 )
|
||||
@@ -1124,6 +1431,25 @@ int main( int argc, char *argv[] )
|
||||
opt.key_file2 = q;
|
||||
else if( strcmp( p, "dhm_file" ) == 0 )
|
||||
opt.dhm_file = q;
|
||||
#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
|
||||
else if( strcmp( p, "async_operations" ) == 0 )
|
||||
opt.async_operations = q;
|
||||
else if( strcmp( p, "async_private_delay1" ) == 0 )
|
||||
opt.async_private_delay1 = atoi( q );
|
||||
else if( strcmp( p, "async_private_delay2" ) == 0 )
|
||||
opt.async_private_delay2 = atoi( q );
|
||||
else if( strcmp( p, "async_private_error" ) == 0 )
|
||||
{
|
||||
int n = atoi( q );
|
||||
if( n < -SSL_ASYNC_INJECT_ERROR_MAX ||
|
||||
n > SSL_ASYNC_INJECT_ERROR_MAX )
|
||||
{
|
||||
ret = 2;
|
||||
goto usage;
|
||||
}
|
||||
opt.async_private_error = n;
|
||||
}
|
||||
#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
|
||||
else if( strcmp( p, "psk" ) == 0 )
|
||||
opt.psk = q;
|
||||
else if( strcmp( p, "psk_identity" ) == 0 )
|
||||
@@ -1392,6 +1718,21 @@ int main( int argc, char *argv[] )
|
||||
if( opt.hs_to_min == 0 || opt.hs_to_max < opt.hs_to_min )
|
||||
goto usage;
|
||||
}
|
||||
else if( strcmp( p, "mtu" ) == 0 )
|
||||
{
|
||||
opt.dtls_mtu = atoi( q );
|
||||
if( opt.dtls_mtu < 0 )
|
||||
goto usage;
|
||||
}
|
||||
else if( strcmp( p, "dgram_packing" ) == 0 )
|
||||
{
|
||||
opt.dgram_packing = atoi( q );
|
||||
if( opt.dgram_packing != 0 &&
|
||||
opt.dgram_packing != 1 )
|
||||
{
|
||||
goto usage;
|
||||
}
|
||||
}
|
||||
else if( strcmp( p, "sni" ) == 0 )
|
||||
{
|
||||
opt.sni = q;
|
||||
@@ -1412,6 +1753,13 @@ int main( int argc, char *argv[] )
|
||||
#if defined(MBEDTLS_DEBUG_C)
|
||||
mbedtls_debug_set_threshold( opt.debug_level );
|
||||
#endif
|
||||
buf = mbedtls_calloc( 1, opt.buffer_size + 1 );
|
||||
if( buf == NULL )
|
||||
{
|
||||
mbedtls_printf( "Could not allocate %u bytes\n", opt.buffer_size );
|
||||
ret = 3;
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if( opt.force_ciphersuite[0] > 0 )
|
||||
{
|
||||
@@ -1863,6 +2211,9 @@ int main( int argc, char *argv[] )
|
||||
#if defined(MBEDTLS_SSL_PROTO_DTLS)
|
||||
if( opt.hs_to_min != DFL_HS_TO_MIN || opt.hs_to_max != DFL_HS_TO_MAX )
|
||||
mbedtls_ssl_conf_handshake_timeout( &conf, opt.hs_to_min, opt.hs_to_max );
|
||||
|
||||
if( opt.dgram_packing != DFL_DGRAM_PACKING )
|
||||
mbedtls_ssl_set_datagram_packing( &ssl, opt.dgram_packing );
|
||||
#endif /* MBEDTLS_SSL_PROTO_DTLS */
|
||||
|
||||
#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
|
||||
@@ -2018,22 +2369,109 @@ int main( int argc, char *argv[] )
|
||||
mbedtls_ssl_conf_ca_chain( &conf, &cacert, NULL );
|
||||
}
|
||||
if( key_cert_init )
|
||||
if( ( ret = mbedtls_ssl_conf_own_cert( &conf, &srvcert, &pkey ) ) != 0 )
|
||||
{
|
||||
mbedtls_pk_context *pk = &pkey;
|
||||
#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
|
||||
if( opt.async_private_delay1 >= 0 )
|
||||
{
|
||||
ret = ssl_async_set_key( &ssl_async_keys, &srvcert, pk, 0,
|
||||
opt.async_private_delay1 );
|
||||
if( ret < 0 )
|
||||
{
|
||||
mbedtls_printf( " Test error: ssl_async_set_key failed (%d)\n",
|
||||
ret );
|
||||
goto exit;
|
||||
}
|
||||
pk = NULL;
|
||||
}
|
||||
#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
|
||||
if( ( ret = mbedtls_ssl_conf_own_cert( &conf, &srvcert, pk ) ) != 0 )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! mbedtls_ssl_conf_own_cert returned %d\n\n", ret );
|
||||
goto exit;
|
||||
}
|
||||
}
|
||||
if( key_cert_init2 )
|
||||
if( ( ret = mbedtls_ssl_conf_own_cert( &conf, &srvcert2, &pkey2 ) ) != 0 )
|
||||
{
|
||||
mbedtls_pk_context *pk = &pkey2;
|
||||
#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
|
||||
if( opt.async_private_delay2 >= 0 )
|
||||
{
|
||||
ret = ssl_async_set_key( &ssl_async_keys, &srvcert2, pk, 0,
|
||||
opt.async_private_delay2 );
|
||||
if( ret < 0 )
|
||||
{
|
||||
mbedtls_printf( " Test error: ssl_async_set_key failed (%d)\n",
|
||||
ret );
|
||||
goto exit;
|
||||
}
|
||||
pk = NULL;
|
||||
}
|
||||
#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
|
||||
if( ( ret = mbedtls_ssl_conf_own_cert( &conf, &srvcert2, pk ) ) != 0 )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! mbedtls_ssl_conf_own_cert returned %d\n\n", ret );
|
||||
goto exit;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
|
||||
if( opt.async_operations[0] != '-' )
|
||||
{
|
||||
mbedtls_ssl_async_sign_t *sign = NULL;
|
||||
mbedtls_ssl_async_decrypt_t *decrypt = NULL;
|
||||
const char *r;
|
||||
for( r = opt.async_operations; *r; r++ )
|
||||
{
|
||||
switch( *r )
|
||||
{
|
||||
case 'd':
|
||||
decrypt = ssl_async_decrypt;
|
||||
break;
|
||||
case 's':
|
||||
sign = ssl_async_sign;
|
||||
break;
|
||||
}
|
||||
}
|
||||
ssl_async_keys.inject_error = ( opt.async_private_error < 0 ?
|
||||
- opt.async_private_error :
|
||||
opt.async_private_error );
|
||||
ssl_async_keys.f_rng = mbedtls_ctr_drbg_random;
|
||||
ssl_async_keys.p_rng = &ctr_drbg;
|
||||
mbedtls_ssl_conf_async_private_cb( &conf,
|
||||
sign,
|
||||
decrypt,
|
||||
ssl_async_resume,
|
||||
ssl_async_cancel,
|
||||
&ssl_async_keys );
|
||||
}
|
||||
#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
|
||||
#endif /* MBEDTLS_X509_CRT_PARSE_C */
|
||||
|
||||
#if defined(SNI_OPTION)
|
||||
if( opt.sni != NULL )
|
||||
{
|
||||
mbedtls_ssl_conf_sni( &conf, sni_callback, sni_info );
|
||||
#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
|
||||
if( opt.async_private_delay2 >= 0 )
|
||||
{
|
||||
sni_entry *cur;
|
||||
for( cur = sni_info; cur != NULL; cur = cur->next )
|
||||
{
|
||||
ret = ssl_async_set_key( &ssl_async_keys,
|
||||
cur->cert, cur->key, 1,
|
||||
opt.async_private_delay2 );
|
||||
if( ret < 0 )
|
||||
{
|
||||
mbedtls_printf( " Test error: ssl_async_set_key failed (%d)\n",
|
||||
ret );
|
||||
goto exit;
|
||||
}
|
||||
cur->key = NULL;
|
||||
}
|
||||
}
|
||||
#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_ECP_C)
|
||||
@@ -2094,6 +2532,11 @@ int main( int argc, char *argv[] )
|
||||
mbedtls_ssl_set_bio( &ssl, &client_fd, mbedtls_net_send, mbedtls_net_recv,
|
||||
opt.nbio == 0 ? mbedtls_net_recv_timeout : NULL );
|
||||
|
||||
#if defined(MBEDTLS_SSL_PROTO_DTLS)
|
||||
if( opt.dtls_mtu != DFL_DTLS_MTU )
|
||||
mbedtls_ssl_set_mtu( &ssl, opt.dtls_mtu );
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_TIMING_C)
|
||||
mbedtls_ssl_set_timer_cb( &ssl, &timer, mbedtls_timing_set_delay,
|
||||
mbedtls_timing_get_delay );
|
||||
@@ -2205,8 +2648,16 @@ handshake:
|
||||
|
||||
while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 )
|
||||
{
|
||||
if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
|
||||
ret != MBEDTLS_ERR_SSL_WANT_WRITE )
|
||||
#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
|
||||
if( ret == MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS &&
|
||||
ssl_async_keys.inject_error == SSL_ASYNC_INJECT_ERROR_CANCEL )
|
||||
{
|
||||
mbedtls_printf( " cancelling on injected error\n" );
|
||||
break;
|
||||
}
|
||||
#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
|
||||
|
||||
if( ! mbedtls_status_is_ssl_in_progress( ret ) )
|
||||
break;
|
||||
|
||||
/* For event-driven IO, wait for socket to become available */
|
||||
@@ -2244,6 +2695,11 @@ handshake:
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
|
||||
if( opt.async_private_error < 0 )
|
||||
/* Injected error only the first time round, to test reset */
|
||||
ssl_async_keys.inject_error = SSL_ASYNC_INJECT_ERROR_NONE;
|
||||
#endif
|
||||
goto reset;
|
||||
}
|
||||
else /* ret == 0 */
|
||||
@@ -2320,12 +2776,11 @@ data_exchange:
|
||||
do
|
||||
{
|
||||
int terminated = 0;
|
||||
len = sizeof( buf ) - 1;
|
||||
memset( buf, 0, sizeof( buf ) );
|
||||
len = opt.buffer_size - 1;
|
||||
memset( buf, 0, opt.buffer_size );
|
||||
ret = mbedtls_ssl_read( &ssl, buf, len );
|
||||
|
||||
if( ret == MBEDTLS_ERR_SSL_WANT_READ ||
|
||||
ret == MBEDTLS_ERR_SSL_WANT_WRITE )
|
||||
if( mbedtls_status_is_ssl_in_progress( ret ) )
|
||||
{
|
||||
if( opt.event == 1 /* level triggered IO */ )
|
||||
{
|
||||
@@ -2422,10 +2877,10 @@ data_exchange:
|
||||
}
|
||||
else /* Not stream, so datagram */
|
||||
{
|
||||
len = sizeof( buf ) - 1;
|
||||
memset( buf, 0, sizeof( buf ) );
|
||||
len = opt.buffer_size - 1;
|
||||
memset( buf, 0, opt.buffer_size );
|
||||
|
||||
while( 1 )
|
||||
do
|
||||
{
|
||||
/* Without the call to `mbedtls_ssl_check_pending`, it might
|
||||
* happen that the client sends application data in the same
|
||||
@@ -2455,10 +2910,8 @@ data_exchange:
|
||||
* it can happen that the subsequent call to `mbedtls_ssl_read`
|
||||
* returns `MBEDTLS_ERR_SSL_WANT_READ`, because the pending messages
|
||||
* might be discarded (e.g. because they are retransmissions). */
|
||||
if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
|
||||
ret != MBEDTLS_ERR_SSL_WANT_WRITE )
|
||||
break;
|
||||
}
|
||||
while( mbedtls_status_is_ssl_in_progress( ret ) );
|
||||
|
||||
if( ret <= 0 )
|
||||
{
|
||||
@@ -2493,8 +2946,7 @@ data_exchange:
|
||||
|
||||
while( ( ret = mbedtls_ssl_renegotiate( &ssl ) ) != 0 )
|
||||
{
|
||||
if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
|
||||
ret != MBEDTLS_ERR_SSL_WANT_WRITE )
|
||||
if( ! mbedtls_status_is_ssl_in_progress( ret ) )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! mbedtls_ssl_renegotiate returned %d\n\n", ret );
|
||||
goto reset;
|
||||
@@ -2524,6 +2976,25 @@ data_exchange:
|
||||
len = sprintf( (char *) buf, HTTP_RESPONSE,
|
||||
mbedtls_ssl_get_ciphersuite( &ssl ) );
|
||||
|
||||
/* Add padding to the response to reach opt.response_size in length */
|
||||
if( opt.response_size != DFL_RESPONSE_SIZE &&
|
||||
len < opt.response_size )
|
||||
{
|
||||
memset( buf + len, 'B', opt.response_size - len );
|
||||
len += opt.response_size - len;
|
||||
}
|
||||
|
||||
/* Truncate if response size is smaller than the "natural" size */
|
||||
if( opt.response_size != DFL_RESPONSE_SIZE &&
|
||||
len > opt.response_size )
|
||||
{
|
||||
len = opt.response_size;
|
||||
|
||||
/* Still end with \r\n unless that's really not possible */
|
||||
if( len >= 2 ) buf[len - 2] = '\r';
|
||||
if( len >= 1 ) buf[len - 1] = '\n';
|
||||
}
|
||||
|
||||
if( opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM )
|
||||
{
|
||||
for( written = 0, frags = 0; written < len; written += ret, frags++ )
|
||||
@@ -2537,8 +3008,7 @@ data_exchange:
|
||||
goto reset;
|
||||
}
|
||||
|
||||
if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
|
||||
ret != MBEDTLS_ERR_SSL_WANT_WRITE )
|
||||
if( ! mbedtls_status_is_ssl_in_progress( ret ) )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! mbedtls_ssl_write returned %d\n\n", ret );
|
||||
goto reset;
|
||||
@@ -2562,8 +3032,7 @@ data_exchange:
|
||||
{
|
||||
ret = mbedtls_ssl_write( &ssl, buf, len );
|
||||
|
||||
if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
|
||||
ret != MBEDTLS_ERR_SSL_WANT_WRITE )
|
||||
if( ! mbedtls_status_is_ssl_in_progress( ret ) )
|
||||
break;
|
||||
|
||||
/* For event-driven IO, wait for socket to become available */
|
||||
@@ -2641,6 +3110,17 @@ exit:
|
||||
mbedtls_x509_crt_free( &srvcert2 );
|
||||
mbedtls_pk_free( &pkey2 );
|
||||
#endif
|
||||
#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
|
||||
for( i = 0; (size_t) i < ssl_async_keys.slots_used; i++ )
|
||||
{
|
||||
if( ssl_async_keys.slots[i].pk_owned )
|
||||
{
|
||||
mbedtls_pk_free( ssl_async_keys.slots[i].pk );
|
||||
mbedtls_free( ssl_async_keys.slots[i].pk );
|
||||
ssl_async_keys.slots[i].pk = NULL;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
#if defined(SNI_OPTION)
|
||||
sni_free( sni_info );
|
||||
#endif
|
||||
@@ -2666,6 +3146,8 @@ exit:
|
||||
mbedtls_ssl_cookie_free( &cookie_ctx );
|
||||
#endif
|
||||
|
||||
mbedtls_free( buf );
|
||||
|
||||
#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
|
||||
#if defined(MBEDTLS_MEMORY_DEBUG)
|
||||
mbedtls_memory_buffer_alloc_status();
|
||||
|
||||
@@ -16,6 +16,11 @@ target_link_libraries(selftest ${libs})
|
||||
add_executable(benchmark benchmark.c)
|
||||
target_link_libraries(benchmark ${libs})
|
||||
|
||||
if(TEST_CPP)
|
||||
add_executable(cpp_dummy_build cpp_dummy_build.cpp)
|
||||
target_link_libraries(cpp_dummy_build ${libs})
|
||||
endif()
|
||||
|
||||
add_executable(ssl_cert_test ssl_cert_test.c)
|
||||
target_link_libraries(ssl_cert_test ${libs})
|
||||
|
||||
|
||||
@@ -54,21 +54,29 @@ int main( void )
|
||||
#include "mbedtls/sha1.h"
|
||||
#include "mbedtls/sha256.h"
|
||||
#include "mbedtls/sha512.h"
|
||||
|
||||
#include "mbedtls/arc4.h"
|
||||
#include "mbedtls/des.h"
|
||||
#include "mbedtls/aes.h"
|
||||
#include "mbedtls/aria.h"
|
||||
#include "mbedtls/blowfish.h"
|
||||
#include "mbedtls/camellia.h"
|
||||
#include "mbedtls/chacha20.h"
|
||||
#include "mbedtls/gcm.h"
|
||||
#include "mbedtls/ccm.h"
|
||||
#include "mbedtls/chachapoly.h"
|
||||
#include "mbedtls/cmac.h"
|
||||
#include "mbedtls/poly1305.h"
|
||||
|
||||
#include "mbedtls/havege.h"
|
||||
#include "mbedtls/ctr_drbg.h"
|
||||
#include "mbedtls/hmac_drbg.h"
|
||||
|
||||
#include "mbedtls/rsa.h"
|
||||
#include "mbedtls/dhm.h"
|
||||
#include "mbedtls/ecdsa.h"
|
||||
#include "mbedtls/ecdh.h"
|
||||
|
||||
#include "mbedtls/error.h"
|
||||
|
||||
#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
|
||||
@@ -93,8 +101,9 @@ int main( void )
|
||||
|
||||
#define OPTIONS \
|
||||
"md4, md5, ripemd160, sha1, sha256, sha512,\n" \
|
||||
"arc4, des3, des, camellia, blowfish,\n" \
|
||||
"aes_cbc, aes_gcm, aes_ccm, aes_cmac, des3_cmac,\n" \
|
||||
"arc4, des3, des, camellia, blowfish, chacha20,\n" \
|
||||
"aes_cbc, aes_gcm, aes_ccm, aes_ctx, chachapoly,\n" \
|
||||
"aes_cmac, des3_cmac, poly1305\n" \
|
||||
"havege, ctr_drbg, hmac_drbg\n" \
|
||||
"rsa, dhm, ecdsa, ecdh.\n"
|
||||
|
||||
@@ -110,25 +119,34 @@ int main( void )
|
||||
#define TIME_AND_TSC( TITLE, CODE ) \
|
||||
do { \
|
||||
unsigned long ii, jj, tsc; \
|
||||
int ret = 0; \
|
||||
\
|
||||
mbedtls_printf( HEADER_FORMAT, TITLE ); \
|
||||
fflush( stdout ); \
|
||||
\
|
||||
mbedtls_set_alarm( 1 ); \
|
||||
for( ii = 1; ! mbedtls_timing_alarmed; ii++ ) \
|
||||
for( ii = 1; ret == 0 && ! mbedtls_timing_alarmed; ii++ ) \
|
||||
{ \
|
||||
CODE; \
|
||||
ret = CODE; \
|
||||
} \
|
||||
\
|
||||
tsc = mbedtls_timing_hardclock(); \
|
||||
for( jj = 0; jj < 1024; jj++ ) \
|
||||
for( jj = 0; ret == 0 && jj < 1024; jj++ ) \
|
||||
{ \
|
||||
CODE; \
|
||||
ret = CODE; \
|
||||
} \
|
||||
\
|
||||
mbedtls_printf( "%9lu KiB/s, %9lu cycles/byte\n", \
|
||||
ii * BUFSIZE / 1024, \
|
||||
( mbedtls_timing_hardclock() - tsc ) / ( jj * BUFSIZE ) ); \
|
||||
if( ret != 0 ) \
|
||||
{ \
|
||||
PRINT_ERROR; \
|
||||
} \
|
||||
else \
|
||||
{ \
|
||||
mbedtls_printf( "%9lu KiB/s, %9lu cycles/byte\n", \
|
||||
ii * BUFSIZE / 1024, \
|
||||
( mbedtls_timing_hardclock() - tsc ) \
|
||||
/ ( jj * BUFSIZE ) ); \
|
||||
} \
|
||||
} while( 0 )
|
||||
|
||||
#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && defined(MBEDTLS_MEMORY_DEBUG)
|
||||
@@ -228,8 +246,10 @@ unsigned char buf[BUFSIZE];
|
||||
typedef struct {
|
||||
char md4, md5, ripemd160, sha1, sha256, sha512,
|
||||
arc4, des3, des,
|
||||
aes_cbc, aes_gcm, aes_ccm, aes_cmac, des3_cmac,
|
||||
camellia, blowfish,
|
||||
aes_cbc, aes_gcm, aes_ccm, aes_xts, chachapoly,
|
||||
aes_cmac, des3_cmac,
|
||||
aria, camellia, blowfish, chacha20,
|
||||
poly1305,
|
||||
havege, ctr_drbg, hmac_drbg,
|
||||
rsa, dhm, ecdsa, ecdh;
|
||||
} todo_list;
|
||||
@@ -274,18 +294,28 @@ int main( int argc, char *argv[] )
|
||||
todo.des = 1;
|
||||
else if( strcmp( argv[i], "aes_cbc" ) == 0 )
|
||||
todo.aes_cbc = 1;
|
||||
else if( strcmp( argv[i], "aes_xts" ) == 0 )
|
||||
todo.aes_xts = 1;
|
||||
else if( strcmp( argv[i], "aes_gcm" ) == 0 )
|
||||
todo.aes_gcm = 1;
|
||||
else if( strcmp( argv[i], "aes_ccm" ) == 0 )
|
||||
todo.aes_ccm = 1;
|
||||
else if( strcmp( argv[i], "chachapoly" ) == 0 )
|
||||
todo.chachapoly = 1;
|
||||
else if( strcmp( argv[i], "aes_cmac" ) == 0 )
|
||||
todo.aes_cmac = 1;
|
||||
else if( strcmp( argv[i], "des3_cmac" ) == 0 )
|
||||
todo.des3_cmac = 1;
|
||||
else if( strcmp( argv[i], "aria" ) == 0 )
|
||||
todo.aria = 1;
|
||||
else if( strcmp( argv[i], "camellia" ) == 0 )
|
||||
todo.camellia = 1;
|
||||
else if( strcmp( argv[i], "blowfish" ) == 0 )
|
||||
todo.blowfish = 1;
|
||||
else if( strcmp( argv[i], "chacha20" ) == 0 )
|
||||
todo.chacha20 = 1;
|
||||
else if( strcmp( argv[i], "poly1305" ) == 0 )
|
||||
todo.poly1305 = 1;
|
||||
else if( strcmp( argv[i], "havege" ) == 0 )
|
||||
todo.havege = 1;
|
||||
else if( strcmp( argv[i], "ctr_drbg" ) == 0 )
|
||||
@@ -419,6 +449,29 @@ int main( int argc, char *argv[] )
|
||||
mbedtls_aes_free( &aes );
|
||||
}
|
||||
#endif
|
||||
#if defined(MBEDTLS_CIPHER_MODE_XTS)
|
||||
if( todo.aes_xts )
|
||||
{
|
||||
int keysize;
|
||||
mbedtls_aes_xts_context ctx;
|
||||
|
||||
mbedtls_aes_xts_init( &ctx );
|
||||
for( keysize = 128; keysize <= 256; keysize += 128 )
|
||||
{
|
||||
mbedtls_snprintf( title, sizeof( title ), "AES-XTS-%d", keysize );
|
||||
|
||||
memset( buf, 0, sizeof( buf ) );
|
||||
memset( tmp, 0, sizeof( tmp ) );
|
||||
mbedtls_aes_xts_setkey_enc( &ctx, tmp, keysize * 2 );
|
||||
|
||||
TIME_AND_TSC( title,
|
||||
mbedtls_aes_crypt_xts( &ctx, MBEDTLS_AES_ENCRYPT, BUFSIZE,
|
||||
tmp, buf, buf ) );
|
||||
|
||||
mbedtls_aes_xts_free( &ctx );
|
||||
}
|
||||
}
|
||||
#endif
|
||||
#if defined(MBEDTLS_GCM_C)
|
||||
if( todo.aes_gcm )
|
||||
{
|
||||
@@ -465,6 +518,26 @@ int main( int argc, char *argv[] )
|
||||
}
|
||||
}
|
||||
#endif
|
||||
#if defined(MBEDTLS_CHACHAPOLY_C)
|
||||
if( todo.chachapoly )
|
||||
{
|
||||
mbedtls_chachapoly_context chachapoly;
|
||||
|
||||
mbedtls_chachapoly_init( &chachapoly );
|
||||
memset( buf, 0, sizeof( buf ) );
|
||||
memset( tmp, 0, sizeof( tmp ) );
|
||||
|
||||
mbedtls_snprintf( title, sizeof( title ), "ChaCha20-Poly1305" );
|
||||
|
||||
mbedtls_chachapoly_setkey( &chachapoly, tmp );
|
||||
|
||||
TIME_AND_TSC( title,
|
||||
mbedtls_chachapoly_encrypt_and_tag( &chachapoly,
|
||||
BUFSIZE, tmp, NULL, 0, buf, buf, tmp ) );
|
||||
|
||||
mbedtls_chachapoly_free( &chachapoly );
|
||||
}
|
||||
#endif
|
||||
#if defined(MBEDTLS_CMAC_C)
|
||||
if( todo.aes_cmac )
|
||||
{
|
||||
@@ -498,6 +571,28 @@ int main( int argc, char *argv[] )
|
||||
#endif /* MBEDTLS_CMAC_C */
|
||||
#endif /* MBEDTLS_AES_C */
|
||||
|
||||
#if defined(MBEDTLS_ARIA_C) && defined(MBEDTLS_CIPHER_MODE_CBC)
|
||||
if( todo.aria )
|
||||
{
|
||||
int keysize;
|
||||
mbedtls_aria_context aria;
|
||||
mbedtls_aria_init( &aria );
|
||||
for( keysize = 128; keysize <= 256; keysize += 64 )
|
||||
{
|
||||
mbedtls_snprintf( title, sizeof( title ), "ARIA-CBC-%d", keysize );
|
||||
|
||||
memset( buf, 0, sizeof( buf ) );
|
||||
memset( tmp, 0, sizeof( tmp ) );
|
||||
mbedtls_aria_setkey_enc( &aria, tmp, keysize );
|
||||
|
||||
TIME_AND_TSC( title,
|
||||
mbedtls_aria_crypt_cbc( &aria, MBEDTLS_ARIA_ENCRYPT,
|
||||
BUFSIZE, tmp, buf, buf ) );
|
||||
}
|
||||
mbedtls_aria_free( &aria );
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_CAMELLIA_C) && defined(MBEDTLS_CIPHER_MODE_CBC)
|
||||
if( todo.camellia )
|
||||
{
|
||||
@@ -520,6 +615,20 @@ int main( int argc, char *argv[] )
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_CHACHA20_C)
|
||||
if ( todo.chacha20 )
|
||||
{
|
||||
TIME_AND_TSC( "ChaCha20", mbedtls_chacha20_crypt( buf, buf, 0U, BUFSIZE, buf, buf ) );
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_POLY1305_C)
|
||||
if ( todo.poly1305 )
|
||||
{
|
||||
TIME_AND_TSC( "Poly1305", mbedtls_poly1305_mac( buf, buf, BUFSIZE, buf ) );
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_BLOWFISH_C) && defined(MBEDTLS_CIPHER_MODE_CBC)
|
||||
if( todo.blowfish )
|
||||
{
|
||||
@@ -564,15 +673,13 @@ int main( int argc, char *argv[] )
|
||||
if( mbedtls_ctr_drbg_seed( &ctr_drbg, myrand, NULL, NULL, 0 ) != 0 )
|
||||
mbedtls_exit(1);
|
||||
TIME_AND_TSC( "CTR_DRBG (NOPR)",
|
||||
if( mbedtls_ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) != 0 )
|
||||
mbedtls_exit(1) );
|
||||
mbedtls_ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) );
|
||||
|
||||
if( mbedtls_ctr_drbg_seed( &ctr_drbg, myrand, NULL, NULL, 0 ) != 0 )
|
||||
mbedtls_exit(1);
|
||||
mbedtls_ctr_drbg_set_prediction_resistance( &ctr_drbg, MBEDTLS_CTR_DRBG_PR_ON );
|
||||
TIME_AND_TSC( "CTR_DRBG (PR)",
|
||||
if( mbedtls_ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) != 0 )
|
||||
mbedtls_exit(1) );
|
||||
mbedtls_ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) );
|
||||
mbedtls_ctr_drbg_free( &ctr_drbg );
|
||||
}
|
||||
#endif
|
||||
@@ -592,18 +699,14 @@ int main( int argc, char *argv[] )
|
||||
if( mbedtls_hmac_drbg_seed( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
|
||||
mbedtls_exit(1);
|
||||
TIME_AND_TSC( "HMAC_DRBG SHA-1 (NOPR)",
|
||||
if( mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 )
|
||||
mbedtls_exit(1) );
|
||||
mbedtls_hmac_drbg_free( &hmac_drbg );
|
||||
mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) );
|
||||
|
||||
if( mbedtls_hmac_drbg_seed( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
|
||||
mbedtls_exit(1);
|
||||
mbedtls_hmac_drbg_set_prediction_resistance( &hmac_drbg,
|
||||
MBEDTLS_HMAC_DRBG_PR_ON );
|
||||
TIME_AND_TSC( "HMAC_DRBG SHA-1 (PR)",
|
||||
if( mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 )
|
||||
mbedtls_exit(1) );
|
||||
mbedtls_hmac_drbg_free( &hmac_drbg );
|
||||
mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) );
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_SHA256_C)
|
||||
@@ -613,19 +716,16 @@ int main( int argc, char *argv[] )
|
||||
if( mbedtls_hmac_drbg_seed( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
|
||||
mbedtls_exit(1);
|
||||
TIME_AND_TSC( "HMAC_DRBG SHA-256 (NOPR)",
|
||||
if( mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 )
|
||||
mbedtls_exit(1) );
|
||||
mbedtls_hmac_drbg_free( &hmac_drbg );
|
||||
mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) );
|
||||
|
||||
if( mbedtls_hmac_drbg_seed( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
|
||||
mbedtls_exit(1);
|
||||
mbedtls_hmac_drbg_set_prediction_resistance( &hmac_drbg,
|
||||
MBEDTLS_HMAC_DRBG_PR_ON );
|
||||
TIME_AND_TSC( "HMAC_DRBG SHA-256 (PR)",
|
||||
if( mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 )
|
||||
mbedtls_exit(1) );
|
||||
mbedtls_hmac_drbg_free( &hmac_drbg );
|
||||
mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) );
|
||||
#endif
|
||||
mbedtls_hmac_drbg_free( &hmac_drbg );
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
119
programs/test/cpp_dummy_build.cpp
Normal file
119
programs/test/cpp_dummy_build.cpp
Normal file
@@ -0,0 +1,119 @@
|
||||
/*
|
||||
* This program is a dummy C++ program to ensure Mbed TLS library header files
|
||||
* can be included and built with a C++ compiler.
|
||||
*
|
||||
* Copyright (C) 2018, ARM Limited, All Rights Reserved
|
||||
* SPDX-License-Identifier: Apache-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.
|
||||
*
|
||||
* This file is part of mbed TLS (https://tls.mbed.org)
|
||||
*/
|
||||
|
||||
#if !defined(MBEDTLS_CONFIG_FILE)
|
||||
#include "mbedtls/config.h"
|
||||
#else
|
||||
#include MBEDTLS_CONFIG_FILE
|
||||
#endif
|
||||
|
||||
#include "mbedtls/aes.h"
|
||||
#include "mbedtls/aesni.h"
|
||||
#include "mbedtls/arc4.h"
|
||||
#include "mbedtls/aria.h"
|
||||
#include "mbedtls/asn1.h"
|
||||
#include "mbedtls/asn1write.h"
|
||||
#include "mbedtls/base64.h"
|
||||
#include "mbedtls/bignum.h"
|
||||
#include "mbedtls/blowfish.h"
|
||||
#include "mbedtls/bn_mul.h"
|
||||
#include "mbedtls/camellia.h"
|
||||
#include "mbedtls/ccm.h"
|
||||
#include "mbedtls/certs.h"
|
||||
#include "mbedtls/chacha20.h"
|
||||
#include "mbedtls/chachapoly.h"
|
||||
#include "mbedtls/check_config.h"
|
||||
#include "mbedtls/cipher.h"
|
||||
#include "mbedtls/cipher_internal.h"
|
||||
#include "mbedtls/cmac.h"
|
||||
#include "mbedtls/compat-1.3.h"
|
||||
#include "mbedtls/ctr_drbg.h"
|
||||
#include "mbedtls/debug.h"
|
||||
#include "mbedtls/des.h"
|
||||
#include "mbedtls/dhm.h"
|
||||
#include "mbedtls/ecdh.h"
|
||||
#include "mbedtls/ecdsa.h"
|
||||
#include "mbedtls/ecjpake.h"
|
||||
#include "mbedtls/ecp.h"
|
||||
#include "mbedtls/ecp_internal.h"
|
||||
#include "mbedtls/entropy.h"
|
||||
#include "mbedtls/entropy_poll.h"
|
||||
#include "mbedtls/error.h"
|
||||
#include "mbedtls/gcm.h"
|
||||
#include "mbedtls/havege.h"
|
||||
#include "mbedtls/hkdf.h"
|
||||
#include "mbedtls/hmac_drbg.h"
|
||||
#include "mbedtls/md.h"
|
||||
#include "mbedtls/md2.h"
|
||||
#include "mbedtls/md4.h"
|
||||
#include "mbedtls/md5.h"
|
||||
#include "mbedtls/md_internal.h"
|
||||
#include "mbedtls/net.h"
|
||||
#include "mbedtls/net_sockets.h"
|
||||
#include "mbedtls/nist_kw.h"
|
||||
#include "mbedtls/oid.h"
|
||||
#include "mbedtls/padlock.h"
|
||||
#include "mbedtls/pem.h"
|
||||
#include "mbedtls/pk.h"
|
||||
#include "mbedtls/pk_internal.h"
|
||||
#include "mbedtls/pkcs11.h"
|
||||
#include "mbedtls/pkcs12.h"
|
||||
#include "mbedtls/pkcs5.h"
|
||||
#include "mbedtls/platform_time.h"
|
||||
#include "mbedtls/platform_util.h"
|
||||
#include "mbedtls/poly1305.h"
|
||||
#include "mbedtls/ripemd160.h"
|
||||
#include "mbedtls/rsa.h"
|
||||
#include "mbedtls/rsa_internal.h"
|
||||
#include "mbedtls/sha1.h"
|
||||
#include "mbedtls/sha256.h"
|
||||
#include "mbedtls/sha512.h"
|
||||
#include "mbedtls/ssl.h"
|
||||
#include "mbedtls/ssl_cache.h"
|
||||
#include "mbedtls/ssl_ciphersuites.h"
|
||||
#include "mbedtls/ssl_cookie.h"
|
||||
#include "mbedtls/ssl_internal.h"
|
||||
#include "mbedtls/ssl_ticket.h"
|
||||
#include "mbedtls/threading.h"
|
||||
#include "mbedtls/timing.h"
|
||||
#include "mbedtls/version.h"
|
||||
#include "mbedtls/x509.h"
|
||||
#include "mbedtls/x509_crl.h"
|
||||
#include "mbedtls/x509_crt.h"
|
||||
#include "mbedtls/x509_csr.h"
|
||||
#include "mbedtls/xtea.h"
|
||||
|
||||
#if defined(MBEDTLS_PLATFORM_C)
|
||||
#include "mbedtls/platform.h"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
|
||||
#include "mbedtls/memory_buffer_alloc.h"
|
||||
#endif
|
||||
|
||||
int main()
|
||||
{
|
||||
mbedtls_platform_context *ctx = NULL;
|
||||
mbedtls_platform_setup(ctx);
|
||||
mbedtls_printf("CPP Build test\n");
|
||||
mbedtls_platform_teardown(ctx);
|
||||
}
|
||||
@@ -44,6 +44,10 @@
|
||||
#include "mbedtls/des.h"
|
||||
#include "mbedtls/aes.h"
|
||||
#include "mbedtls/camellia.h"
|
||||
#include "mbedtls/aria.h"
|
||||
#include "mbedtls/chacha20.h"
|
||||
#include "mbedtls/poly1305.h"
|
||||
#include "mbedtls/chachapoly.h"
|
||||
#include "mbedtls/base64.h"
|
||||
#include "mbedtls/bignum.h"
|
||||
#include "mbedtls/rsa.h"
|
||||
@@ -53,6 +57,7 @@
|
||||
#include "mbedtls/ecp.h"
|
||||
#include "mbedtls/ecjpake.h"
|
||||
#include "mbedtls/timing.h"
|
||||
#include "mbedtls/nist_kw.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
@@ -204,9 +209,21 @@ const selftest_t selftests[] =
|
||||
#if defined(MBEDTLS_CCM_C) && defined(MBEDTLS_AES_C)
|
||||
{"ccm", mbedtls_ccm_self_test},
|
||||
#endif
|
||||
#if defined(MBEDTLS_NIST_KW_C) && defined(MBEDTLS_AES_C)
|
||||
{"nist_kw", mbedtls_nist_kw_self_test},
|
||||
#endif
|
||||
#if defined(MBEDTLS_CMAC_C)
|
||||
{"cmac", mbedtls_cmac_self_test},
|
||||
#endif
|
||||
#if defined(MBEDTLS_CHACHA20_C)
|
||||
{"chacha20", mbedtls_chacha20_self_test},
|
||||
#endif
|
||||
#if defined(MBEDTLS_POLY1305_C)
|
||||
{"poly1305", mbedtls_poly1305_self_test},
|
||||
#endif
|
||||
#if defined(MBEDTLS_CHACHAPOLY_C)
|
||||
{"chacha20-poly1305", mbedtls_chachapoly_self_test},
|
||||
#endif
|
||||
#if defined(MBEDTLS_BASE64_C)
|
||||
{"base64", mbedtls_base64_self_test},
|
||||
#endif
|
||||
@@ -225,6 +242,9 @@ const selftest_t selftests[] =
|
||||
#if defined(MBEDTLS_CAMELLIA_C)
|
||||
{"camellia", mbedtls_camellia_self_test},
|
||||
#endif
|
||||
#if defined(MBEDTLS_ARIA_C)
|
||||
{"aria", mbedtls_aria_self_test},
|
||||
#endif
|
||||
#if defined(MBEDTLS_CTR_DRBG_C)
|
||||
{"ctr_drbg", mbedtls_ctr_drbg_self_test},
|
||||
#endif
|
||||
|
||||
@@ -29,9 +29,12 @@
|
||||
#include "mbedtls/platform.h"
|
||||
#else
|
||||
#include <stdio.h>
|
||||
#define mbedtls_snprintf snprintf
|
||||
#define mbedtls_printf printf
|
||||
#endif
|
||||
#include <stdlib.h>
|
||||
#define mbedtls_snprintf snprintf
|
||||
#define mbedtls_printf printf
|
||||
#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
|
||||
#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
|
||||
#endif /* MBEDTLS_PLATFORM_C */
|
||||
|
||||
#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_X509_CRT_PARSE_C) && \
|
||||
defined(MBEDTLS_FS_IO) && defined(MBEDTLS_X509_CRL_PARSE_C)
|
||||
@@ -80,7 +83,8 @@ const char *client_private_keys[MAX_CLIENT_CERTS] =
|
||||
|
||||
int main( void )
|
||||
{
|
||||
int ret, i;
|
||||
int ret = 1, i;
|
||||
int exit_code = MBEDTLS_EXIT_FAILURE;
|
||||
mbedtls_x509_crt cacert;
|
||||
mbedtls_x509_crl crl;
|
||||
char buf[10240];
|
||||
@@ -210,7 +214,6 @@ int main( void )
|
||||
if( ! mbedtls_pk_can_do( &clicert.pk, MBEDTLS_PK_RSA ) )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! certificate's key is not RSA\n\n" );
|
||||
ret = MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
|
||||
goto exit;
|
||||
}
|
||||
|
||||
@@ -241,6 +244,8 @@ int main( void )
|
||||
mbedtls_pk_free( &pk );
|
||||
}
|
||||
|
||||
exit_code = MBEDTLS_EXIT_SUCCESS;
|
||||
|
||||
exit:
|
||||
mbedtls_x509_crt_free( &cacert );
|
||||
mbedtls_x509_crl_free( &crl );
|
||||
@@ -250,7 +255,7 @@ exit:
|
||||
fflush( stdout ); getchar();
|
||||
#endif
|
||||
|
||||
return( ret );
|
||||
return( exit_code );
|
||||
}
|
||||
#endif /* MBEDTLS_RSA_C && MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_FS_IO &&
|
||||
MBEDTLS_X509_CRL_PARSE_C */
|
||||
|
||||
@@ -37,10 +37,14 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#define mbedtls_time time
|
||||
#define mbedtls_time_t time_t
|
||||
#define mbedtls_printf printf
|
||||
#endif
|
||||
#define mbedtls_time time
|
||||
#define mbedtls_time_t time_t
|
||||
#define mbedtls_printf printf
|
||||
#define mbedtls_calloc calloc
|
||||
#define mbedtls_free free
|
||||
#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
|
||||
#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
|
||||
#endif /* MBEDTLS_PLATFORM_C */
|
||||
|
||||
#if !defined(MBEDTLS_NET_C)
|
||||
int main( void )
|
||||
@@ -104,6 +108,21 @@ int main( void )
|
||||
" delay=%%d default: 0 (no delayed packets)\n" \
|
||||
" delay about 1:N packets randomly\n" \
|
||||
" delay_ccs=0/1 default: 0 (don't delay ChangeCipherSpec)\n" \
|
||||
" delay_cli=%%s Handshake message from client that should be\n"\
|
||||
" delayed. Possible values are 'ClientHello',\n" \
|
||||
" 'Certificate', 'CertificateVerify', and\n" \
|
||||
" 'ClientKeyExchange'.\n" \
|
||||
" May be used multiple times, even for the same\n"\
|
||||
" message, in which case the respective message\n"\
|
||||
" gets delayed multiple times.\n" \
|
||||
" delay_srv=%%s Handshake message from server that should be\n"\
|
||||
" delayed. Possible values are 'HelloRequest',\n"\
|
||||
" 'ServerHello', 'ServerHelloDone', 'Certificate'\n"\
|
||||
" 'ServerKeyExchange', 'NewSessionTicket',\n"\
|
||||
" 'HelloVerifyRequest' and ''CertificateRequest'.\n"\
|
||||
" May be used multiple times, even for the same\n"\
|
||||
" message, in which case the respective message\n"\
|
||||
" gets delayed multiple times.\n" \
|
||||
" drop=%%d default: 0 (no dropped packets)\n" \
|
||||
" drop about 1:N packets randomly\n" \
|
||||
" mtu=%%d default: 0 (unlimited)\n" \
|
||||
@@ -119,6 +138,9 @@ int main( void )
|
||||
/*
|
||||
* global options
|
||||
*/
|
||||
|
||||
#define MAX_DELAYED_HS 10
|
||||
|
||||
static struct options
|
||||
{
|
||||
const char *server_addr; /* address to forward packets to */
|
||||
@@ -129,6 +151,12 @@ static struct options
|
||||
int duplicate; /* duplicate 1 in N packets (none if 0) */
|
||||
int delay; /* delay 1 packet in N (none if 0) */
|
||||
int delay_ccs; /* delay ChangeCipherSpec */
|
||||
char* delay_cli[MAX_DELAYED_HS]; /* handshake types of messages from
|
||||
* client that should be delayed. */
|
||||
uint8_t delay_cli_cnt; /* Number of entries in delay_cli. */
|
||||
char* delay_srv[MAX_DELAYED_HS]; /* handshake types of messages from
|
||||
* server that should be delayed. */
|
||||
uint8_t delay_srv_cnt; /* Number of entries in delay_srv. */
|
||||
int drop; /* drop 1 packet in N (none if 0) */
|
||||
int mtu; /* drop packets larger than this */
|
||||
int bad_ad; /* inject corrupted ApplicationData record */
|
||||
@@ -162,6 +190,11 @@ static void get_options( int argc, char *argv[] )
|
||||
opt.pack = DFL_PACK;
|
||||
/* Other members default to 0 */
|
||||
|
||||
opt.delay_cli_cnt = 0;
|
||||
opt.delay_srv_cnt = 0;
|
||||
memset( opt.delay_cli, 0, sizeof( opt.delay_cli ) );
|
||||
memset( opt.delay_srv, 0, sizeof( opt.delay_srv ) );
|
||||
|
||||
for( i = 1; i < argc; i++ )
|
||||
{
|
||||
p = argv[i];
|
||||
@@ -195,6 +228,43 @@ static void get_options( int argc, char *argv[] )
|
||||
if( opt.delay_ccs < 0 || opt.delay_ccs > 1 )
|
||||
exit_usage( p, q );
|
||||
}
|
||||
else if( strcmp( p, "delay_cli" ) == 0 ||
|
||||
strcmp( p, "delay_srv" ) == 0 )
|
||||
{
|
||||
uint8_t *delay_cnt;
|
||||
char **delay_list;
|
||||
size_t len;
|
||||
char *buf;
|
||||
|
||||
if( strcmp( p, "delay_cli" ) == 0 )
|
||||
{
|
||||
delay_cnt = &opt.delay_cli_cnt;
|
||||
delay_list = opt.delay_cli;
|
||||
}
|
||||
else
|
||||
{
|
||||
delay_cnt = &opt.delay_srv_cnt;
|
||||
delay_list = opt.delay_srv;
|
||||
}
|
||||
|
||||
if( *delay_cnt == MAX_DELAYED_HS )
|
||||
{
|
||||
mbedtls_printf( " too many uses of %s: only %d allowed\n",
|
||||
p, MAX_DELAYED_HS );
|
||||
exit_usage( p, NULL );
|
||||
}
|
||||
|
||||
len = strlen( q );
|
||||
buf = mbedtls_calloc( 1, len + 1 );
|
||||
if( buf == NULL )
|
||||
{
|
||||
mbedtls_printf( " Allocation failure\n" );
|
||||
exit( 1 );
|
||||
}
|
||||
memcpy( buf, q, len + 1 );
|
||||
|
||||
delay_list[ (*delay_cnt)++ ] = buf;
|
||||
}
|
||||
else if( strcmp( p, "drop" ) == 0 )
|
||||
{
|
||||
opt.drop = atoi( q );
|
||||
@@ -486,11 +556,37 @@ int send_packet( const packet *p, const char *why )
|
||||
return( 0 );
|
||||
}
|
||||
|
||||
static packet prev;
|
||||
#define MAX_DELAYED_MSG 5
|
||||
static size_t prev_len;
|
||||
static packet prev[MAX_DELAYED_MSG];
|
||||
|
||||
void clear_pending( void )
|
||||
{
|
||||
memset( &prev, 0, sizeof( packet ) );
|
||||
memset( &prev, 0, sizeof( prev ) );
|
||||
prev_len = 0;
|
||||
}
|
||||
|
||||
void delay_packet( packet *delay )
|
||||
{
|
||||
if( prev_len == MAX_DELAYED_MSG )
|
||||
return;
|
||||
|
||||
memcpy( &prev[prev_len++], delay, sizeof( packet ) );
|
||||
}
|
||||
|
||||
int send_delayed()
|
||||
{
|
||||
uint8_t offset;
|
||||
int ret;
|
||||
for( offset = 0; offset < prev_len; offset++ )
|
||||
{
|
||||
ret = send_packet( &prev[offset], "delayed" );
|
||||
if( ret != 0 )
|
||||
return( ret );
|
||||
}
|
||||
|
||||
clear_pending();
|
||||
return( 0 );
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -538,6 +634,10 @@ int handle_message( const char *way,
|
||||
packet cur;
|
||||
size_t id;
|
||||
|
||||
uint8_t delay_idx;
|
||||
char ** delay_list;
|
||||
uint8_t delay_list_len;
|
||||
|
||||
/* receive packet */
|
||||
if( ( ret = mbedtls_net_recv( src, cur.buf, sizeof( cur.buf ) ) ) <= 0 )
|
||||
{
|
||||
@@ -553,6 +653,37 @@ int handle_message( const char *way,
|
||||
|
||||
id = cur.len % sizeof( dropped );
|
||||
|
||||
if( strcmp( way, "S <- C" ) == 0 )
|
||||
{
|
||||
delay_list = opt.delay_cli;
|
||||
delay_list_len = opt.delay_cli_cnt;
|
||||
}
|
||||
else
|
||||
{
|
||||
delay_list = opt.delay_srv;
|
||||
delay_list_len = opt.delay_srv_cnt;
|
||||
}
|
||||
|
||||
/* Check if message type is in the list of messages
|
||||
* that should be delayed */
|
||||
for( delay_idx = 0; delay_idx < delay_list_len; delay_idx++ )
|
||||
{
|
||||
if( delay_list[ delay_idx ] == NULL )
|
||||
continue;
|
||||
|
||||
if( strcmp( delay_list[ delay_idx ], cur.type ) == 0 )
|
||||
{
|
||||
/* Delay message */
|
||||
delay_packet( &cur );
|
||||
|
||||
/* Remove entry from list */
|
||||
mbedtls_free( delay_list[delay_idx] );
|
||||
delay_list[delay_idx] = NULL;
|
||||
|
||||
return( 0 );
|
||||
}
|
||||
}
|
||||
|
||||
/* do we want to drop, delay, or forward it? */
|
||||
if( ( opt.mtu != 0 &&
|
||||
cur.len > (unsigned) opt.mtu ) ||
|
||||
@@ -572,12 +703,11 @@ int handle_message( const char *way,
|
||||
strcmp( cur.type, "ApplicationData" ) != 0 &&
|
||||
! ( opt.protect_hvr &&
|
||||
strcmp( cur.type, "HelloVerifyRequest" ) == 0 ) &&
|
||||
prev.dst == NULL &&
|
||||
cur.len != (size_t) opt.protect_len &&
|
||||
dropped[id] < DROP_MAX &&
|
||||
rand() % opt.delay == 0 ) )
|
||||
{
|
||||
memcpy( &prev, &cur, sizeof( packet ) );
|
||||
delay_packet( &cur );
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -585,14 +715,10 @@ int handle_message( const char *way,
|
||||
if( ( ret = send_packet( &cur, "forwarded" ) ) != 0 )
|
||||
return( ret );
|
||||
|
||||
/* send previously delayed message if any */
|
||||
if( prev.dst != NULL )
|
||||
{
|
||||
ret = send_packet( &prev, "delayed" );
|
||||
memset( &prev, 0, sizeof( packet ) );
|
||||
if( ret != 0 )
|
||||
return( ret );
|
||||
}
|
||||
/* send previously delayed messages if any */
|
||||
ret = send_delayed();
|
||||
if( ret != 0 )
|
||||
return( ret );
|
||||
}
|
||||
|
||||
return( 0 );
|
||||
@@ -600,7 +726,9 @@ int handle_message( const char *way,
|
||||
|
||||
int main( int argc, char *argv[] )
|
||||
{
|
||||
int ret;
|
||||
int ret = 1;
|
||||
int exit_code = MBEDTLS_EXIT_FAILURE;
|
||||
uint8_t delay_idx;
|
||||
|
||||
mbedtls_net_context listen_fd, client_fd, server_fd;
|
||||
|
||||
@@ -781,10 +909,12 @@ accept:
|
||||
|
||||
}
|
||||
|
||||
exit_code = MBEDTLS_EXIT_SUCCESS;
|
||||
|
||||
exit:
|
||||
|
||||
#ifdef MBEDTLS_ERROR_C
|
||||
if( ret != 0 )
|
||||
if( exit_code != MBEDTLS_EXIT_SUCCESS )
|
||||
{
|
||||
char error_buf[100];
|
||||
mbedtls_strerror( ret, error_buf, 100 );
|
||||
@@ -793,6 +923,12 @@ exit:
|
||||
}
|
||||
#endif
|
||||
|
||||
for( delay_idx = 0; delay_idx < MAX_DELAYED_HS; delay_idx++ )
|
||||
{
|
||||
mbedtls_free( opt.delay_cli + delay_idx );
|
||||
mbedtls_free( opt.delay_srv + delay_idx );
|
||||
}
|
||||
|
||||
mbedtls_net_free( &client_fd );
|
||||
mbedtls_net_free( &server_fd );
|
||||
mbedtls_net_free( &listen_fd );
|
||||
@@ -802,7 +938,7 @@ exit:
|
||||
fflush( stdout ); getchar();
|
||||
#endif
|
||||
|
||||
return( ret != 0 );
|
||||
return( exit_code );
|
||||
}
|
||||
|
||||
#endif /* MBEDTLS_NET_C */
|
||||
|
||||
@@ -66,7 +66,7 @@ int main( int argc, char** argv )
|
||||
char buf[BUFFER_LEN];
|
||||
char *p = buf;
|
||||
char *end = p + BUFFER_LEN;
|
||||
char c;
|
||||
int c;
|
||||
|
||||
if( argc != 2 )
|
||||
{
|
||||
@@ -83,7 +83,7 @@ int main( int argc, char** argv )
|
||||
}
|
||||
|
||||
while( ( c = fgetc( fp ) ) != EOF && p < end - 1 )
|
||||
*p++ = c;
|
||||
*p++ = (char)c;
|
||||
*p = '\0';
|
||||
|
||||
if( p - buf != 0 )
|
||||
|
||||
@@ -29,10 +29,13 @@
|
||||
#include "mbedtls/platform.h"
|
||||
#else
|
||||
#include <stdio.h>
|
||||
#define mbedtls_free free
|
||||
#define mbedtls_calloc calloc
|
||||
#define mbedtls_printf printf
|
||||
#endif
|
||||
#include <stdlib.h>
|
||||
#define mbedtls_free free
|
||||
#define mbedtls_calloc calloc
|
||||
#define mbedtls_printf printf
|
||||
#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
|
||||
#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
|
||||
#endif /* MBEDTLS_PLATFORM_C */
|
||||
|
||||
#if defined(MBEDTLS_BASE64_C) && defined(MBEDTLS_FS_IO)
|
||||
#include "mbedtls/error.h"
|
||||
@@ -178,7 +181,8 @@ static int write_file( const char *path, unsigned char *buf, size_t n )
|
||||
|
||||
int main( int argc, char *argv[] )
|
||||
{
|
||||
int ret = 0;
|
||||
int ret = 1;
|
||||
int exit_code = MBEDTLS_EXIT_FAILURE;
|
||||
unsigned char *pem_buffer = NULL;
|
||||
unsigned char der_buffer[4096];
|
||||
char buf[1024];
|
||||
@@ -273,6 +277,8 @@ int main( int argc, char *argv[] )
|
||||
|
||||
mbedtls_printf( " ok\n" );
|
||||
|
||||
exit_code = MBEDTLS_EXIT_SUCCESS;
|
||||
|
||||
exit:
|
||||
free( pem_buffer );
|
||||
|
||||
@@ -281,6 +287,6 @@ exit:
|
||||
fflush( stdout ); getchar();
|
||||
#endif
|
||||
|
||||
return( ret );
|
||||
return( exit_code );
|
||||
}
|
||||
#endif /* MBEDTLS_BASE64_C && MBEDTLS_FS_IO */
|
||||
|
||||
@@ -30,11 +30,13 @@
|
||||
#else
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#define mbedtls_time time
|
||||
#define mbedtls_time_t time_t
|
||||
#define mbedtls_fprintf fprintf
|
||||
#define mbedtls_printf printf
|
||||
#endif
|
||||
#define mbedtls_time time
|
||||
#define mbedtls_time_t time_t
|
||||
#define mbedtls_fprintf fprintf
|
||||
#define mbedtls_printf printf
|
||||
#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
|
||||
#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
|
||||
#endif /* MBEDTLS_PLATFORM_C */
|
||||
|
||||
#if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_ENTROPY_C) || \
|
||||
!defined(MBEDTLS_SSL_TLS_C) || !defined(MBEDTLS_SSL_CLI_C) || \
|
||||
@@ -145,7 +147,8 @@ static int my_verify( void *data, mbedtls_x509_crt *crt, int depth, uint32_t *fl
|
||||
|
||||
int main( int argc, char *argv[] )
|
||||
{
|
||||
int ret = 0;
|
||||
int ret = 1;
|
||||
int exit_code = MBEDTLS_EXIT_FAILURE;
|
||||
mbedtls_net_context server_fd;
|
||||
unsigned char buf[1024];
|
||||
mbedtls_entropy_context entropy;
|
||||
@@ -180,7 +183,6 @@ int main( int argc, char *argv[] )
|
||||
{
|
||||
usage:
|
||||
mbedtls_printf( USAGE );
|
||||
ret = 2;
|
||||
goto exit;
|
||||
}
|
||||
|
||||
@@ -252,19 +254,23 @@ int main( int argc, char *argv[] )
|
||||
|
||||
if( strlen( opt.ca_path ) )
|
||||
{
|
||||
ret = mbedtls_x509_crt_parse_path( &cacert, opt.ca_path );
|
||||
if( ( ret = mbedtls_x509_crt_parse_path( &cacert, opt.ca_path ) ) < 0 )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse_path returned -0x%x\n\n", -ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
verify = 1;
|
||||
}
|
||||
else if( strlen( opt.ca_file ) )
|
||||
{
|
||||
ret = mbedtls_x509_crt_parse_file( &cacert, opt.ca_file );
|
||||
verify = 1;
|
||||
}
|
||||
if( ( ret = mbedtls_x509_crt_parse_file( &cacert, opt.ca_file ) ) < 0 )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse_file returned -0x%x\n\n", -ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if( ret < 0 )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n", -ret );
|
||||
goto exit;
|
||||
verify = 1;
|
||||
}
|
||||
|
||||
mbedtls_printf( " ok (%d skipped)\n", ret );
|
||||
@@ -332,8 +338,6 @@ int main( int argc, char *argv[] )
|
||||
cur = cur->next;
|
||||
}
|
||||
|
||||
ret = 0;
|
||||
|
||||
/*
|
||||
* 1.3 Verify the certificate
|
||||
*/
|
||||
@@ -470,6 +474,8 @@ ssl_exit:
|
||||
else
|
||||
goto usage;
|
||||
|
||||
exit_code = MBEDTLS_EXIT_SUCCESS;
|
||||
|
||||
exit:
|
||||
|
||||
mbedtls_net_free( &server_fd );
|
||||
@@ -485,10 +491,7 @@ exit:
|
||||
fflush( stdout ); getchar();
|
||||
#endif
|
||||
|
||||
if( ret < 0 )
|
||||
ret = 1;
|
||||
|
||||
return( ret );
|
||||
return( exit_code );
|
||||
}
|
||||
#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_SSL_TLS_C &&
|
||||
MBEDTLS_SSL_CLI_C && MBEDTLS_NET_C && MBEDTLS_RSA_C &&
|
||||
|
||||
@@ -29,8 +29,11 @@
|
||||
#include "mbedtls/platform.h"
|
||||
#else
|
||||
#include <stdio.h>
|
||||
#define mbedtls_printf printf
|
||||
#endif
|
||||
#include <stdlib.h>
|
||||
#define mbedtls_printf printf
|
||||
#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
|
||||
#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
|
||||
#endif /* MBEDTLS_PLATFORM_C */
|
||||
|
||||
#if !defined(MBEDTLS_X509_CSR_WRITE_C) || !defined(MBEDTLS_FS_IO) || \
|
||||
!defined(MBEDTLS_PK_PARSE_C) || !defined(MBEDTLS_SHA256_C) || \
|
||||
@@ -143,7 +146,8 @@ int write_certificate_request( mbedtls_x509write_csr *req, const char *output_fi
|
||||
|
||||
int main( int argc, char *argv[] )
|
||||
{
|
||||
int ret = 0;
|
||||
int ret = 1;
|
||||
int exit_code = MBEDTLS_EXIT_FAILURE;
|
||||
mbedtls_pk_context key;
|
||||
char buf[1024];
|
||||
int i;
|
||||
@@ -165,7 +169,6 @@ int main( int argc, char *argv[] )
|
||||
{
|
||||
usage:
|
||||
mbedtls_printf( USAGE );
|
||||
ret = 1;
|
||||
goto exit;
|
||||
}
|
||||
|
||||
@@ -380,9 +383,11 @@ int main( int argc, char *argv[] )
|
||||
|
||||
mbedtls_printf( " ok\n" );
|
||||
|
||||
exit_code = MBEDTLS_EXIT_SUCCESS;
|
||||
|
||||
exit:
|
||||
|
||||
if( ret != 0 && ret != 1)
|
||||
if( exit_code != MBEDTLS_EXIT_SUCCESS )
|
||||
{
|
||||
#ifdef MBEDTLS_ERROR_C
|
||||
mbedtls_strerror( ret, buf, sizeof( buf ) );
|
||||
@@ -402,7 +407,7 @@ exit:
|
||||
fflush( stdout ); getchar();
|
||||
#endif
|
||||
|
||||
return( ret );
|
||||
return( exit_code );
|
||||
}
|
||||
#endif /* MBEDTLS_X509_CSR_WRITE_C && MBEDTLS_PK_PARSE_C && MBEDTLS_FS_IO &&
|
||||
MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C && MBEDTLS_PEM_WRITE_C */
|
||||
|
||||
@@ -29,8 +29,11 @@
|
||||
#include "mbedtls/platform.h"
|
||||
#else
|
||||
#include <stdio.h>
|
||||
#define mbedtls_printf printf
|
||||
#endif
|
||||
#include <stdlib.h>
|
||||
#define mbedtls_printf printf
|
||||
#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
|
||||
#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
|
||||
#endif /* MBEDTLS_PLATFORM_C */
|
||||
|
||||
#if !defined(MBEDTLS_X509_CRT_WRITE_C) || \
|
||||
!defined(MBEDTLS_X509_CRT_PARSE_C) || !defined(MBEDTLS_FS_IO) || \
|
||||
@@ -161,7 +164,7 @@ struct options
|
||||
const char *issuer_key; /* filename of the issuer key file */
|
||||
const char *subject_pwd; /* password for the subject key file */
|
||||
const char *issuer_pwd; /* password for the issuer key file */
|
||||
const char *output_file; /* where to store the constructed key file */
|
||||
const char *output_file; /* where to store the constructed CRT */
|
||||
const char *subject_name; /* subject name for certificate */
|
||||
const char *issuer_name; /* issuer name for certificate */
|
||||
const char *not_before; /* validity period not before */
|
||||
@@ -211,7 +214,8 @@ int write_certificate( mbedtls_x509write_cert *crt, const char *output_file,
|
||||
|
||||
int main( int argc, char *argv[] )
|
||||
{
|
||||
int ret = 0;
|
||||
int ret = 1;
|
||||
int exit_code = MBEDTLS_EXIT_FAILURE;
|
||||
mbedtls_x509_crt issuer_crt;
|
||||
mbedtls_pk_context loaded_issuer_key, loaded_subject_key;
|
||||
mbedtls_pk_context *issuer_key = &loaded_issuer_key,
|
||||
@@ -238,6 +242,7 @@ int main( int argc, char *argv[] )
|
||||
mbedtls_pk_init( &loaded_subject_key );
|
||||
mbedtls_mpi_init( &serial );
|
||||
mbedtls_ctr_drbg_init( &ctr_drbg );
|
||||
mbedtls_entropy_init( &entropy );
|
||||
#if defined(MBEDTLS_X509_CSR_PARSE_C)
|
||||
mbedtls_x509_csr_init( &csr );
|
||||
#endif
|
||||
@@ -248,7 +253,6 @@ int main( int argc, char *argv[] )
|
||||
{
|
||||
usage:
|
||||
mbedtls_printf( USAGE );
|
||||
ret = 1;
|
||||
goto exit;
|
||||
}
|
||||
|
||||
@@ -472,7 +476,6 @@ int main( int argc, char *argv[] )
|
||||
mbedtls_printf( " . Seeding the random number generator..." );
|
||||
fflush( stdout );
|
||||
|
||||
mbedtls_entropy_init( &entropy );
|
||||
if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
|
||||
(const unsigned char *) pers,
|
||||
strlen( pers ) ) ) != 0 )
|
||||
@@ -607,15 +610,10 @@ int main( int argc, char *argv[] )
|
||||
//
|
||||
if( strlen( opt.issuer_crt ) )
|
||||
{
|
||||
if( !mbedtls_pk_can_do( &issuer_crt.pk, MBEDTLS_PK_RSA ) ||
|
||||
mbedtls_mpi_cmp_mpi( &mbedtls_pk_rsa( issuer_crt.pk )->N,
|
||||
&mbedtls_pk_rsa( *issuer_key )->N ) != 0 ||
|
||||
mbedtls_mpi_cmp_mpi( &mbedtls_pk_rsa( issuer_crt.pk )->E,
|
||||
&mbedtls_pk_rsa( *issuer_key )->E ) != 0 )
|
||||
if( mbedtls_pk_check_pair( &issuer_crt.pk, issuer_key ) != 0 )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! issuer_key does not match "
|
||||
"issuer certificate\n\n" );
|
||||
ret = -1;
|
||||
goto exit;
|
||||
}
|
||||
}
|
||||
@@ -772,7 +770,7 @@ int main( int argc, char *argv[] )
|
||||
}
|
||||
|
||||
/*
|
||||
* 1.2. Writing the request
|
||||
* 1.2. Writing the certificate
|
||||
*/
|
||||
mbedtls_printf( " . Writing the certificate..." );
|
||||
fflush( stdout );
|
||||
@@ -788,7 +786,13 @@ int main( int argc, char *argv[] )
|
||||
|
||||
mbedtls_printf( " ok\n" );
|
||||
|
||||
exit_code = MBEDTLS_EXIT_SUCCESS;
|
||||
|
||||
exit:
|
||||
#if defined(MBEDTLS_X509_CSR_PARSE_C)
|
||||
mbedtls_x509_csr_free( &csr );
|
||||
#endif /* MBEDTLS_X509_CSR_PARSE_C */
|
||||
mbedtls_x509_crt_free( &issuer_crt );
|
||||
mbedtls_x509write_crt_free( &crt );
|
||||
mbedtls_pk_free( &loaded_subject_key );
|
||||
mbedtls_pk_free( &loaded_issuer_key );
|
||||
@@ -801,7 +805,7 @@ exit:
|
||||
fflush( stdout ); getchar();
|
||||
#endif
|
||||
|
||||
return( ret );
|
||||
return( exit_code );
|
||||
}
|
||||
#endif /* MBEDTLS_X509_CRT_WRITE_C && MBEDTLS_X509_CRT_PARSE_C &&
|
||||
MBEDTLS_FS_IO && MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C &&
|
||||
|
||||
@@ -29,8 +29,11 @@
|
||||
#include "mbedtls/platform.h"
|
||||
#else
|
||||
#include <stdio.h>
|
||||
#define mbedtls_printf printf
|
||||
#endif
|
||||
#include <stdlib.h>
|
||||
#define mbedtls_printf printf
|
||||
#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
|
||||
#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
|
||||
#endif /* MBEDTLS_PLATFORM_C */
|
||||
|
||||
#if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_RSA_C) || \
|
||||
!defined(MBEDTLS_X509_CRL_PARSE_C) || !defined(MBEDTLS_FS_IO)
|
||||
@@ -67,7 +70,8 @@ struct options
|
||||
|
||||
int main( int argc, char *argv[] )
|
||||
{
|
||||
int ret = 0;
|
||||
int ret = 1;
|
||||
int exit_code = MBEDTLS_EXIT_FAILURE;
|
||||
unsigned char buf[100000];
|
||||
mbedtls_x509_crl crl;
|
||||
int i;
|
||||
@@ -131,6 +135,8 @@ int main( int argc, char *argv[] )
|
||||
|
||||
mbedtls_printf( "%s\n", buf );
|
||||
|
||||
exit_code = MBEDTLS_EXIT_SUCCESS;
|
||||
|
||||
exit:
|
||||
mbedtls_x509_crl_free( &crl );
|
||||
|
||||
@@ -139,7 +145,7 @@ exit:
|
||||
fflush( stdout ); getchar();
|
||||
#endif
|
||||
|
||||
return( ret );
|
||||
return( exit_code );
|
||||
}
|
||||
#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_RSA_C && MBEDTLS_X509_CRL_PARSE_C &&
|
||||
MBEDTLS_FS_IO */
|
||||
|
||||
@@ -29,8 +29,11 @@
|
||||
#include "mbedtls/platform.h"
|
||||
#else
|
||||
#include <stdio.h>
|
||||
#define mbedtls_printf printf
|
||||
#endif
|
||||
#include <stdlib.h>
|
||||
#define mbedtls_printf printf
|
||||
#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
|
||||
#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
|
||||
#endif /* MBEDTLS_PLATFORM_C */
|
||||
|
||||
#if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_RSA_C) || \
|
||||
!defined(MBEDTLS_X509_CSR_PARSE_C) || !defined(MBEDTLS_FS_IO)
|
||||
@@ -67,7 +70,8 @@ struct options
|
||||
|
||||
int main( int argc, char *argv[] )
|
||||
{
|
||||
int ret = 0;
|
||||
int ret = 1;
|
||||
int exit_code = MBEDTLS_EXIT_FAILURE;
|
||||
unsigned char buf[100000];
|
||||
mbedtls_x509_csr csr;
|
||||
int i;
|
||||
@@ -131,6 +135,8 @@ int main( int argc, char *argv[] )
|
||||
|
||||
mbedtls_printf( "%s\n", buf );
|
||||
|
||||
exit_code = MBEDTLS_EXIT_SUCCESS;
|
||||
|
||||
exit:
|
||||
mbedtls_x509_csr_free( &csr );
|
||||
|
||||
@@ -139,7 +145,7 @@ exit:
|
||||
fflush( stdout ); getchar();
|
||||
#endif
|
||||
|
||||
return( ret );
|
||||
return( exit_code );
|
||||
}
|
||||
#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_RSA_C && MBEDTLS_X509_CSR_PARSE_C &&
|
||||
MBEDTLS_FS_IO */
|
||||
|
||||
Reference in New Issue
Block a user