blob: 4c7f3d2caecf152c3d040835aa2145a71bf604d9 [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é-Gonnard84dea012021-06-15 11:29:26 +020024 res = mbedtls_pk_parse_keyfile( &ctx, key_file, pwd,
25 mbedtls_test_rnd_std_rand, NULL );
Paul Bakker1a7550a2013-09-15 13:01:22 +020026
27 TEST_ASSERT( res == result );
28
29 if( res == 0 )
30 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020031 mbedtls_rsa_context *rsa;
32 TEST_ASSERT( mbedtls_pk_can_do( &ctx, MBEDTLS_PK_RSA ) );
33 rsa = mbedtls_pk_rsa( ctx );
34 TEST_ASSERT( mbedtls_rsa_check_privkey( rsa ) == 0 );
Paul Bakker1a7550a2013-09-15 13:01:22 +020035 }
36
Paul Bakkerbd51b262014-07-10 15:26:12 +020037exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020038 mbedtls_pk_free( &ctx );
Paul Bakker1a7550a2013-09-15 13:01:22 +020039}
40/* END_CASE */
41
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020042/* BEGIN_CASE depends_on:MBEDTLS_RSA_C:MBEDTLS_FS_IO */
Azim Khanf1aaec92017-05-30 14:23:15 +010043void pk_parse_public_keyfile_rsa( char * key_file, int result )
Paul Bakker1a7550a2013-09-15 13:01:22 +020044{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020045 mbedtls_pk_context ctx;
Paul Bakker1a7550a2013-09-15 13:01:22 +020046 int res;
47
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020048 mbedtls_pk_init( &ctx );
Paul Bakker1a7550a2013-09-15 13:01:22 +020049
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020050 res = mbedtls_pk_parse_public_keyfile( &ctx, key_file );
Paul Bakker1a7550a2013-09-15 13:01:22 +020051
52 TEST_ASSERT( res == result );
53
54 if( res == 0 )
55 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020056 mbedtls_rsa_context *rsa;
57 TEST_ASSERT( mbedtls_pk_can_do( &ctx, MBEDTLS_PK_RSA ) );
58 rsa = mbedtls_pk_rsa( ctx );
59 TEST_ASSERT( mbedtls_rsa_check_pubkey( rsa ) == 0 );
Paul Bakker1a7550a2013-09-15 13:01:22 +020060 }
61
Paul Bakkerbd51b262014-07-10 15:26:12 +020062exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020063 mbedtls_pk_free( &ctx );
Paul Bakker1a7550a2013-09-15 13:01:22 +020064}
65/* END_CASE */
66
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020067/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_ECP_C */
Azim Khanf1aaec92017-05-30 14:23:15 +010068void pk_parse_public_keyfile_ec( char * key_file, int result )
Paul Bakker1a7550a2013-09-15 13:01:22 +020069{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020070 mbedtls_pk_context ctx;
Paul Bakker1a7550a2013-09-15 13:01:22 +020071 int res;
72
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020073 mbedtls_pk_init( &ctx );
Paul Bakker1a7550a2013-09-15 13:01:22 +020074
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020075 res = mbedtls_pk_parse_public_keyfile( &ctx, key_file );
Paul Bakker1a7550a2013-09-15 13:01:22 +020076
77 TEST_ASSERT( res == result );
78
79 if( res == 0 )
80 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020081 mbedtls_ecp_keypair *eckey;
82 TEST_ASSERT( mbedtls_pk_can_do( &ctx, MBEDTLS_PK_ECKEY ) );
83 eckey = mbedtls_pk_ec( ctx );
84 TEST_ASSERT( mbedtls_ecp_check_pubkey( &eckey->grp, &eckey->Q ) == 0 );
Paul Bakker1a7550a2013-09-15 13:01:22 +020085 }
86
Paul Bakkerbd51b262014-07-10 15:26:12 +020087exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020088 mbedtls_pk_free( &ctx );
Paul Bakker1a7550a2013-09-15 13:01:22 +020089}
90/* END_CASE */
91
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020092/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_ECP_C */
Azim Khanf1aaec92017-05-30 14:23:15 +010093void pk_parse_keyfile_ec( char * key_file, char * password, int result )
Paul Bakker1a7550a2013-09-15 13:01:22 +020094{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020095 mbedtls_pk_context ctx;
Paul Bakker1a7550a2013-09-15 13:01:22 +020096 int res;
97
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020098 mbedtls_pk_init( &ctx );
Paul Bakker1a7550a2013-09-15 13:01:22 +020099
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +0200100 res = mbedtls_pk_parse_keyfile( &ctx, key_file, password,
101 mbedtls_test_rnd_std_rand, NULL );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200102
103 TEST_ASSERT( res == result );
104
105 if( res == 0 )
106 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200107 mbedtls_ecp_keypair *eckey;
108 TEST_ASSERT( mbedtls_pk_can_do( &ctx, MBEDTLS_PK_ECKEY ) );
109 eckey = mbedtls_pk_ec( ctx );
110 TEST_ASSERT( mbedtls_ecp_check_privkey( &eckey->grp, &eckey->d ) == 0 );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200111 }
112
Paul Bakkerbd51b262014-07-10 15:26:12 +0200113exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200114 mbedtls_pk_free( &ctx );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200115}
116/* END_CASE */
117
Manuel Pégourié-Gonnardb65370f2020-02-10 10:50:16 +0100118/* BEGIN_CASE */
119void pk_parse_key( data_t * buf, int result )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200120{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200121 mbedtls_pk_context pk;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200122
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200123 mbedtls_pk_init( &pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200124
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +0200125 TEST_ASSERT( mbedtls_pk_parse_key( &pk, buf->x, buf->len, NULL, 0,
126 mbedtls_test_rnd_std_rand, NULL ) == result );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200127
Paul Bakkerbd51b262014-07-10 15:26:12 +0200128exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200129 mbedtls_pk_free( &pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200130}
131/* END_CASE */