libotr_test_helper.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. static int g_session_established = 0;
  13. OtrlPolicy policy(void *opdata, ConnContext *context) {
  14. return OTRL_POLICY_ALWAYS;
  15. }
  16. int is_logged_in(void *opdata, const char *accountname, const char *protocol, const char *recipient) {
  17. return 1;
  18. }
  19. void inject_message(void *opdata, const char *accountname, const char *protocol, const char *recipient, const char *message) {
  20. printf("%s\n", message);
  21. fflush(stdout);
  22. fprintf(stderr, "libotr helper sent: %s\n", message);
  23. }
  24. void notify(void *opdata, OtrlNotifyLevel level, const char *accountname, const char *protocol, const char *username, const char *title, const char *primary, const char *secondary) {
  25. fprintf(stderr, "NOTIFY: %s %s %s %s\n", username, title, primary, secondary);
  26. }
  27. int display_otr_message(void *opdata, const char *accountname, const char *protocol, const char *username, const char *msg) {
  28. fprintf(stderr, "MESSAGE: %s %s\n", username, msg);
  29. return 1;
  30. }
  31. void update_context_list(void *opdata) {
  32. }
  33. const char *protocol_name(void *opdata, const char *protocol) {
  34. return "PROTOCOL";
  35. }
  36. void protocol_name_free(void *opdata, const char *protocol_name) {
  37. }
  38. void new_fingerprint(void *opdata, OtrlUserState us, const char *accountname, const char *protocol, const char *username, unsigned char fingerprint[20]) {
  39. fprintf(stderr, "NEW FINGERPRINT\n");
  40. g_session_established = 1;
  41. }
  42. void write_fingerprints(void *opdata) {
  43. }
  44. void gone_secure(void *opdata, ConnContext *context) {
  45. }
  46. void gone_insecure(void *opdata, ConnContext *context) {
  47. }
  48. void still_secure(void *opdata, ConnContext *context, int is_reply) {
  49. }
  50. void log_message(void *opdata, const char *message) {
  51. fprintf(stderr, "MESSAGE: %s\n", message);
  52. }
  53. int max_message_size(void *opdata, ConnContext *context) {
  54. return 99999;
  55. }
  56. const char *account_name(void *opdata, const char *account, const char *protocol) {
  57. return "ACCOUNT";
  58. }
  59. void account_name_free(void *opdata, const char *account_name) {
  60. }
  61. OtrlMessageAppOps uiops = {
  62. policy,
  63. NULL,
  64. is_logged_in,
  65. inject_message,
  66. notify,
  67. display_otr_message,
  68. update_context_list,
  69. protocol_name,
  70. protocol_name_free,
  71. new_fingerprint,
  72. write_fingerprints,
  73. gone_secure,
  74. gone_insecure,
  75. still_secure,
  76. log_message,
  77. max_message_size,
  78. account_name,
  79. account_name_free,
  80. };
  81. static const char kPrivateKeyData[] = "(privkeys (account (name \"account\") (protocol proto) (private-key (dsa (p #00FC07ABCF0DC916AFF6E9AE47BEF60C7AB9B4D6B2469E436630E36F8A489BE812486A09F30B71224508654940A835301ACC525A4FF133FC152CC53DCC59D65C30A54F1993FE13FE63E5823D4C746DB21B90F9B9C00B49EC7404AB1D929BA7FBA12F2E45C6E0A651689750E8528AB8C031D3561FECEE72EBB4A090D450A9B7A857#) (q #00997BD266EF7B1F60A5C23F3A741F2AEFD07A2081#) (g #535E360E8A95EBA46A4F7DE50AD6E9B2A6DB785A66B64EB9F20338D2A3E8FB0E94725848F1AA6CC567CB83A1CC517EC806F2E92EAE71457E80B2210A189B91250779434B41FC8A8873F6DB94BEA7D177F5D59E7E114EE10A49CFD9CEF88AE43387023B672927BA74B04EB6BBB5E57597766A2F9CE3857D7ACE3E1E3BC1FC6F26#) (y #0AC8670AD767D7A8D9D14CC1AC6744CD7D76F993B77FFD9E39DF01E5A6536EF65E775FCEF2A983E2A19BD6415500F6979715D9FD1257E1FE2B6F5E1E74B333079E7C880D39868462A93454B41877BE62E5EF0A041C2EE9C9E76BD1E12AE25D9628DECB097025DD625EF49C3258A1A3C0FF501E3DC673B76D7BABF349009B6ECF#) (x #14D0345A3562C480A039E3C72764F72D79043216#)))))\n";
  82. int
  83. main() {
  84. OTRL_INIT;
  85. // We have to write the private key information to a file because the libotr
  86. // API demands a filename to read from.
  87. const char *tmpdir = "/tmp";
  88. if (getenv("TMP")) {
  89. tmpdir = getenv("TMP");
  90. }
  91. char private_key_file[256];
  92. snprintf(private_key_file, sizeof(private_key_file), "%s/libotr_test_helper_privatekeys-XXXXXX", tmpdir);
  93. int fd = mkstemp(private_key_file);
  94. if (fd == -1) {
  95. perror("creating temp file");
  96. }
  97. write(fd, kPrivateKeyData, sizeof(kPrivateKeyData)-1);
  98. close(fd);
  99. OtrlUserState userstate = otrl_userstate_create();
  100. otrl_privkey_read(userstate, private_key_file);
  101. unlink(private_key_file);
  102. fprintf(stderr, "libotr helper started\n");
  103. char buf[4096];
  104. for (;;) {
  105. char* message = fgets(buf, sizeof(buf), stdin);
  106. if (strlen(message) == 0) {
  107. break;
  108. }
  109. message[strlen(message) - 1] = 0;
  110. fprintf(stderr, "libotr helper got: %s\n", message);
  111. char *newmessage = NULL;
  112. OtrlTLV *tlvs;
  113. int ignore_message = otrl_message_receiving(userstate, &uiops, NULL, "account", "proto", "peer", message, &newmessage, &tlvs, NULL, NULL);
  114. if (tlvs) {
  115. otrl_tlv_free(tlvs);
  116. }
  117. if (newmessage != NULL) {
  118. fprintf(stderr, "libotr got: %s\n", newmessage);
  119. otrl_message_free(newmessage);
  120. gcry_error_t err;
  121. char *newmessage = NULL;
  122. err = otrl_message_sending(userstate, &uiops, NULL, "account", "proto", "peer", "test message", NULL, &newmessage, NULL, NULL);
  123. if (newmessage == NULL) {
  124. fprintf(stderr, "libotr didn't encrypt message\n");
  125. return 1;
  126. }
  127. write(1, newmessage, strlen(newmessage));
  128. write(1, "\n", 1);
  129. g_session_established = 0;
  130. otrl_message_free(newmessage);
  131. write(1, "?OTRv2?\n", 8);
  132. }
  133. }
  134. return 0;
  135. }