pem_test.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /* LibTomCrypt, modular cryptographic library -- Tom St Denis */
  2. /* SPDX-License-Identifier: Unlicense */
  3. #include <tomcrypt_test.h>
  4. #if defined(LTC_PEM) && defined(LTC_TEST_READDIR) && !defined(LTC_EASY)
  5. #ifdef LTC_SSH
  6. static int password_get_ssh(void **p, unsigned long *l, void *u)
  7. {
  8. LTC_UNUSED_PARAM(u);
  9. *p = strdup("abc123");
  10. *l = 6;
  11. return 0;
  12. }
  13. static int s_pem_decode_ssh(const void *in, unsigned long inlen, void *key)
  14. {
  15. password_ctx pw_ctx = { .callback = password_get_ssh };
  16. return pem_decode_openssh(in, inlen, key, &pw_ctx);
  17. }
  18. static int s_pem_decode_ssh_f(FILE *f, void *key)
  19. {
  20. password_ctx pw_ctx = { .callback = password_get_ssh };
  21. return pem_decode_openssh_filehandle(f, key, &pw_ctx);
  22. }
  23. int s_authorized_key_cb(ltc_pka_key *k, const char *comment, void *ctx)
  24. {
  25. LTC_UNUSED_PARAM(comment);
  26. LTC_UNUSED_PARAM(ctx);
  27. pka_key_destroy(&k);
  28. return 0;
  29. }
  30. static int s_read_authorized_keys(const void *in, unsigned long inlen, void *ctx)
  31. {
  32. return ssh_read_authorized_keys(in, inlen, s_authorized_key_cb, ctx);
  33. }
  34. static int s_read_authorized_keys_f(FILE *f, void *ctx)
  35. {
  36. return ssh_read_authorized_keys_filehandle(f, s_authorized_key_cb, ctx);
  37. }
  38. static int s_read_invalid_authorized_keys(const void *in, unsigned long inlen, void *ctx)
  39. {
  40. SHOULD_FAIL(ssh_read_authorized_keys(in, inlen, s_authorized_key_cb, ctx));
  41. return CRYPT_OK;
  42. }
  43. #endif
  44. static int password_get(void **p, unsigned long *l, void *u)
  45. {
  46. LTC_UNUSED_PARAM(u);
  47. *p = strdup("secret");
  48. *l = 6;
  49. return 0;
  50. }
  51. #if defined(LTC_MDSA)
  52. static dsa_key s_dsa_key_should;
  53. #endif
  54. #if defined(LTC_MRSA)
  55. static rsa_key s_rsa_key_should;
  56. #endif
  57. #if defined(LTC_MECC)
  58. static ecc_key s_ecc_key_should;
  59. #endif
  60. static int s_key_cmp(ltc_pka_key *key)
  61. {
  62. switch (key->id) {
  63. case LTC_PKA_DSA:
  64. #if defined(LTC_MDSA)
  65. return dsa_key_cmp(key->u.dsa.type, &s_dsa_key_should, &key->u.dsa);
  66. #endif
  67. break;
  68. case LTC_PKA_RSA:
  69. #if defined(LTC_MRSA)
  70. return rsa_key_cmp(key->u.rsa.type, &s_rsa_key_should, &key->u.rsa);
  71. #endif
  72. break;
  73. case LTC_PKA_EC:
  74. #if defined(LTC_MECC)
  75. return ecc_key_cmp(key->u.ecc.type, &s_ecc_key_should, &key->u.ecc);
  76. #endif
  77. break;
  78. case LTC_PKA_ED25519:
  79. case LTC_PKA_X25519:
  80. case LTC_PKA_DH:
  81. return CRYPT_OK;
  82. default:
  83. return CRYPT_INVALID_ARG;
  84. }
  85. return CRYPT_INVALID_ARG;
  86. }
  87. static int s_pem_only_decode_pkcs(const void *in, unsigned long inlen, void *key)
  88. {
  89. password_ctx pw_ctx = { .callback = password_get };
  90. return pem_decode_pkcs(in, inlen, key, &pw_ctx);
  91. }
  92. static int s_pem_decode_pkcs(const void *in, unsigned long inlen, void *key)
  93. {
  94. int err;
  95. if ((err = s_pem_only_decode_pkcs(in, inlen, key)) != CRYPT_OK) {
  96. return err;
  97. }
  98. return s_key_cmp(key);
  99. }
  100. static int s_pem_decode_pkcs_f(FILE *f, void *key)
  101. {
  102. int err;
  103. password_ctx pw_ctx = { .callback = password_get };
  104. if ((err = pem_decode_pkcs_filehandle(f, key, &pw_ctx)) != CRYPT_OK) {
  105. return err;
  106. }
  107. return s_key_cmp(key);
  108. }
  109. static int s_pem_only_decode(const void *in, unsigned long inlen, void *key)
  110. {
  111. password_ctx pw_ctx = { .callback = password_get };
  112. if ((strcmp(ltc_mp.name, "TomsFastMath") == 0) && (inlen > 2048)) {
  113. #if defined(LTC_TEST_DBG) && LTC_TEST_DBG > 1
  114. fprintf(stderr, "Skipping testcase because of TomsFastMath\n");
  115. #endif
  116. return CRYPT_NOP;
  117. }
  118. return pem_decode(in, inlen, key, &pw_ctx);
  119. }
  120. static int s_pem_only_decode_f(FILE *f, void *key)
  121. {
  122. password_ctx pw_ctx = { .callback = password_get };
  123. return pem_decode_filehandle(f, key, &pw_ctx);
  124. }
  125. int pem_test(void)
  126. {
  127. ltc_pka_key key;
  128. if (ltc_mp.name == NULL) return CRYPT_NOP;
  129. #if defined(LTC_MDSA)
  130. DO(dsa_import(ltc_dsa_private_test_key, ltc_dsa_private_test_key_sz, &s_dsa_key_should));
  131. #endif
  132. #if defined(LTC_MRSA)
  133. DO(rsa_import(ltc_rsa_private_test_key, ltc_rsa_private_test_key_sz, &s_rsa_key_should));
  134. #endif
  135. #if defined(LTC_MECC)
  136. DO(ecc_import_openssl(ltc_ecc_long_pri_test_key, ltc_ecc_long_pri_test_key_sz, &s_ecc_key_should));
  137. #endif
  138. DO(test_process_dir("tests/pem/pkcs", &key, s_pem_decode_pkcs, NULL, (dir_cleanup_cb)pka_key_free, "pem_pkcs_test"));
  139. DO(test_process_dir("tests/pem/pkcs", &key, NULL, s_pem_decode_pkcs_f, (dir_cleanup_cb)pka_key_free, "pem_pkcs_test_filehandle"));
  140. DO(test_process_dir("tests/pem/pkcs/ecc-pkcs8", &key, s_pem_decode_pkcs, NULL, (dir_cleanup_cb)pka_key_free, "pem_pkcs_test+ecc"));
  141. DO(test_process_dir("tests/pem/pkcs/ecc-pkcs8", &key, NULL, s_pem_decode_pkcs_f, (dir_cleanup_cb)pka_key_free, "pem_pkcs_test_filehandle+ecc"));
  142. DO(test_process_dir("tests/pem/pkcs/extra", &key, s_pem_only_decode_pkcs, NULL, (dir_cleanup_cb)pka_key_free, "pem_pkcs_test+extra"));
  143. #ifdef LTC_SSH
  144. DO(test_process_dir("tests/pem/ssh", &key, s_pem_decode_ssh, NULL, (dir_cleanup_cb)pka_key_free, "pem_ssh_test"));
  145. DO(test_process_dir("tests/pem/ssh", &key, NULL, s_pem_decode_ssh_f, (dir_cleanup_cb)pka_key_free, "pem_ssh_test_filehandle"));
  146. DO(test_process_dir("tests/pem/ssh/authorized_keys", &key, s_read_authorized_keys, NULL, (dir_cleanup_cb)pka_key_free, "pem_ssh_authorized_keys_test"));
  147. DO(test_process_dir("tests/pem/ssh/authorized_keys", &key, NULL, s_read_authorized_keys_f, (dir_cleanup_cb)pka_key_free, "pem_ssh_authorized_keys_test"));
  148. DO(test_process_dir("tests/pem/ssh/authorized_keys-invalid", &key, s_read_invalid_authorized_keys, NULL, NULL, "pem_ssh_authorized_keys_invalid_test"));
  149. DO(test_process_dir("tests/pem/ssh/extra", &key, s_pem_decode_ssh, NULL, (dir_cleanup_cb)pka_key_free, "pem_ssh_test+extra"));
  150. DO(test_process_dir("tests/pem/pubkeys", &key, s_pem_only_decode, NULL, (dir_cleanup_cb)pka_key_free, "pem_pubkeys_test"));
  151. DO(test_process_dir("tests/pem/pubkeys", &key, NULL, s_pem_only_decode_f, (dir_cleanup_cb)pka_key_free, "pem_pubkeys_test_filehandle"));
  152. DO(test_process_dir("tests/pem/pubkeys/authorized_keys", &key, s_read_authorized_keys, NULL, (dir_cleanup_cb)pka_key_free, "pem_pubkeys_authorized_keys_test"));
  153. DO(test_process_dir("tests/pem/pubkeys/authorized_keys", &key, NULL, s_read_authorized_keys_f, (dir_cleanup_cb)pka_key_free, "pem_pubkeys_authorized_keys_test"));
  154. #endif
  155. DO(test_process_dir("tests/pem", &key, s_pem_only_decode, NULL, (dir_cleanup_cb)pka_key_free, "pem_test"));
  156. DO(test_process_dir("tests/pem", &key, NULL, s_pem_only_decode_f, (dir_cleanup_cb)pka_key_free, "pem_test_filehandle"));
  157. if (strcmp(ltc_mp.name, "TomsFastMath") != 0) {
  158. DO(test_process_dir("tests/pem/non-tfm", &key, s_pem_only_decode, NULL, (dir_cleanup_cb)pka_key_free, "pem_test"));
  159. DO(test_process_dir("tests/pem/non-tfm", &key, NULL, s_pem_only_decode_f, (dir_cleanup_cb)pka_key_free, "pem_test_filehandle"));
  160. }
  161. #if defined(LTC_MDSA)
  162. dsa_free(&s_dsa_key_should);
  163. #endif
  164. #if defined(LTC_MRSA)
  165. rsa_free(&s_rsa_key_should);
  166. #endif
  167. #if defined(LTC_MECC)
  168. ecc_free(&s_ecc_key_should);
  169. #endif
  170. return 0;
  171. }
  172. #else
  173. int pem_test(void)
  174. {
  175. return CRYPT_NOP;
  176. }
  177. #endif