blob: 3e77c55c93d4888431a52829eacead7a86a17196 [file] [log] [blame]
Paul Bakkere6ee41f2012-05-19 08:43:48 +00001/*
Manuel Pégourié-Gonnard91699212015-01-22 16:26:39 +00002 * Test application that shows some mbed TLS and OpenSSL compatibility
Paul Bakkere6ee41f2012-05-19 08:43:48 +00003 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00004 * Copyright (C) 2011-2012 ARM Limited, All Rights Reserved
Paul Bakkere6ee41f2012-05-19 08:43:48 +00005 *
Manuel Pégourié-Gonnard860b5162015-01-28 17:12:07 +00006 * This file is part of mbed TLS (https://polarssl.org)
Paul Bakkere6ee41f2012-05-19 08:43:48 +00007 *
Paul Bakkere6ee41f2012-05-19 08:43:48 +00008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020023#if !defined(POLARSSL_CONFIG_FILE)
Manuel Pégourié-Gonnardabd6e022013-09-20 13:30:43 +020024#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020025#else
26#include POLARSSL_CONFIG_FILE
27#endif
Paul Bakkere6ee41f2012-05-19 08:43:48 +000028
Rich Evansf90016a2015-01-19 14:26:37 +000029#if defined(POLARSSL_PLATFORM_C)
30#include "polarssl/platform.h"
31#else
Rich Evans18b78c72015-02-11 14:06:19 +000032#include <stdio.h>
Rich Evansf90016a2015-01-19 14:26:37 +000033#define polarssl_printf printf
Rich Evansf90016a2015-01-19 14:26:37 +000034#endif
35
Rich Evans18b78c72015-02-11 14:06:19 +000036#if defined(POLARSSL_BIGNUM_C) && defined(POLARSSL_RSA_C) &&\
37 defined(POLARSSL_PK_PARSE_C) && defined(POLARSSL_FS_IO)
Paul Bakkere6ee41f2012-05-19 08:43:48 +000038#include <openssl/rsa.h>
Rich Evans18b78c72015-02-11 14:06:19 +000039
Paul Bakker5eb264c2014-01-23 15:43:07 +010040#ifndef OPENSSL_NO_ENGINE
Paul Bakkere6ee41f2012-05-19 08:43:48 +000041#include <openssl/engine.h>
Paul Bakker5eb264c2014-01-23 15:43:07 +010042#endif
Rich Evans18b78c72015-02-11 14:06:19 +000043
Paul Bakkere6ee41f2012-05-19 08:43:48 +000044#include <openssl/pem.h>
45#include <openssl/bio.h>
46
Paul Bakker36713e82013-09-17 13:25:29 +020047#include "polarssl/pk.h"
Paul Bakkere6ee41f2012-05-19 08:43:48 +000048#include "polarssl/x509.h"
Paul Bakkere6ee41f2012-05-19 08:43:48 +000049#include "polarssl/entropy.h"
50#include "polarssl/ctr_drbg.h"
51
Rich Evans18b78c72015-02-11 14:06:19 +000052#include <stdio.h>
53#include <stdlib.h>
54#include <string.h>
55#include <sys/stat.h>
56#include <unistd.h>
57#endif
58
Paul Bakkered27a042013-04-18 22:46:23 +020059#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_RSA_C) || \
Paul Bakker36713e82013-09-17 13:25:29 +020060 !defined(POLARSSL_PK_PARSE_C) || !defined(POLARSSL_FS_IO)
Rich Evans85b05ec2015-02-12 11:37:29 +000061int main( void )
Paul Bakkered27a042013-04-18 22:46:23 +020062{
Rich Evansf90016a2015-01-19 14:26:37 +000063 polarssl_printf("POLARSSL_BIGNUM_C and/or POLARSSL_RSA_C and/or "
Paul Bakker36713e82013-09-17 13:25:29 +020064 "POLARSSL_PK_PARSE_C and/or POLARSSL_FS_IO not defined.\n");
Paul Bakkered27a042013-04-18 22:46:23 +020065 return( 0 );
66}
67#else
Paul Bakkere6ee41f2012-05-19 08:43:48 +000068int main( int argc, char *argv[] )
69{
70 int ret;
71 FILE *key_file;
72 size_t olen;
Paul Bakker36713e82013-09-17 13:25:29 +020073 pk_context p_pk;
74 rsa_context *p_rsa;
Paul Bakkere6ee41f2012-05-19 08:43:48 +000075 RSA *o_rsa;
76 entropy_context entropy;
77 ctr_drbg_context ctr_drbg;
78 unsigned char input[1024];
79 unsigned char p_pub_encrypted[512];
80 unsigned char o_pub_encrypted[512];
81 unsigned char p_pub_decrypted[512];
82 unsigned char o_pub_decrypted[512];
83 unsigned char p_priv_encrypted[512];
84 unsigned char o_priv_encrypted[512];
85 unsigned char p_priv_decrypted[512];
86 unsigned char o_priv_decrypted[512];
Paul Bakkeref3f8c72013-06-24 13:01:08 +020087 const char *pers = "o_p_test_example";
Paul Bakkere6ee41f2012-05-19 08:43:48 +000088
89 entropy_init( &entropy );
90 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +020091 (const unsigned char *) pers,
92 strlen( pers ) ) ) != 0 )
Paul Bakkere6ee41f2012-05-19 08:43:48 +000093 {
Rich Evansf90016a2015-01-19 14:26:37 +000094 polarssl_printf( " failed\n ! ctr_drbg_init returned %d\n", ret );
Paul Bakkere6ee41f2012-05-19 08:43:48 +000095 goto exit;
96 }
97 ERR_load_crypto_strings();
98
99 ret = 1;
100
101 if( argc != 3 )
102 {
Rich Evansf90016a2015-01-19 14:26:37 +0000103 polarssl_printf( "usage: o_p_test <keyfile with private_key> <string of max 100 characters>\n" );
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000104
105#ifdef WIN32
Rich Evansf90016a2015-01-19 14:26:37 +0000106 polarssl_printf( "\n" );
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000107#endif
108
109 goto exit;
110 }
111
Rich Evansf90016a2015-01-19 14:26:37 +0000112 polarssl_printf( " . Reading private key from %s into mbed TLS ...", argv[1] );
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000113 fflush( stdout );
114
Paul Bakker36713e82013-09-17 13:25:29 +0200115 pk_init( &p_pk );
116 if( pk_parse_keyfile( &p_pk, argv[1], NULL ) != 0 )
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000117 {
118 ret = 1;
Rich Evansf90016a2015-01-19 14:26:37 +0000119 polarssl_printf( " failed\n ! Could not load key.\n\n" );
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000120 goto exit;
121 }
122
Paul Bakker36713e82013-09-17 13:25:29 +0200123 if( !pk_can_do( &p_pk, POLARSSL_PK_RSA ) )
124 {
125 ret = 1;
Rich Evansf90016a2015-01-19 14:26:37 +0000126 polarssl_printf( " failed\n ! Key is not an RSA key\n" );
Paul Bakker36713e82013-09-17 13:25:29 +0200127 goto exit;
128 }
129
130 p_rsa = pk_rsa( p_pk );
131
Rich Evansf90016a2015-01-19 14:26:37 +0000132 polarssl_printf( " passed\n");
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000133
Rich Evansf90016a2015-01-19 14:26:37 +0000134 polarssl_printf( " . Reading private key from %s into OpenSSL ...", argv[1] );
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000135 fflush( stdout );
136
137 key_file = fopen( argv[1], "r" );
138 o_rsa = PEM_read_RSAPrivateKey(key_file, 0, 0, 0);
139 fclose(key_file);
140 if( o_rsa == NULL )
141 {
142 ret = 1;
Rich Evansf90016a2015-01-19 14:26:37 +0000143 polarssl_printf( " failed\n ! Could not load key.\n\n" );
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000144 goto exit;
145 }
146
Rich Evansf90016a2015-01-19 14:26:37 +0000147 polarssl_printf( " passed\n");
148 polarssl_printf( "\n" );
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000149
150 if( strlen( argv[1] ) > 100 )
151 {
Rich Evansf90016a2015-01-19 14:26:37 +0000152 polarssl_printf( " Input data larger than 100 characters.\n\n" );
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000153 goto exit;
154 }
155
156 memcpy( input, argv[2], strlen( argv[2] ) );
157
158 /*
159 * Calculate the RSA encryption with public key.
160 */
Rich Evansf90016a2015-01-19 14:26:37 +0000161 polarssl_printf( " . Generating the RSA encrypted value with mbed TLS (RSA_PUBLIC) ..." );
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000162 fflush( stdout );
163
Paul Bakker36713e82013-09-17 13:25:29 +0200164 if( ( ret = rsa_pkcs1_encrypt( p_rsa, ctr_drbg_random, &ctr_drbg, RSA_PUBLIC, strlen( argv[2] ), input, p_pub_encrypted ) ) != 0 )
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000165 {
Rich Evansf90016a2015-01-19 14:26:37 +0000166 polarssl_printf( " failed\n ! rsa_pkcs1_encrypt returned %d\n\n", ret );
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000167 goto exit;
168 }
169 else
Rich Evansf90016a2015-01-19 14:26:37 +0000170 polarssl_printf( " passed\n");
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000171
Rich Evansf90016a2015-01-19 14:26:37 +0000172 polarssl_printf( " . Generating the RSA encrypted value with OpenSSL (PUBLIC) ..." );
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000173 fflush( stdout );
174
Paul Bakker36713e82013-09-17 13:25:29 +0200175 if( ( ret = RSA_public_encrypt( strlen( argv[2] ), input, o_pub_encrypted, o_rsa, RSA_PKCS1_PADDING ) ) == -1 )
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000176 {
177 unsigned long code = ERR_get_error();
Rich Evansf90016a2015-01-19 14:26:37 +0000178 polarssl_printf( " failed\n ! RSA_public_encrypt returned %d %s\n\n", ret, ERR_error_string( code, NULL ) );
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000179 goto exit;
180 }
181 else
Rich Evansf90016a2015-01-19 14:26:37 +0000182 polarssl_printf( " passed\n");
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000183
184 /*
185 * Calculate the RSA encryption with private key.
186 */
Rich Evansf90016a2015-01-19 14:26:37 +0000187 polarssl_printf( " . Generating the RSA encrypted value with mbed TLS (RSA_PRIVATE) ..." );
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000188 fflush( stdout );
189
Paul Bakker36713e82013-09-17 13:25:29 +0200190 if( ( ret = rsa_pkcs1_encrypt( p_rsa, ctr_drbg_random, &ctr_drbg, RSA_PRIVATE, strlen( argv[2] ), input, p_priv_encrypted ) ) != 0 )
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000191 {
Rich Evansf90016a2015-01-19 14:26:37 +0000192 polarssl_printf( " failed\n ! rsa_pkcs1_encrypt returned %d\n\n", ret );
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000193 goto exit;
194 }
195 else
Rich Evansf90016a2015-01-19 14:26:37 +0000196 polarssl_printf( " passed\n");
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000197
Rich Evansf90016a2015-01-19 14:26:37 +0000198 polarssl_printf( " . Generating the RSA encrypted value with OpenSSL (PRIVATE) ..." );
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000199 fflush( stdout );
200
Paul Bakker36713e82013-09-17 13:25:29 +0200201 if( ( ret = RSA_private_encrypt( strlen( argv[2] ), input, o_priv_encrypted, o_rsa, RSA_PKCS1_PADDING ) ) == -1 )
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000202 {
203 unsigned long code = ERR_get_error();
Rich Evansf90016a2015-01-19 14:26:37 +0000204 polarssl_printf( " failed\n ! RSA_private_encrypt returned %d %s\n\n", ret, ERR_error_string( code, NULL ) );
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000205 goto exit;
206 }
207 else
Rich Evansf90016a2015-01-19 14:26:37 +0000208 polarssl_printf( " passed\n");
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000209
Rich Evansf90016a2015-01-19 14:26:37 +0000210 polarssl_printf( "\n" );
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000211
212 /*
213 * Calculate the RSA decryption with private key.
214 */
Rich Evansf90016a2015-01-19 14:26:37 +0000215 polarssl_printf( " . Generating the RSA decrypted value for OpenSSL (PUBLIC) with mbed TLS (PRIVATE) ..." );
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000216 fflush( stdout );
217
Paul Bakker36713e82013-09-17 13:25:29 +0200218 if( ( ret = rsa_pkcs1_decrypt( p_rsa, ctr_drbg_random, &ctr_drbg, RSA_PRIVATE, &olen, o_pub_encrypted, p_pub_decrypted, 1024 ) ) != 0 )
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000219 {
Rich Evansf90016a2015-01-19 14:26:37 +0000220 polarssl_printf( " failed\n ! rsa_pkcs1_decrypt returned %d\n\n", ret );
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000221 }
222 else
Rich Evansf90016a2015-01-19 14:26:37 +0000223 polarssl_printf( " passed\n");
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000224
Rich Evansf90016a2015-01-19 14:26:37 +0000225 polarssl_printf( " . Generating the RSA decrypted value for mbed TLS (PUBLIC) with OpenSSL (PRIVATE) ..." );
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000226 fflush( stdout );
227
Paul Bakker36713e82013-09-17 13:25:29 +0200228 if( ( ret = RSA_private_decrypt( p_rsa->len, p_pub_encrypted, o_pub_decrypted, o_rsa, RSA_PKCS1_PADDING ) ) == -1 )
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000229 {
230 unsigned long code = ERR_get_error();
Rich Evansf90016a2015-01-19 14:26:37 +0000231 polarssl_printf( " failed\n ! RSA_private_decrypt returned %d %s\n\n", ret, ERR_error_string( code, NULL ) );
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000232 }
233 else
Rich Evansf90016a2015-01-19 14:26:37 +0000234 polarssl_printf( " passed\n");
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000235
236 /*
237 * Calculate the RSA decryption with public key.
238 */
Rich Evansf90016a2015-01-19 14:26:37 +0000239 polarssl_printf( " . Generating the RSA decrypted value for OpenSSL (PRIVATE) with mbed TLS (PUBLIC) ..." );
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000240 fflush( stdout );
241
Paul Bakker36713e82013-09-17 13:25:29 +0200242 if( ( ret = rsa_pkcs1_decrypt( p_rsa, NULL, NULL, RSA_PUBLIC, &olen, o_priv_encrypted, p_priv_decrypted, 1024 ) ) != 0 )
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000243 {
Rich Evansf90016a2015-01-19 14:26:37 +0000244 polarssl_printf( " failed\n ! rsa_pkcs1_decrypt returned %d\n\n", ret );
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000245 }
246 else
Rich Evansf90016a2015-01-19 14:26:37 +0000247 polarssl_printf( " passed\n");
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000248
Rich Evansf90016a2015-01-19 14:26:37 +0000249 polarssl_printf( " . Generating the RSA decrypted value for mbed TLS (PRIVATE) with OpenSSL (PUBLIC) ..." );
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000250 fflush( stdout );
251
Paul Bakker36713e82013-09-17 13:25:29 +0200252 if( ( ret = RSA_public_decrypt( p_rsa->len, p_priv_encrypted, o_priv_decrypted, o_rsa, RSA_PKCS1_PADDING ) ) == -1 )
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000253 {
254 unsigned long code = ERR_get_error();
Rich Evansf90016a2015-01-19 14:26:37 +0000255 polarssl_printf( " failed\n ! RSA_public_decrypt returned %d %s\n\n", ret, ERR_error_string( code, NULL ) );
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000256 }
257 else
Rich Evansf90016a2015-01-19 14:26:37 +0000258 polarssl_printf( " passed\n");
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000259
Rich Evansf90016a2015-01-19 14:26:37 +0000260 polarssl_printf( "\n" );
261 polarssl_printf( "String value (OpenSSL Public Encrypt, mbed TLS Private Decrypt): '%s'\n", p_pub_decrypted );
262 polarssl_printf( "String value (mbed TLS Public Encrypt, OpenSSL Private Decrypt): '%s'\n", o_pub_decrypted );
263 polarssl_printf( "String value (OpenSSL Private Encrypt, mbed TLS Public Decrypt): '%s'\n", p_priv_decrypted );
264 polarssl_printf( "String value (mbed TLS Private Encrypt, OpenSSL Public Decrypt): '%s'\n", o_priv_decrypted );
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000265
266exit:
Paul Bakkera317a982014-06-18 16:44:11 +0200267 ctr_drbg_free( &ctr_drbg );
Paul Bakker1ffefac2013-09-28 15:23:03 +0200268 entropy_free( &entropy );
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000269
270#ifdef WIN32
Rich Evansf90016a2015-01-19 14:26:37 +0000271 polarssl_printf( " + Press Enter to exit this program.\n" );
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000272 fflush( stdout ); getchar();
273#endif
274
275 return( ret );
276}
Paul Bakkered27a042013-04-18 22:46:23 +0200277#endif /* POLARSSL_BIGNUM_C && POLARSSL_RSA_C &&
Paul Bakker36713e82013-09-17 13:25:29 +0200278 POLARSSL_PK_PARSE_C && POLARSSL_FS_IO */