blob: 920f9369b5fc2f2e4536147e8653cf958c80cace [file] [log] [blame]
Paul Bakker1a7550a2013-09-15 13:01:22 +02001/* BEGIN_HEADER */
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00002#include "mbedtls/pk.h"
3#include "mbedtls/pem.h"
4#include "mbedtls/oid.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +02005/* END_HEADER */
6
7/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008 * depends_on:MBEDTLS_PK_PARSE_C:MBEDTLS_BIGNUM_C
Paul Bakker1a7550a2013-09-15 13:01:22 +02009 * END_DEPENDENCIES
10 */
11
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012/* BEGIN_CASE depends_on:MBEDTLS_RSA_C:MBEDTLS_FS_IO */
Azim Khanf1aaec92017-05-30 14:23:15 +010013void pk_parse_keyfile_rsa( char * key_file, char * password, int result )
Paul Bakker1a7550a2013-09-15 13:01:22 +020014{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020015 mbedtls_pk_context ctx;
Paul Bakker1a7550a2013-09-15 13:01:22 +020016 int res;
17 char *pwd = password;
18
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020019 mbedtls_pk_init( &ctx );
Paul Bakker1a7550a2013-09-15 13:01:22 +020020
21 if( strcmp( pwd, "NULL" ) == 0 )
22 pwd = NULL;
23
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020024 res = mbedtls_pk_parse_keyfile( &ctx, key_file, pwd );
Paul Bakker1a7550a2013-09-15 13:01:22 +020025
26 TEST_ASSERT( res == result );
27
28 if( res == 0 )
29 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030 mbedtls_rsa_context *rsa;
31 TEST_ASSERT( mbedtls_pk_can_do( &ctx, MBEDTLS_PK_RSA ) );
32 rsa = mbedtls_pk_rsa( ctx );
33 TEST_ASSERT( mbedtls_rsa_check_privkey( rsa ) == 0 );
Paul Bakker1a7550a2013-09-15 13:01:22 +020034 }
35
Paul Bakkerbd51b262014-07-10 15:26:12 +020036exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020037 mbedtls_pk_free( &ctx );
Paul Bakker1a7550a2013-09-15 13:01:22 +020038}
39/* END_CASE */
40
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020041/* BEGIN_CASE depends_on:MBEDTLS_RSA_C:MBEDTLS_FS_IO */
Azim Khanf1aaec92017-05-30 14:23:15 +010042void pk_parse_public_keyfile_rsa( char * key_file, int result )
Paul Bakker1a7550a2013-09-15 13:01:22 +020043{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020044 mbedtls_pk_context ctx;
Paul Bakker1a7550a2013-09-15 13:01:22 +020045 int res;
46
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047 mbedtls_pk_init( &ctx );
Paul Bakker1a7550a2013-09-15 13:01:22 +020048
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049 res = mbedtls_pk_parse_public_keyfile( &ctx, key_file );
Paul Bakker1a7550a2013-09-15 13:01:22 +020050
51 TEST_ASSERT( res == result );
52
53 if( res == 0 )
54 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020055 mbedtls_rsa_context *rsa;
56 TEST_ASSERT( mbedtls_pk_can_do( &ctx, MBEDTLS_PK_RSA ) );
57 rsa = mbedtls_pk_rsa( ctx );
58 TEST_ASSERT( mbedtls_rsa_check_pubkey( rsa ) == 0 );
Paul Bakker1a7550a2013-09-15 13:01:22 +020059 }
60
Paul Bakkerbd51b262014-07-10 15:26:12 +020061exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062 mbedtls_pk_free( &ctx );
Paul Bakker1a7550a2013-09-15 13:01:22 +020063}
64/* END_CASE */
65
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020066/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_ECP_C */
Azim Khanf1aaec92017-05-30 14:23:15 +010067void pk_parse_public_keyfile_ec( char * key_file, int result )
Paul Bakker1a7550a2013-09-15 13:01:22 +020068{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020069 mbedtls_pk_context ctx;
Paul Bakker1a7550a2013-09-15 13:01:22 +020070 int res;
71
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020072 mbedtls_pk_init( &ctx );
Paul Bakker1a7550a2013-09-15 13:01:22 +020073
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020074 res = mbedtls_pk_parse_public_keyfile( &ctx, key_file );
Paul Bakker1a7550a2013-09-15 13:01:22 +020075
76 TEST_ASSERT( res == result );
77
78 if( res == 0 )
79 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020080 mbedtls_ecp_keypair *eckey;
81 TEST_ASSERT( mbedtls_pk_can_do( &ctx, MBEDTLS_PK_ECKEY ) );
82 eckey = mbedtls_pk_ec( ctx );
83 TEST_ASSERT( mbedtls_ecp_check_pubkey( &eckey->grp, &eckey->Q ) == 0 );
Paul Bakker1a7550a2013-09-15 13:01:22 +020084 }
85
Paul Bakkerbd51b262014-07-10 15:26:12 +020086exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020087 mbedtls_pk_free( &ctx );
Paul Bakker1a7550a2013-09-15 13:01:22 +020088}
89/* END_CASE */
90
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020091/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_ECP_C */
Azim Khanf1aaec92017-05-30 14:23:15 +010092void pk_parse_keyfile_ec( char * key_file, char * password, int result )
Paul Bakker1a7550a2013-09-15 13:01:22 +020093{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020094 mbedtls_pk_context ctx;
Paul Bakker1a7550a2013-09-15 13:01:22 +020095 int res;
96
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020097 mbedtls_pk_init( &ctx );
Paul Bakker1a7550a2013-09-15 13:01:22 +020098
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020099 res = mbedtls_pk_parse_keyfile( &ctx, key_file, password );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200100
101 TEST_ASSERT( res == result );
102
103 if( res == 0 )
104 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200105 mbedtls_ecp_keypair *eckey;
106 TEST_ASSERT( mbedtls_pk_can_do( &ctx, MBEDTLS_PK_ECKEY ) );
107 eckey = mbedtls_pk_ec( ctx );
108 TEST_ASSERT( mbedtls_ecp_check_privkey( &eckey->grp, &eckey->d ) == 0 );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200109 }
110
Paul Bakkerbd51b262014-07-10 15:26:12 +0200111exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200112 mbedtls_pk_free( &ctx );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200113}
114/* END_CASE */
115
Azim Khanf1aaec92017-05-30 14:23:15 +0100116/* BEGIN_CASE depends_on:MBEDTLS_RSA_C */
Azim Khand30ca132017-06-09 04:32:58 +0100117void pk_parse_key( HexParam_t * buf, char * result_str, int result )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200118{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200119 mbedtls_pk_context pk;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200120 unsigned char output[2000];
Paul Bakker1a7550a2013-09-15 13:01:22 +0200121 ((void) result_str);
122
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200123 mbedtls_pk_init( &pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200124
Paul Bakker1a7550a2013-09-15 13:01:22 +0200125 memset( output, 0, 2000 );
126
Paul Bakker1a7550a2013-09-15 13:01:22 +0200127
Azim Khand30ca132017-06-09 04:32:58 +0100128 TEST_ASSERT( mbedtls_pk_parse_key( &pk, buf->x, buf->len, NULL, 0 ) == ( result ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200129 if( ( result ) == 0 )
130 {
131 TEST_ASSERT( 1 );
132 }
133
Paul Bakkerbd51b262014-07-10 15:26:12 +0200134exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200135 mbedtls_pk_free( &pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200136}
137/* END_CASE */