blob: 0a7798836d2b22d4747269ffa0d69f6b789a7f4a [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"
Przemek Stekielbc0509a2022-08-10 15:10:15 +02005#include "legacy_or_psa.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +02006/* END_HEADER */
7
8/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009 * depends_on:MBEDTLS_PK_PARSE_C:MBEDTLS_BIGNUM_C
Paul Bakker1a7550a2013-09-15 13:01:22 +020010 * END_DEPENDENCIES
11 */
12
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020013/* BEGIN_CASE depends_on:MBEDTLS_RSA_C:MBEDTLS_FS_IO */
Azim Khanf1aaec92017-05-30 14:23:15 +010014void pk_parse_keyfile_rsa( char * key_file, char * password, int result )
Paul Bakker1a7550a2013-09-15 13:01:22 +020015{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020016 mbedtls_pk_context ctx;
Paul Bakker1a7550a2013-09-15 13:01:22 +020017 int res;
18 char *pwd = password;
19
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020020 mbedtls_pk_init( &ctx );
Paul Bakker1a7550a2013-09-15 13:01:22 +020021
22 if( strcmp( pwd, "NULL" ) == 0 )
23 pwd = NULL;
24
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +020025 res = mbedtls_pk_parse_keyfile( &ctx, key_file, pwd,
26 mbedtls_test_rnd_std_rand, NULL );
Paul Bakker1a7550a2013-09-15 13:01:22 +020027
28 TEST_ASSERT( res == result );
29
30 if( res == 0 )
31 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020032 mbedtls_rsa_context *rsa;
33 TEST_ASSERT( mbedtls_pk_can_do( &ctx, MBEDTLS_PK_RSA ) );
34 rsa = mbedtls_pk_rsa( ctx );
35 TEST_ASSERT( mbedtls_rsa_check_privkey( rsa ) == 0 );
Paul Bakker1a7550a2013-09-15 13:01:22 +020036 }
37
Paul Bakkerbd51b262014-07-10 15:26:12 +020038exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020039 mbedtls_pk_free( &ctx );
Paul Bakker1a7550a2013-09-15 13:01:22 +020040}
41/* END_CASE */
42
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020043/* BEGIN_CASE depends_on:MBEDTLS_RSA_C:MBEDTLS_FS_IO */
Azim Khanf1aaec92017-05-30 14:23:15 +010044void pk_parse_public_keyfile_rsa( char * key_file, int result )
Paul Bakker1a7550a2013-09-15 13:01:22 +020045{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020046 mbedtls_pk_context ctx;
Paul Bakker1a7550a2013-09-15 13:01:22 +020047 int res;
48
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049 mbedtls_pk_init( &ctx );
Paul Bakker1a7550a2013-09-15 13:01:22 +020050
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020051 res = mbedtls_pk_parse_public_keyfile( &ctx, key_file );
Paul Bakker1a7550a2013-09-15 13:01:22 +020052
53 TEST_ASSERT( res == result );
54
55 if( res == 0 )
56 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020057 mbedtls_rsa_context *rsa;
58 TEST_ASSERT( mbedtls_pk_can_do( &ctx, MBEDTLS_PK_RSA ) );
59 rsa = mbedtls_pk_rsa( ctx );
60 TEST_ASSERT( mbedtls_rsa_check_pubkey( rsa ) == 0 );
Paul Bakker1a7550a2013-09-15 13:01:22 +020061 }
62
Paul Bakkerbd51b262014-07-10 15:26:12 +020063exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020064 mbedtls_pk_free( &ctx );
Paul Bakker1a7550a2013-09-15 13:01:22 +020065}
66/* END_CASE */
67
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020068/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_ECP_C */
Azim Khanf1aaec92017-05-30 14:23:15 +010069void pk_parse_public_keyfile_ec( char * key_file, int result )
Paul Bakker1a7550a2013-09-15 13:01:22 +020070{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020071 mbedtls_pk_context ctx;
Paul Bakker1a7550a2013-09-15 13:01:22 +020072 int res;
73
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020074 mbedtls_pk_init( &ctx );
Paul Bakker1a7550a2013-09-15 13:01:22 +020075
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020076 res = mbedtls_pk_parse_public_keyfile( &ctx, key_file );
Paul Bakker1a7550a2013-09-15 13:01:22 +020077
78 TEST_ASSERT( res == result );
79
80 if( res == 0 )
81 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020082 mbedtls_ecp_keypair *eckey;
83 TEST_ASSERT( mbedtls_pk_can_do( &ctx, MBEDTLS_PK_ECKEY ) );
84 eckey = mbedtls_pk_ec( ctx );
85 TEST_ASSERT( mbedtls_ecp_check_pubkey( &eckey->grp, &eckey->Q ) == 0 );
Paul Bakker1a7550a2013-09-15 13:01:22 +020086 }
87
Paul Bakkerbd51b262014-07-10 15:26:12 +020088exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020089 mbedtls_pk_free( &ctx );
Paul Bakker1a7550a2013-09-15 13:01:22 +020090}
91/* END_CASE */
92
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020093/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_ECP_C */
Azim Khanf1aaec92017-05-30 14:23:15 +010094void pk_parse_keyfile_ec( char * key_file, char * password, int result )
Paul Bakker1a7550a2013-09-15 13:01:22 +020095{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020096 mbedtls_pk_context ctx;
Paul Bakker1a7550a2013-09-15 13:01:22 +020097 int res;
98
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020099 mbedtls_pk_init( &ctx );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200100
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +0200101 res = mbedtls_pk_parse_keyfile( &ctx, key_file, password,
102 mbedtls_test_rnd_std_rand, NULL );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200103
104 TEST_ASSERT( res == result );
105
106 if( res == 0 )
107 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200108 mbedtls_ecp_keypair *eckey;
109 TEST_ASSERT( mbedtls_pk_can_do( &ctx, MBEDTLS_PK_ECKEY ) );
110 eckey = mbedtls_pk_ec( ctx );
111 TEST_ASSERT( mbedtls_ecp_check_privkey( &eckey->grp, &eckey->d ) == 0 );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200112 }
113
Paul Bakkerbd51b262014-07-10 15:26:12 +0200114exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200115 mbedtls_pk_free( &ctx );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200116}
117/* END_CASE */
118
Manuel Pégourié-Gonnardb65370f2020-02-10 10:50:16 +0100119/* BEGIN_CASE */
120void pk_parse_key( data_t * buf, int result )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200121{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200122 mbedtls_pk_context pk;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200123
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200124 mbedtls_pk_init( &pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200125
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +0200126 TEST_ASSERT( mbedtls_pk_parse_key( &pk, buf->x, buf->len, NULL, 0,
127 mbedtls_test_rnd_std_rand, NULL ) == result );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200128
Paul Bakkerbd51b262014-07-10 15:26:12 +0200129exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200130 mbedtls_pk_free( &pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200131}
132/* END_CASE */