2009-02-09 22:32:35 +00:00
|
|
|
/*
|
|
|
|
* SSL certificate functionality tests
|
|
|
|
*
|
2015-01-23 09:45:19 +00:00
|
|
|
* Copyright (C) 2006-2011, ARM Limited, All Rights Reserved
|
2010-07-18 20:36:00 +00:00
|
|
|
*
|
2015-03-06 13:17:10 +00:00
|
|
|
* This file is part of mbed TLS (https://tls.mbed.org)
|
2010-07-18 20:36:00 +00:00
|
|
|
*
|
2009-02-09 22:32:35 +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.
|
|
|
|
*/
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if !defined(MBEDTLS_CONFIG_FILE)
|
2015-03-09 17:05:11 +00:00
|
|
|
#include "mbedtls/config.h"
|
2014-04-29 10:39:06 +00:00
|
|
|
#else
|
2015-04-08 10:49:31 +00:00
|
|
|
#include MBEDTLS_CONFIG_FILE
|
2014-04-29 10:39:06 +00:00
|
|
|
#endif
|
2009-02-09 22:32:35 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_PLATFORM_C)
|
2015-03-09 17:05:11 +00:00
|
|
|
#include "mbedtls/platform.h"
|
2015-01-19 14:26:37 +00:00
|
|
|
#else
|
2015-02-11 14:06:19 +00:00
|
|
|
#include <stdio.h>
|
2015-04-08 10:49:31 +00:00
|
|
|
#define mbedtls_snprintf snprintf
|
|
|
|
#define mbedtls_printf printf
|
|
|
|
#define mbedtls_snprintf snprintf
|
2015-01-19 14:26:37 +00:00
|
|
|
#endif
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_X509_CRT_PARSE_C) && \
|
|
|
|
defined(MBEDTLS_FS_IO) && defined(MBEDTLS_X509_CRL_PARSE_C)
|
2015-03-09 17:05:11 +00:00
|
|
|
#include "mbedtls/certs.h"
|
|
|
|
#include "mbedtls/x509_crt.h"
|
2015-02-11 14:06:19 +00:00
|
|
|
|
2009-02-09 22:32:35 +00:00
|
|
|
#include <stdio.h>
|
2015-02-11 14:06:19 +00:00
|
|
|
#include <string.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined _MSC_VER && !defined snprintf
|
|
|
|
#define snprintf _snprintf
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define MAX_CLIENT_CERTS 8
|
2009-02-09 22:32:35 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if !defined(MBEDTLS_RSA_C) || !defined(MBEDTLS_X509_CRT_PARSE_C) || \
|
|
|
|
!defined(MBEDTLS_FS_IO) || !defined(MBEDTLS_X509_CRL_PARSE_C)
|
2015-02-12 11:37:29 +00:00
|
|
|
int main( void )
|
2013-09-20 10:29:56 +00:00
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_printf("MBEDTLS_RSA_C and/or MBEDTLS_X509_CRT_PARSE_C "
|
|
|
|
"MBEDTLS_FS_IO and/or MBEDTLS_X509_CRL_PARSE_C "
|
2013-09-20 10:29:56 +00:00
|
|
|
"not defined.\n");
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
#else
|
2013-06-24 11:01:08 +00:00
|
|
|
const char *client_certificates[MAX_CLIENT_CERTS] =
|
2009-02-09 22:32:35 +00:00
|
|
|
{
|
2009-05-02 15:13:40 +00:00
|
|
|
"client1.crt",
|
|
|
|
"client2.crt",
|
2009-05-03 10:18:48 +00:00
|
|
|
"server1.crt",
|
|
|
|
"server2.crt",
|
2009-05-02 15:13:40 +00:00
|
|
|
"cert_sha224.crt",
|
|
|
|
"cert_sha256.crt",
|
|
|
|
"cert_sha384.crt",
|
|
|
|
"cert_sha512.crt"
|
2009-02-09 22:32:35 +00:00
|
|
|
};
|
|
|
|
|
2013-06-24 11:01:08 +00:00
|
|
|
const char *client_private_keys[MAX_CLIENT_CERTS] =
|
2009-03-28 17:30:26 +00:00
|
|
|
{
|
2009-05-02 15:13:40 +00:00
|
|
|
"client1.key",
|
|
|
|
"client2.key",
|
2009-05-03 10:18:48 +00:00
|
|
|
"server1.key",
|
|
|
|
"server2.key",
|
2011-02-09 17:10:48 +00:00
|
|
|
"cert_digest.key",
|
|
|
|
"cert_digest.key",
|
|
|
|
"cert_digest.key",
|
|
|
|
"cert_digest.key"
|
2009-03-28 17:30:26 +00:00
|
|
|
};
|
|
|
|
|
2015-02-12 11:37:29 +00:00
|
|
|
int main( void )
|
2009-02-09 22:32:35 +00:00
|
|
|
{
|
|
|
|
int ret, i;
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_x509_crt cacert;
|
|
|
|
mbedtls_x509_crl crl;
|
2009-05-02 15:13:40 +00:00
|
|
|
char buf[10240];
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_x509_crt_init( &cacert );
|
|
|
|
mbedtls_x509_crl_init( &crl );
|
2009-02-09 22:32:35 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 1.1. Load the trusted CA
|
|
|
|
*/
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_printf( "\n . Loading the CA root certificate ..." );
|
2009-02-09 22:32:35 +00:00
|
|
|
fflush( stdout );
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Alternatively, you may load the CA certificates from a .pem or
|
2015-04-08 10:49:31 +00:00
|
|
|
* .crt file by calling mbedtls_x509_crt_parse_file( &cacert, "myca.crt" ).
|
2009-02-09 22:32:35 +00:00
|
|
|
*/
|
2015-04-08 10:49:31 +00:00
|
|
|
ret = mbedtls_x509_crt_parse_file( &cacert, "ssl/test-ca/test-ca.crt" );
|
2009-02-09 22:32:35 +00:00
|
|
|
if( ret != 0 )
|
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse_file returned %d\n\n", ret );
|
2009-02-09 22:32:35 +00:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_printf( " ok\n" );
|
2009-02-09 22:32:35 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_x509_crt_info( buf, 1024, "CRT: ", &cacert );
|
|
|
|
mbedtls_printf("%s\n", buf );
|
2009-05-03 10:18:48 +00:00
|
|
|
|
2009-05-02 15:13:40 +00:00
|
|
|
/*
|
|
|
|
* 1.2. Load the CRL
|
|
|
|
*/
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_printf( " . Loading the CRL ..." );
|
2009-05-02 15:13:40 +00:00
|
|
|
fflush( stdout );
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
ret = mbedtls_x509_crl_parse_file( &crl, "ssl/test-ca/crl.pem" );
|
2009-05-02 15:13:40 +00:00
|
|
|
if( ret != 0 )
|
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_printf( " failed\n ! mbedtls_x509_crl_parse_file returned %d\n\n", ret );
|
2009-05-02 15:13:40 +00:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_printf( " ok\n" );
|
2009-05-02 15:13:40 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_x509_crl_info( buf, 1024, "CRL: ", &crl );
|
|
|
|
mbedtls_printf("%s\n", buf );
|
2009-05-02 15:13:40 +00:00
|
|
|
|
2009-02-09 22:32:35 +00:00
|
|
|
for( i = 0; i < MAX_CLIENT_CERTS; i++ )
|
|
|
|
{
|
|
|
|
/*
|
2009-05-02 15:13:40 +00:00
|
|
|
* 1.3. Load own certificate
|
2009-02-09 22:32:35 +00:00
|
|
|
*/
|
2009-05-02 15:13:40 +00:00
|
|
|
char name[512];
|
2015-05-11 17:54:43 +00:00
|
|
|
uint32_t flags;
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_x509_crt clicert;
|
|
|
|
mbedtls_pk_context pk;
|
2009-03-28 17:53:03 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_x509_crt_init( &clicert );
|
|
|
|
mbedtls_pk_init( &pk );
|
2009-02-09 22:32:35 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_snprintf(name, 512, "ssl/test-ca/%s", client_certificates[i]);
|
2009-02-09 22:32:35 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_printf( " . Loading the client certificate %s...", name );
|
2009-05-02 15:13:40 +00:00
|
|
|
fflush( stdout );
|
2009-02-09 22:32:35 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
ret = mbedtls_x509_crt_parse_file( &clicert, name );
|
2009-05-02 15:13:40 +00:00
|
|
|
if( ret != 0 )
|
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse_file returned %d\n\n", ret );
|
2009-05-02 15:13:40 +00:00
|
|
|
goto exit;
|
|
|
|
}
|
2009-02-09 22:32:35 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_printf( " ok\n" );
|
2009-02-09 22:32:35 +00:00
|
|
|
|
2009-05-02 15:13:40 +00:00
|
|
|
/*
|
|
|
|
* 1.4. Verify certificate validity with CA certificate
|
|
|
|
*/
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_printf( " . Verify the client certificate with CA certificate..." );
|
2009-05-02 15:13:40 +00:00
|
|
|
fflush( stdout );
|
2009-02-09 22:32:35 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
ret = mbedtls_x509_crt_verify( &clicert, &cacert, &crl, NULL, &flags, NULL,
|
2013-09-18 11:46:23 +00:00
|
|
|
NULL );
|
2009-05-02 15:13:40 +00:00
|
|
|
if( ret != 0 )
|
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
if( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED )
|
2009-05-03 10:18:48 +00:00
|
|
|
{
|
2015-04-20 09:56:18 +00:00
|
|
|
char vrfy_buf[512];
|
|
|
|
|
|
|
|
mbedtls_printf( " failed\n" );
|
2015-04-30 08:28:51 +00:00
|
|
|
mbedtls_x509_crt_verify_info( vrfy_buf, sizeof( vrfy_buf ), " ! ", flags );
|
2015-04-20 09:56:18 +00:00
|
|
|
mbedtls_printf( "%s\n", vrfy_buf );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-04-30 08:28:51 +00:00
|
|
|
mbedtls_printf( " failed\n ! mbedtls_x509_crt_verify returned %d\n\n", ret );
|
2009-05-03 10:18:48 +00:00
|
|
|
goto exit;
|
|
|
|
}
|
2009-05-02 15:13:40 +00:00
|
|
|
}
|
2009-02-09 22:32:35 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_printf( " ok\n" );
|
2009-03-28 17:30:26 +00:00
|
|
|
|
|
|
|
/*
|
2009-05-02 15:13:40 +00:00
|
|
|
* 1.5. Load own private key
|
2009-03-28 17:30:26 +00:00
|
|
|
*/
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_snprintf(name, 512, "ssl/test-ca/%s", client_private_keys[i]);
|
2009-03-28 17:30:26 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_printf( " . Loading the client private key %s...", name );
|
2009-03-28 17:30:26 +00:00
|
|
|
fflush( stdout );
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
ret = mbedtls_pk_parse_keyfile( &pk, name, NULL );
|
2009-05-02 15:13:40 +00:00
|
|
|
if( ret != 0 )
|
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile returned %d\n\n", ret );
|
2009-05-02 15:13:40 +00:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_printf( " ok\n" );
|
2009-05-02 15:13:40 +00:00
|
|
|
|
|
|
|
/*
|
2013-07-11 11:17:21 +00:00
|
|
|
* 1.6. Verify certificate validity with private key
|
2009-05-02 15:13:40 +00:00
|
|
|
*/
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_printf( " . Verify the client certificate with private key..." );
|
2009-05-02 15:13:40 +00:00
|
|
|
fflush( stdout );
|
|
|
|
|
2013-07-11 11:17:21 +00:00
|
|
|
|
|
|
|
/* EC NOT IMPLEMENTED YET */
|
2015-04-08 10:49:31 +00:00
|
|
|
if( ! mbedtls_pk_can_do( &clicert.pk, MBEDTLS_PK_RSA ) )
|
2013-07-11 11:17:21 +00:00
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_printf( " failed\n ! certificate's key is not RSA\n\n" );
|
|
|
|
ret = MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
|
2013-07-11 11:17:21 +00:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
ret = mbedtls_mpi_cmp_mpi(&mbedtls_pk_rsa( pk )->N, &mbedtls_pk_rsa( clicert.pk )->N);
|
2009-05-02 15:13:40 +00:00
|
|
|
if( ret != 0 )
|
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_printf( " failed\n ! mbedtls_mpi_cmp_mpi for N returned %d\n\n", ret );
|
2009-05-02 15:13:40 +00:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
ret = mbedtls_mpi_cmp_mpi(&mbedtls_pk_rsa( pk )->E, &mbedtls_pk_rsa( clicert.pk )->E);
|
2009-05-02 15:13:40 +00:00
|
|
|
if( ret != 0 )
|
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_printf( " failed\n ! mbedtls_mpi_cmp_mpi for E returned %d\n\n", ret );
|
2009-05-02 15:13:40 +00:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
ret = mbedtls_rsa_check_privkey( mbedtls_pk_rsa( pk ) );
|
2009-05-02 15:13:40 +00:00
|
|
|
if( ret != 0 )
|
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_printf( " failed\n ! mbedtls_rsa_check_privkey returned %d\n\n", ret );
|
2009-05-02 15:13:40 +00:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_printf( " ok\n" );
|
2009-05-02 15:13:40 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_x509_crt_free( &clicert );
|
|
|
|
mbedtls_pk_free( &pk );
|
2009-02-09 22:32:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_x509_crt_free( &cacert );
|
|
|
|
mbedtls_x509_crl_free( &crl );
|
2009-02-09 22:32:35 +00:00
|
|
|
|
2011-11-18 14:26:47 +00:00
|
|
|
#if defined(_WIN32)
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_printf( " + Press Enter to exit this program.\n" );
|
2009-02-09 22:32:35 +00:00
|
|
|
fflush( stdout ); getchar();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return( ret );
|
|
|
|
}
|
2015-04-08 10:49:31 +00:00
|
|
|
#endif /* MBEDTLS_RSA_C && MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_FS_IO &&
|
|
|
|
MBEDTLS_X509_CRL_PARSE_C */
|