blob: 4f1a616061f55ad04ac7289fb8638be917911074 [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 */
Paul Bakker1a7550a2013-09-15 13:01:22 +020013void pk_parse_keyfile_rsa( char *key_file, char *password, int result )
14{
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 */
Paul Bakker1a7550a2013-09-15 13:01:22 +020042void pk_parse_public_keyfile_rsa( char *key_file, int result )
43{
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 */
Paul Bakker1a7550a2013-09-15 13:01:22 +020067void pk_parse_public_keyfile_ec( char *key_file, int result )
68{
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 */
Paul Bakker1a7550a2013-09-15 13:01:22 +020092void pk_parse_keyfile_ec( char *key_file, char *password, int result )
93{
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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200116/* BEGIN_CASE depends_on:MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200117void pk_parse_key_rsa( char *key_data, char *result_str, int result )
118{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200119 mbedtls_pk_context pk;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200120 unsigned char buf[2000];
121 unsigned char output[2000];
122 int data_len;
123 ((void) result_str);
124
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200125 mbedtls_pk_init( &pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200126
127 memset( buf, 0, 2000 );
128 memset( output, 0, 2000 );
129
130 data_len = unhexify( buf, key_data );
131
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200132 TEST_ASSERT( mbedtls_pk_parse_key( &pk, buf, data_len, NULL, 0 ) == ( result ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200133 if( ( result ) == 0 )
134 {
135 TEST_ASSERT( 1 );
136 }
137
Paul Bakkerbd51b262014-07-10 15:26:12 +0200138exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200139 mbedtls_pk_free( &pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200140}
141/* END_CASE */