pem_test.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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_decode_invalid_pkcs(const void *in, unsigned long inlen, void *key)
  88. {
  89. password_ctx pw_ctx = { .callback = password_get };
  90. SHOULD_FAIL(pem_decode_pkcs(in, inlen, key, &pw_ctx));
  91. return CRYPT_OK;
  92. }
  93. static int s_pem_only_decode_pkcs(const void *in, unsigned long inlen, void *key)
  94. {
  95. password_ctx pw_ctx = { .callback = password_get };
  96. return pem_decode_pkcs(in, inlen, key, &pw_ctx);
  97. }
  98. static int s_pem_decode_pkcs(const void *in, unsigned long inlen, void *key)
  99. {
  100. int err;
  101. if ((err = s_pem_only_decode_pkcs(in, inlen, key)) != CRYPT_OK) {
  102. return err;
  103. }
  104. return s_key_cmp(key);
  105. }
  106. static int s_pem_decode_pkcs_f(FILE *f, void *key)
  107. {
  108. int err;
  109. password_ctx pw_ctx = { .callback = password_get };
  110. if ((err = pem_decode_pkcs_filehandle(f, key, &pw_ctx)) != CRYPT_OK) {
  111. return err;
  112. }
  113. return s_key_cmp(key);
  114. }
  115. static int s_pem_only_decode(const void *in, unsigned long inlen, void *key)
  116. {
  117. password_ctx pw_ctx = { .callback = password_get };
  118. if ((strcmp(ltc_mp.name, "TomsFastMath") == 0) && (inlen > 2048)) {
  119. #if defined(LTC_TEST_DBG) && LTC_TEST_DBG > 1
  120. fprintf(stderr, "Skipping testcase because of TomsFastMath\n");
  121. #endif
  122. return CRYPT_NOP;
  123. }
  124. return pem_decode(in, inlen, key, &pw_ctx);
  125. }
  126. static int s_pem_only_decode_f(FILE *f, void *key)
  127. {
  128. password_ctx pw_ctx = { .callback = password_get };
  129. return pem_decode_filehandle(f, key, &pw_ctx);
  130. }
  131. int pem_test(void)
  132. {
  133. ltc_pka_key key;
  134. if (ltc_mp.name == NULL) return CRYPT_NOP;
  135. #if defined(LTC_MDSA)
  136. DO(dsa_import(ltc_dsa_private_test_key, ltc_dsa_private_test_key_sz, &s_dsa_key_should));
  137. #endif
  138. #if defined(LTC_MRSA)
  139. DO(rsa_import(ltc_rsa_private_test_key, ltc_rsa_private_test_key_sz, &s_rsa_key_should));
  140. #endif
  141. #if defined(LTC_MECC)
  142. DO(ecc_import_openssl(ltc_ecc_long_pri_test_key, ltc_ecc_long_pri_test_key_sz, &s_ecc_key_should));
  143. #endif
  144. DO(test_process_dir("tests/pem/pkcs", &key, s_pem_decode_pkcs, NULL, (dir_cleanup_cb)pka_key_free, "pem_pkcs_test"));
  145. DO(test_process_dir("tests/pem/pkcs", &key, NULL, s_pem_decode_pkcs_f, (dir_cleanup_cb)pka_key_free, "pem_pkcs_test_filehandle"));
  146. 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"));
  147. 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"));
  148. 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"));
  149. DO(test_process_dir("tests/pem/pkcs/invalid", &key, s_pem_decode_invalid_pkcs, NULL, NULL, "pem_test_invalid"));
  150. DO(test_process_dir("tests/pem/pkcs/invalid_but_supported", &key, s_pem_only_decode_pkcs, NULL, (dir_cleanup_cb)pka_key_free, "pem_pkcs_invalid_but_supported"));
  151. #ifdef LTC_SSH
  152. DO(test_process_dir("tests/pem/ssh", &key, s_pem_decode_ssh, NULL, (dir_cleanup_cb)pka_key_free, "pem_ssh_test"));
  153. DO(test_process_dir("tests/pem/ssh", &key, NULL, s_pem_decode_ssh_f, (dir_cleanup_cb)pka_key_free, "pem_ssh_test_filehandle"));
  154. 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"));
  155. 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"));
  156. DO(test_process_dir("tests/pem/ssh/authorized_keys-invalid", &key, s_read_invalid_authorized_keys, NULL, NULL, "pem_ssh_authorized_keys_invalid_test"));
  157. DO(test_process_dir("tests/pem/ssh/extra", &key, s_pem_decode_ssh, NULL, (dir_cleanup_cb)pka_key_free, "pem_ssh_test+extra"));
  158. DO(test_process_dir("tests/pem/pubkeys", &key, s_pem_only_decode, NULL, (dir_cleanup_cb)pka_key_free, "pem_pubkeys_test"));
  159. DO(test_process_dir("tests/pem/pubkeys", &key, NULL, s_pem_only_decode_f, (dir_cleanup_cb)pka_key_free, "pem_pubkeys_test_filehandle"));
  160. 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"));
  161. 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"));
  162. #endif
  163. DO(test_process_dir("tests/pem", &key, s_pem_only_decode, NULL, (dir_cleanup_cb)pka_key_free, "pem_test"));
  164. DO(test_process_dir("tests/pem", &key, NULL, s_pem_only_decode_f, (dir_cleanup_cb)pka_key_free, "pem_test_filehandle"));
  165. if (strcmp(ltc_mp.name, "TomsFastMath") != 0) {
  166. DO(test_process_dir("tests/pem/non-tfm", &key, s_pem_only_decode, NULL, (dir_cleanup_cb)pka_key_free, "pem_test"));
  167. DO(test_process_dir("tests/pem/non-tfm", &key, NULL, s_pem_only_decode_f, (dir_cleanup_cb)pka_key_free, "pem_test_filehandle"));
  168. }
  169. #if defined(LTC_MDSA)
  170. dsa_free(&s_dsa_key_should);
  171. #endif
  172. #if defined(LTC_MRSA)
  173. rsa_free(&s_rsa_key_should);
  174. #endif
  175. #if defined(LTC_MECC)
  176. ecc_free(&s_ecc_key_should);
  177. #endif
  178. return 0;
  179. }
  180. #else
  181. int pem_test(void)
  182. {
  183. return CRYPT_NOP;
  184. }
  185. #endif