2012-05-19 08:43:48 +00:00
|
|
|
/*
|
2015-01-22 16:26:39 +00:00
|
|
|
* Test application that shows some mbed TLS and OpenSSL compatibility
|
2012-05-19 08:43:48 +00:00
|
|
|
*
|
2015-01-23 09:45:19 +00:00
|
|
|
* Copyright (C) 2011-2012 ARM Limited, All Rights Reserved
|
2012-05-19 08:43:48 +00:00
|
|
|
*
|
2015-01-28 17:12:07 +00:00
|
|
|
* This file is part of mbed TLS (https://polarssl.org)
|
2012-05-19 08:43:48 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
2014-04-29 10:39:06 +00:00
|
|
|
#if !defined(POLARSSL_CONFIG_FILE)
|
2013-09-20 11:30:43 +00:00
|
|
|
#include "polarssl/config.h"
|
2014-04-29 10:39:06 +00:00
|
|
|
#else
|
|
|
|
#include POLARSSL_CONFIG_FILE
|
|
|
|
#endif
|
2012-05-19 08:43:48 +00:00
|
|
|
|
2015-01-19 14:26:37 +00:00
|
|
|
#if defined(POLARSSL_PLATFORM_C)
|
|
|
|
#include "polarssl/platform.h"
|
|
|
|
#else
|
2015-02-11 14:06:19 +00:00
|
|
|
#include <stdio.h>
|
2015-01-19 14:26:37 +00:00
|
|
|
#define polarssl_printf printf
|
|
|
|
#endif
|
|
|
|
|
2015-02-13 14:09:44 +00:00
|
|
|
#if defined(POLARSSL_BIGNUM_C) && defined(POLARSSL_RSA_C) && \
|
2015-02-11 14:06:19 +00:00
|
|
|
defined(POLARSSL_PK_PARSE_C) && defined(POLARSSL_FS_IO)
|
2012-05-19 08:43:48 +00:00
|
|
|
#include <openssl/rsa.h>
|
2015-02-11 14:06:19 +00:00
|
|
|
|
2014-01-23 14:43:07 +00:00
|
|
|
#ifndef OPENSSL_NO_ENGINE
|
2012-05-19 08:43:48 +00:00
|
|
|
#include <openssl/engine.h>
|
2014-01-23 14:43:07 +00:00
|
|
|
#endif
|
2015-02-11 14:06:19 +00:00
|
|
|
|
2012-05-19 08:43:48 +00:00
|
|
|
#include <openssl/pem.h>
|
|
|
|
#include <openssl/bio.h>
|
|
|
|
|
2013-09-17 11:25:29 +00:00
|
|
|
#include "polarssl/pk.h"
|
2012-05-19 08:43:48 +00:00
|
|
|
#include "polarssl/x509.h"
|
|
|
|
#include "polarssl/entropy.h"
|
|
|
|
#include "polarssl/ctr_drbg.h"
|
|
|
|
|
2015-02-11 14:06:19 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
2013-04-18 20:46:23 +00:00
|
|
|
#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_RSA_C) || \
|
2013-09-17 11:25:29 +00:00
|
|
|
!defined(POLARSSL_PK_PARSE_C) || !defined(POLARSSL_FS_IO)
|
2015-02-12 11:37:29 +00:00
|
|
|
int main( void )
|
2013-04-18 20:46:23 +00:00
|
|
|
{
|
2015-01-19 14:26:37 +00:00
|
|
|
polarssl_printf("POLARSSL_BIGNUM_C and/or POLARSSL_RSA_C and/or "
|
2013-09-17 11:25:29 +00:00
|
|
|
"POLARSSL_PK_PARSE_C and/or POLARSSL_FS_IO not defined.\n");
|
2013-04-18 20:46:23 +00:00
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
#else
|
2012-05-19 08:43:48 +00:00
|
|
|
int main( int argc, char *argv[] )
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
FILE *key_file;
|
|
|
|
size_t olen;
|
2013-09-17 11:25:29 +00:00
|
|
|
pk_context p_pk;
|
|
|
|
rsa_context *p_rsa;
|
2012-05-19 08:43:48 +00:00
|
|
|
RSA *o_rsa;
|
|
|
|
entropy_context entropy;
|
|
|
|
ctr_drbg_context ctr_drbg;
|
|
|
|
unsigned char input[1024];
|
|
|
|
unsigned char p_pub_encrypted[512];
|
|
|
|
unsigned char o_pub_encrypted[512];
|
|
|
|
unsigned char p_pub_decrypted[512];
|
|
|
|
unsigned char o_pub_decrypted[512];
|
|
|
|
unsigned char p_priv_encrypted[512];
|
|
|
|
unsigned char o_priv_encrypted[512];
|
|
|
|
unsigned char p_priv_decrypted[512];
|
|
|
|
unsigned char o_priv_decrypted[512];
|
2013-06-24 11:01:08 +00:00
|
|
|
const char *pers = "o_p_test_example";
|
2012-05-19 08:43:48 +00:00
|
|
|
|
|
|
|
entropy_init( &entropy );
|
|
|
|
if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
|
2013-06-24 11:01:08 +00:00
|
|
|
(const unsigned char *) pers,
|
|
|
|
strlen( pers ) ) ) != 0 )
|
2012-05-19 08:43:48 +00:00
|
|
|
{
|
2015-01-19 14:26:37 +00:00
|
|
|
polarssl_printf( " failed\n ! ctr_drbg_init returned %d\n", ret );
|
2012-05-19 08:43:48 +00:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
ERR_load_crypto_strings();
|
|
|
|
|
|
|
|
ret = 1;
|
|
|
|
|
|
|
|
if( argc != 3 )
|
|
|
|
{
|
2015-01-19 14:26:37 +00:00
|
|
|
polarssl_printf( "usage: o_p_test <keyfile with private_key> <string of max 100 characters>\n" );
|
2012-05-19 08:43:48 +00:00
|
|
|
|
|
|
|
#ifdef WIN32
|
2015-01-19 14:26:37 +00:00
|
|
|
polarssl_printf( "\n" );
|
2012-05-19 08:43:48 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2015-01-19 14:26:37 +00:00
|
|
|
polarssl_printf( " . Reading private key from %s into mbed TLS ...", argv[1] );
|
2012-05-19 08:43:48 +00:00
|
|
|
fflush( stdout );
|
|
|
|
|
2013-09-17 11:25:29 +00:00
|
|
|
pk_init( &p_pk );
|
|
|
|
if( pk_parse_keyfile( &p_pk, argv[1], NULL ) != 0 )
|
2012-05-19 08:43:48 +00:00
|
|
|
{
|
|
|
|
ret = 1;
|
2015-01-19 14:26:37 +00:00
|
|
|
polarssl_printf( " failed\n ! Could not load key.\n\n" );
|
2012-05-19 08:43:48 +00:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2013-09-17 11:25:29 +00:00
|
|
|
if( !pk_can_do( &p_pk, POLARSSL_PK_RSA ) )
|
|
|
|
{
|
|
|
|
ret = 1;
|
2015-01-19 14:26:37 +00:00
|
|
|
polarssl_printf( " failed\n ! Key is not an RSA key\n" );
|
2013-09-17 11:25:29 +00:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
p_rsa = pk_rsa( p_pk );
|
|
|
|
|
2015-01-19 14:26:37 +00:00
|
|
|
polarssl_printf( " passed\n");
|
2012-05-19 08:43:48 +00:00
|
|
|
|
2015-01-19 14:26:37 +00:00
|
|
|
polarssl_printf( " . Reading private key from %s into OpenSSL ...", argv[1] );
|
2012-05-19 08:43:48 +00:00
|
|
|
fflush( stdout );
|
|
|
|
|
|
|
|
key_file = fopen( argv[1], "r" );
|
|
|
|
o_rsa = PEM_read_RSAPrivateKey(key_file, 0, 0, 0);
|
|
|
|
fclose(key_file);
|
|
|
|
if( o_rsa == NULL )
|
|
|
|
{
|
|
|
|
ret = 1;
|
2015-01-19 14:26:37 +00:00
|
|
|
polarssl_printf( " failed\n ! Could not load key.\n\n" );
|
2012-05-19 08:43:48 +00:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2015-01-19 14:26:37 +00:00
|
|
|
polarssl_printf( " passed\n");
|
|
|
|
polarssl_printf( "\n" );
|
2012-05-19 08:43:48 +00:00
|
|
|
|
|
|
|
if( strlen( argv[1] ) > 100 )
|
|
|
|
{
|
2015-01-19 14:26:37 +00:00
|
|
|
polarssl_printf( " Input data larger than 100 characters.\n\n" );
|
2012-05-19 08:43:48 +00:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy( input, argv[2], strlen( argv[2] ) );
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Calculate the RSA encryption with public key.
|
|
|
|
*/
|
2015-01-19 14:26:37 +00:00
|
|
|
polarssl_printf( " . Generating the RSA encrypted value with mbed TLS (RSA_PUBLIC) ..." );
|
2012-05-19 08:43:48 +00:00
|
|
|
fflush( stdout );
|
|
|
|
|
2013-09-17 11:25:29 +00:00
|
|
|
if( ( ret = rsa_pkcs1_encrypt( p_rsa, ctr_drbg_random, &ctr_drbg, RSA_PUBLIC, strlen( argv[2] ), input, p_pub_encrypted ) ) != 0 )
|
2012-05-19 08:43:48 +00:00
|
|
|
{
|
2015-01-19 14:26:37 +00:00
|
|
|
polarssl_printf( " failed\n ! rsa_pkcs1_encrypt returned %d\n\n", ret );
|
2012-05-19 08:43:48 +00:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
else
|
2015-01-19 14:26:37 +00:00
|
|
|
polarssl_printf( " passed\n");
|
2012-05-19 08:43:48 +00:00
|
|
|
|
2015-01-19 14:26:37 +00:00
|
|
|
polarssl_printf( " . Generating the RSA encrypted value with OpenSSL (PUBLIC) ..." );
|
2012-05-19 08:43:48 +00:00
|
|
|
fflush( stdout );
|
|
|
|
|
2013-09-17 11:25:29 +00:00
|
|
|
if( ( ret = RSA_public_encrypt( strlen( argv[2] ), input, o_pub_encrypted, o_rsa, RSA_PKCS1_PADDING ) ) == -1 )
|
2012-05-19 08:43:48 +00:00
|
|
|
{
|
|
|
|
unsigned long code = ERR_get_error();
|
2015-01-19 14:26:37 +00:00
|
|
|
polarssl_printf( " failed\n ! RSA_public_encrypt returned %d %s\n\n", ret, ERR_error_string( code, NULL ) );
|
2012-05-19 08:43:48 +00:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
else
|
2015-01-19 14:26:37 +00:00
|
|
|
polarssl_printf( " passed\n");
|
2012-05-19 08:43:48 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Calculate the RSA encryption with private key.
|
|
|
|
*/
|
2015-01-19 14:26:37 +00:00
|
|
|
polarssl_printf( " . Generating the RSA encrypted value with mbed TLS (RSA_PRIVATE) ..." );
|
2012-05-19 08:43:48 +00:00
|
|
|
fflush( stdout );
|
|
|
|
|
2013-09-17 11:25:29 +00:00
|
|
|
if( ( ret = rsa_pkcs1_encrypt( p_rsa, ctr_drbg_random, &ctr_drbg, RSA_PRIVATE, strlen( argv[2] ), input, p_priv_encrypted ) ) != 0 )
|
2012-05-19 08:43:48 +00:00
|
|
|
{
|
2015-01-19 14:26:37 +00:00
|
|
|
polarssl_printf( " failed\n ! rsa_pkcs1_encrypt returned %d\n\n", ret );
|
2012-05-19 08:43:48 +00:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
else
|
2015-01-19 14:26:37 +00:00
|
|
|
polarssl_printf( " passed\n");
|
2012-05-19 08:43:48 +00:00
|
|
|
|
2015-01-19 14:26:37 +00:00
|
|
|
polarssl_printf( " . Generating the RSA encrypted value with OpenSSL (PRIVATE) ..." );
|
2012-05-19 08:43:48 +00:00
|
|
|
fflush( stdout );
|
|
|
|
|
2013-09-17 11:25:29 +00:00
|
|
|
if( ( ret = RSA_private_encrypt( strlen( argv[2] ), input, o_priv_encrypted, o_rsa, RSA_PKCS1_PADDING ) ) == -1 )
|
2012-05-19 08:43:48 +00:00
|
|
|
{
|
|
|
|
unsigned long code = ERR_get_error();
|
2015-01-19 14:26:37 +00:00
|
|
|
polarssl_printf( " failed\n ! RSA_private_encrypt returned %d %s\n\n", ret, ERR_error_string( code, NULL ) );
|
2012-05-19 08:43:48 +00:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
else
|
2015-01-19 14:26:37 +00:00
|
|
|
polarssl_printf( " passed\n");
|
2012-05-19 08:43:48 +00:00
|
|
|
|
2015-01-19 14:26:37 +00:00
|
|
|
polarssl_printf( "\n" );
|
2012-05-19 08:43:48 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Calculate the RSA decryption with private key.
|
|
|
|
*/
|
2015-01-19 14:26:37 +00:00
|
|
|
polarssl_printf( " . Generating the RSA decrypted value for OpenSSL (PUBLIC) with mbed TLS (PRIVATE) ..." );
|
2012-05-19 08:43:48 +00:00
|
|
|
fflush( stdout );
|
|
|
|
|
2013-09-17 11:25:29 +00:00
|
|
|
if( ( ret = rsa_pkcs1_decrypt( p_rsa, ctr_drbg_random, &ctr_drbg, RSA_PRIVATE, &olen, o_pub_encrypted, p_pub_decrypted, 1024 ) ) != 0 )
|
2012-05-19 08:43:48 +00:00
|
|
|
{
|
2015-01-19 14:26:37 +00:00
|
|
|
polarssl_printf( " failed\n ! rsa_pkcs1_decrypt returned %d\n\n", ret );
|
2012-05-19 08:43:48 +00:00
|
|
|
}
|
|
|
|
else
|
2015-01-19 14:26:37 +00:00
|
|
|
polarssl_printf( " passed\n");
|
2012-05-19 08:43:48 +00:00
|
|
|
|
2015-01-19 14:26:37 +00:00
|
|
|
polarssl_printf( " . Generating the RSA decrypted value for mbed TLS (PUBLIC) with OpenSSL (PRIVATE) ..." );
|
2012-05-19 08:43:48 +00:00
|
|
|
fflush( stdout );
|
|
|
|
|
2013-09-17 11:25:29 +00:00
|
|
|
if( ( ret = RSA_private_decrypt( p_rsa->len, p_pub_encrypted, o_pub_decrypted, o_rsa, RSA_PKCS1_PADDING ) ) == -1 )
|
2012-05-19 08:43:48 +00:00
|
|
|
{
|
|
|
|
unsigned long code = ERR_get_error();
|
2015-01-19 14:26:37 +00:00
|
|
|
polarssl_printf( " failed\n ! RSA_private_decrypt returned %d %s\n\n", ret, ERR_error_string( code, NULL ) );
|
2012-05-19 08:43:48 +00:00
|
|
|
}
|
|
|
|
else
|
2015-01-19 14:26:37 +00:00
|
|
|
polarssl_printf( " passed\n");
|
2012-05-19 08:43:48 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Calculate the RSA decryption with public key.
|
|
|
|
*/
|
2015-01-19 14:26:37 +00:00
|
|
|
polarssl_printf( " . Generating the RSA decrypted value for OpenSSL (PRIVATE) with mbed TLS (PUBLIC) ..." );
|
2012-05-19 08:43:48 +00:00
|
|
|
fflush( stdout );
|
|
|
|
|
2013-09-17 11:25:29 +00:00
|
|
|
if( ( ret = rsa_pkcs1_decrypt( p_rsa, NULL, NULL, RSA_PUBLIC, &olen, o_priv_encrypted, p_priv_decrypted, 1024 ) ) != 0 )
|
2012-05-19 08:43:48 +00:00
|
|
|
{
|
2015-01-19 14:26:37 +00:00
|
|
|
polarssl_printf( " failed\n ! rsa_pkcs1_decrypt returned %d\n\n", ret );
|
2012-05-19 08:43:48 +00:00
|
|
|
}
|
|
|
|
else
|
2015-01-19 14:26:37 +00:00
|
|
|
polarssl_printf( " passed\n");
|
2012-05-19 08:43:48 +00:00
|
|
|
|
2015-01-19 14:26:37 +00:00
|
|
|
polarssl_printf( " . Generating the RSA decrypted value for mbed TLS (PRIVATE) with OpenSSL (PUBLIC) ..." );
|
2012-05-19 08:43:48 +00:00
|
|
|
fflush( stdout );
|
|
|
|
|
2013-09-17 11:25:29 +00:00
|
|
|
if( ( ret = RSA_public_decrypt( p_rsa->len, p_priv_encrypted, o_priv_decrypted, o_rsa, RSA_PKCS1_PADDING ) ) == -1 )
|
2012-05-19 08:43:48 +00:00
|
|
|
{
|
|
|
|
unsigned long code = ERR_get_error();
|
2015-01-19 14:26:37 +00:00
|
|
|
polarssl_printf( " failed\n ! RSA_public_decrypt returned %d %s\n\n", ret, ERR_error_string( code, NULL ) );
|
2012-05-19 08:43:48 +00:00
|
|
|
}
|
|
|
|
else
|
2015-01-19 14:26:37 +00:00
|
|
|
polarssl_printf( " passed\n");
|
2012-05-19 08:43:48 +00:00
|
|
|
|
2015-01-19 14:26:37 +00:00
|
|
|
polarssl_printf( "\n" );
|
|
|
|
polarssl_printf( "String value (OpenSSL Public Encrypt, mbed TLS Private Decrypt): '%s'\n", p_pub_decrypted );
|
|
|
|
polarssl_printf( "String value (mbed TLS Public Encrypt, OpenSSL Private Decrypt): '%s'\n", o_pub_decrypted );
|
|
|
|
polarssl_printf( "String value (OpenSSL Private Encrypt, mbed TLS Public Decrypt): '%s'\n", p_priv_decrypted );
|
|
|
|
polarssl_printf( "String value (mbed TLS Private Encrypt, OpenSSL Public Decrypt): '%s'\n", o_priv_decrypted );
|
2012-05-19 08:43:48 +00:00
|
|
|
|
|
|
|
exit:
|
2014-06-18 14:44:11 +00:00
|
|
|
ctr_drbg_free( &ctr_drbg );
|
2013-09-28 13:23:03 +00:00
|
|
|
entropy_free( &entropy );
|
2012-05-19 08:43:48 +00:00
|
|
|
|
|
|
|
#ifdef WIN32
|
2015-01-19 14:26:37 +00:00
|
|
|
polarssl_printf( " + Press Enter to exit this program.\n" );
|
2012-05-19 08:43:48 +00:00
|
|
|
fflush( stdout ); getchar();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return( ret );
|
|
|
|
}
|
2013-04-18 20:46:23 +00:00
|
|
|
#endif /* POLARSSL_BIGNUM_C && POLARSSL_RSA_C &&
|
2013-09-17 11:25:29 +00:00
|
|
|
POLARSSL_PK_PARSE_C && POLARSSL_FS_IO */
|