libotr_test_helper.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. // Copyright 2012 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // This code can be compiled and used to test the otr package against libotr.
  5. // See otr_test.go.
  6. // +build ignore
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <unistd.h>
  10. #include <proto.h>
  11. #include <message.h>
  12. #include <privkey.h>
  13. static int g_session_established = 0;
  14. OtrlPolicy policy(void *opdata, ConnContext *context) {
  15. return OTRL_POLICY_ALWAYS;
  16. }
  17. int is_logged_in(void *opdata, const char *accountname, const char *protocol,
  18. const char *recipient) {
  19. return 1;
  20. }
  21. void inject_message(void *opdata, const char *accountname, const char *protocol,
  22. const char *recipient, const char *message) {
  23. printf("%s\n", message);
  24. fflush(stdout);
  25. fprintf(stderr, "libotr helper sent: %s\n", message);
  26. }
  27. void update_context_list(void *opdata) {}
  28. void new_fingerprint(void *opdata, OtrlUserState us, const char *accountname,
  29. const char *protocol, const char *username,
  30. unsigned char fingerprint[20]) {
  31. fprintf(stderr, "NEW FINGERPRINT\n");
  32. g_session_established = 1;
  33. }
  34. void write_fingerprints(void *opdata) {}
  35. void gone_secure(void *opdata, ConnContext *context) {}
  36. void gone_insecure(void *opdata, ConnContext *context) {}
  37. void still_secure(void *opdata, ConnContext *context, int is_reply) {}
  38. int max_message_size(void *opdata, ConnContext *context) { return 99999; }
  39. const char *account_name(void *opdata, const char *account,
  40. const char *protocol) {
  41. return "ACCOUNT";
  42. }
  43. void account_name_free(void *opdata, const char *account_name) {}
  44. const char *error_message(void *opdata, ConnContext *context,
  45. OtrlErrorCode err_code) {
  46. return "ERR";
  47. }
  48. void error_message_free(void *opdata, const char *msg) {}
  49. void resent_msg_prefix_free(void *opdata, const char *prefix) {}
  50. void handle_smp_event(void *opdata, OtrlSMPEvent smp_event,
  51. ConnContext *context, unsigned short progress_event,
  52. char *question) {}
  53. void handle_msg_event(void *opdata, OtrlMessageEvent msg_event,
  54. ConnContext *context, const char *message,
  55. gcry_error_t err) {
  56. fprintf(stderr, "msg event: %d %s\n", msg_event, message);
  57. }
  58. OtrlMessageAppOps uiops = {
  59. policy,
  60. NULL,
  61. is_logged_in,
  62. inject_message,
  63. update_context_list,
  64. new_fingerprint,
  65. write_fingerprints,
  66. gone_secure,
  67. gone_insecure,
  68. still_secure,
  69. max_message_size,
  70. account_name,
  71. account_name_free,
  72. NULL, /* received_symkey */
  73. error_message,
  74. error_message_free,
  75. NULL, /* resent_msg_prefix */
  76. resent_msg_prefix_free,
  77. handle_smp_event,
  78. handle_msg_event,
  79. NULL /* create_instag */,
  80. NULL /* convert_msg */,
  81. NULL /* convert_free */,
  82. NULL /* timer_control */,
  83. };
  84. static const char kPrivateKeyData[] =
  85. "(privkeys (account (name \"account\") (protocol proto) (private-key (dsa "
  86. "(p "
  87. "#00FC07ABCF0DC916AFF6E9AE47BEF60C7AB9B4D6B2469E436630E36F8A489BE812486A09F"
  88. "30B71224508654940A835301ACC525A4FF133FC152CC53DCC59D65C30A54F1993FE13FE63E"
  89. "5823D4C746DB21B90F9B9C00B49EC7404AB1D929BA7FBA12F2E45C6E0A651689750E8528AB"
  90. "8C031D3561FECEE72EBB4A090D450A9B7A857#) (q "
  91. "#00997BD266EF7B1F60A5C23F3A741F2AEFD07A2081#) (g "
  92. "#535E360E8A95EBA46A4F7DE50AD6E9B2A6DB785A66B64EB9F20338D2A3E8FB0E94725848F"
  93. "1AA6CC567CB83A1CC517EC806F2E92EAE71457E80B2210A189B91250779434B41FC8A8873F"
  94. "6DB94BEA7D177F5D59E7E114EE10A49CFD9CEF88AE43387023B672927BA74B04EB6BBB5E57"
  95. "597766A2F9CE3857D7ACE3E1E3BC1FC6F26#) (y "
  96. "#0AC8670AD767D7A8D9D14CC1AC6744CD7D76F993B77FFD9E39DF01E5A6536EF65E775FCEF"
  97. "2A983E2A19BD6415500F6979715D9FD1257E1FE2B6F5E1E74B333079E7C880D39868462A93"
  98. "454B41877BE62E5EF0A041C2EE9C9E76BD1E12AE25D9628DECB097025DD625EF49C3258A1A"
  99. "3C0FF501E3DC673B76D7BABF349009B6ECF#) (x "
  100. "#14D0345A3562C480A039E3C72764F72D79043216#)))))\n";
  101. int main() {
  102. OTRL_INIT;
  103. // We have to write the private key information to a file because the libotr
  104. // API demands a filename to read from.
  105. const char *tmpdir = "/tmp";
  106. if (getenv("TMP")) {
  107. tmpdir = getenv("TMP");
  108. }
  109. char private_key_file[256];
  110. snprintf(private_key_file, sizeof(private_key_file),
  111. "%s/libotr_test_helper_privatekeys-XXXXXX", tmpdir);
  112. int fd = mkstemp(private_key_file);
  113. if (fd == -1) {
  114. perror("creating temp file");
  115. }
  116. write(fd, kPrivateKeyData, sizeof(kPrivateKeyData) - 1);
  117. close(fd);
  118. OtrlUserState userstate = otrl_userstate_create();
  119. otrl_privkey_read(userstate, private_key_file);
  120. unlink(private_key_file);
  121. fprintf(stderr, "libotr helper started\n");
  122. char buf[4096];
  123. for (;;) {
  124. char *message = fgets(buf, sizeof(buf), stdin);
  125. if (strlen(message) == 0) {
  126. break;
  127. }
  128. message[strlen(message) - 1] = 0;
  129. fprintf(stderr, "libotr helper got: %s\n", message);
  130. char *newmessage = NULL;
  131. OtrlTLV *tlvs;
  132. int ignore_message = otrl_message_receiving(
  133. userstate, &uiops, NULL, "account", "proto", "peer", message,
  134. &newmessage, &tlvs, NULL, NULL, NULL);
  135. if (tlvs) {
  136. otrl_tlv_free(tlvs);
  137. }
  138. if (newmessage != NULL) {
  139. fprintf(stderr, "libotr got: %s\n", newmessage);
  140. otrl_message_free(newmessage);
  141. gcry_error_t err;
  142. char *newmessage = NULL;
  143. err = otrl_message_sending(userstate, &uiops, NULL, "account", "proto",
  144. "peer", 0, "test message", NULL, &newmessage,
  145. OTRL_FRAGMENT_SEND_SKIP, NULL, NULL, NULL);
  146. if (newmessage == NULL) {
  147. fprintf(stderr, "libotr didn't encrypt message\n");
  148. return 1;
  149. }
  150. write(1, newmessage, strlen(newmessage));
  151. write(1, "\n", 1);
  152. fprintf(stderr, "libotr sent: %s\n", newmessage);
  153. otrl_message_free(newmessage);
  154. g_session_established = 0;
  155. write(1, "?OTRv2?\n", 8);
  156. fprintf(stderr, "libotr sent: ?OTRv2\n");
  157. }
  158. }
  159. return 0;
  160. }