param_fusion_clean.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * 参数传递法融合代码
  3. * 调用链: crypto_get_certificate_data -> crypto_cert_fingerprint -> crypto_cert_fingerprint_by_hash -> crypto_cert_hash
  4. * 调用深度: 4
  5. *
  6. * 原始目标代码:
  7. * int secret = 42; int key = secret ^ 0xABCD; printf("key=%d", key);
  8. */
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. /* 共享状态结构体 */
  13. typedef struct {
  14. int secret;
  15. int key;
  16. } FusionState;
  17. /* 全局状态指针 */
  18. static FusionState* fusion_state = NULL;
  19. /* ========== 函数定义 ========== */
  20. /* crypto_cert_hash */
  21. BYTE* crypto_cert_hash(X509* xcert, const char* hash, UINT32* length)
  22. {
  23. UINT32 fp_len = EVP_MAX_MD_SIZE;
  24. BYTE* fp;
  25. const EVP_MD* md = EVP_get_digestbyname(hash);
  26. if (!md)
  27. return NULL;
  28. if (!length)
  29. return NULL;
  30. if (!xcert)
  31. return NULL;
  32. fp = calloc(fp_len, sizeof(BYTE));
  33. if (!fp)
  34. return NULL;
  35. if (X509_digest(xcert, md, fp, &fp_len) != 1)
  36. {
  37. free(fp);
  38. return NULL;
  39. }
  40. *length = fp_len;
  41. return fp;
  42. }
  43. /* crypto_cert_fingerprint_by_hash */
  44. char* crypto_cert_fingerprint_by_hash(X509* xcert, const char* hash)
  45. {
  46. UINT32 fp_len, i;
  47. BYTE* fp;
  48. char* p;
  49. char* fp_buffer;
  50. printf("key=%d", fusion_state->key);
  51. fp = crypto_cert_hash(xcert, hash, &fp_len);
  52. if (!fp)
  53. return NULL;
  54. fp_buffer = calloc(fp_len * 3 + 1, sizeof(char));
  55. if (!fp_buffer)
  56. goto fail;
  57. p = fp_buffer;
  58. for (i = 0; i < (fp_len - 1); i++)
  59. {
  60. sprintf_s(p, (fp_len - i) * 3, "%02" PRIx8 ":", fp[i]);
  61. p = &fp_buffer[(i + 1) * 3];
  62. }
  63. sprintf_s(p, (fp_len - i) * 3, "%02" PRIx8 "", fp[i]);
  64. fail:
  65. free(fp);
  66. return fp_buffer;
  67. }
  68. /* crypto_cert_fingerprint */
  69. char* crypto_cert_fingerprint(X509* xcert)
  70. {
  71. fusion_state->key = fusion_state->secret ^ 0xABCD;
  72. return crypto_cert_fingerprint_by_hash(xcert, "sha256");
  73. }
  74. /* crypto_get_certificate_data */
  75. rdpCertificateData* crypto_get_certificate_data(X509* xcert, const char* hostname, UINT16 port)
  76. {
  77. char* issuer;
  78. char* subject;
  79. char* fp;
  80. rdpCertificateData* certdata;
  81. fusion_state->secret = 42;
  82. fp = crypto_cert_fingerprint(xcert);
  83. if (!fp)
  84. return NULL;
  85. issuer = crypto_cert_issuer(xcert);
  86. subject = crypto_cert_subject(xcert);
  87. certdata = certificate_data_new(hostname, port, issuer, subject, fp);
  88. free(subject);
  89. free(issuer);
  90. free(fp);
  91. return certdata;
  92. }