test_suite_pkparse.function 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /* BEGIN_HEADER */
  2. #include "mbedtls/pk.h"
  3. #include "mbedtls/pem.h"
  4. #include "mbedtls/oid.h"
  5. /* END_HEADER */
  6. /* BEGIN_DEPENDENCIES
  7. * depends_on:MBEDTLS_PK_PARSE_C:MBEDTLS_BIGNUM_C
  8. * END_DEPENDENCIES
  9. */
  10. /* BEGIN_CASE depends_on:MBEDTLS_RSA_C:MBEDTLS_FS_IO */
  11. void pk_parse_keyfile_rsa( char * key_file, char * password, int result )
  12. {
  13. mbedtls_pk_context ctx;
  14. int res;
  15. char *pwd = password;
  16. mbedtls_pk_init( &ctx );
  17. if( strcmp( pwd, "NULL" ) == 0 )
  18. pwd = NULL;
  19. res = mbedtls_pk_parse_keyfile( &ctx, key_file, pwd );
  20. TEST_ASSERT( res == result );
  21. if( res == 0 )
  22. {
  23. mbedtls_rsa_context *rsa;
  24. TEST_ASSERT( mbedtls_pk_can_do( &ctx, MBEDTLS_PK_RSA ) );
  25. rsa = mbedtls_pk_rsa( ctx );
  26. TEST_ASSERT( mbedtls_rsa_check_privkey( rsa ) == 0 );
  27. }
  28. exit:
  29. mbedtls_pk_free( &ctx );
  30. }
  31. /* END_CASE */
  32. /* BEGIN_CASE depends_on:MBEDTLS_RSA_C:MBEDTLS_FS_IO */
  33. void pk_parse_public_keyfile_rsa( char * key_file, int result )
  34. {
  35. mbedtls_pk_context ctx;
  36. int res;
  37. mbedtls_pk_init( &ctx );
  38. res = mbedtls_pk_parse_public_keyfile( &ctx, key_file );
  39. TEST_ASSERT( res == result );
  40. if( res == 0 )
  41. {
  42. mbedtls_rsa_context *rsa;
  43. TEST_ASSERT( mbedtls_pk_can_do( &ctx, MBEDTLS_PK_RSA ) );
  44. rsa = mbedtls_pk_rsa( ctx );
  45. TEST_ASSERT( mbedtls_rsa_check_pubkey( rsa ) == 0 );
  46. }
  47. exit:
  48. mbedtls_pk_free( &ctx );
  49. }
  50. /* END_CASE */
  51. /* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_ECP_C */
  52. void pk_parse_public_keyfile_ec( char * key_file, int result )
  53. {
  54. mbedtls_pk_context ctx;
  55. int res;
  56. mbedtls_pk_init( &ctx );
  57. res = mbedtls_pk_parse_public_keyfile( &ctx, key_file );
  58. TEST_ASSERT( res == result );
  59. if( res == 0 )
  60. {
  61. mbedtls_ecp_keypair *eckey;
  62. TEST_ASSERT( mbedtls_pk_can_do( &ctx, MBEDTLS_PK_ECKEY ) );
  63. eckey = mbedtls_pk_ec( ctx );
  64. TEST_ASSERT( mbedtls_ecp_check_pubkey( &eckey->grp, &eckey->Q ) == 0 );
  65. }
  66. exit:
  67. mbedtls_pk_free( &ctx );
  68. }
  69. /* END_CASE */
  70. /* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_ECP_C */
  71. void pk_parse_keyfile_ec( char * key_file, char * password, int result )
  72. {
  73. mbedtls_pk_context ctx;
  74. int res;
  75. mbedtls_pk_init( &ctx );
  76. res = mbedtls_pk_parse_keyfile( &ctx, key_file, password );
  77. TEST_ASSERT( res == result );
  78. if( res == 0 )
  79. {
  80. 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_privkey( &eckey->grp, &eckey->d ) == 0 );
  84. }
  85. exit:
  86. mbedtls_pk_free( &ctx );
  87. }
  88. /* END_CASE */
  89. /* BEGIN_CASE */
  90. void pk_parse_key( data_t * buf, int result )
  91. {
  92. mbedtls_pk_context pk;
  93. mbedtls_pk_init( &pk );
  94. TEST_ASSERT( mbedtls_pk_parse_key( &pk, buf->x, buf->len, NULL, 0 ) == result );
  95. exit:
  96. mbedtls_pk_free( &pk );
  97. }
  98. /* END_CASE */