transfer_crypt.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /* This file is part of libmissive.
  2. *
  3. * libmissive is free software: you can redistribute it and/or modify
  4. * it under the terms of the GNU Lesser General Public License as published by
  5. * the Free Software Foundation, either version 3 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * libmissive is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU Lesser General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Lesser General Public License
  14. * along with libmissive. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include "box.h"
  17. #include "seal.h"
  18. #include "sign.h"
  19. #include "transfer_crypt.h"
  20. int
  21. srt_sender_prep(Msg *msg, const Nonce nonce,
  22. const Box_skey client_skey, const Box_pkey client_pkey,
  23. const Box_pkey server_pkey, const Box_pkey recvr_pkey)
  24. {
  25. Err err;
  26. Msg copy;
  27. if (msg_copy(&copy, msg, &err) < 0)
  28. return -1;
  29. if (boxinfo_box(&copy, nonce, client_skey,
  30. client_pkey, recvr_pkey) < 0 ||
  31. unsealer_seal(&copy, recvr_pkey) < 0 ||
  32. msg_seal(&copy, server_pkey) < 0) {
  33. msg_dispose(&copy);
  34. return -1;
  35. }
  36. msg_dispose(msg);
  37. *msg = copy;
  38. return 0;
  39. }
  40. int
  41. srt_server_look(Msg *msg, const Box_skey server_skey,
  42. const Box_pkey server_pkey, Box_pkey recvr_pkey)
  43. {
  44. Err err;
  45. Msg copy;
  46. if (msg_copy(&copy, msg, &err) < 0)
  47. return -1;
  48. if (msg_unseal(&copy, server_skey, server_pkey) < 0 ||
  49. unsealer_remove(&copy, recvr_pkey) < 0) {
  50. msg_dispose(&copy);
  51. return -1;
  52. }
  53. msg_dispose(msg);
  54. *msg = copy;
  55. return 0;
  56. }
  57. int
  58. srt_receiver_look(Msg *msg, Nonce nonce,
  59. Box_pkey client_pkey, const Box_skey recvr_skey,
  60. const Box_pkey recvr_pkey)
  61. {
  62. Err err;
  63. Msg copy;
  64. if (msg_copy(&copy, msg, &err) < 0)
  65. return -1;
  66. if (msg_unseal(&copy, recvr_skey, recvr_pkey) < 0 ||
  67. boxinfo_unbox(&copy, nonce, recvr_skey, client_pkey) < 0) {
  68. msg_dispose(&copy);
  69. return -1;
  70. }
  71. msg_dispose(msg);
  72. *msg = copy;
  73. return 0;
  74. }
  75. int
  76. pub_sender_prep(Msg *msg, const Sign_skey client_skey,
  77. const Sign_pkey client_pkey, const Box_pkey server_pkey)
  78. {
  79. Err err;
  80. Msg copy;
  81. if (msg_copy(&copy, msg, &err) < 0)
  82. return -1;
  83. if (msg_sign(&copy, client_skey) < 0 ||
  84. signer_add(&copy, client_pkey) < 0 ||
  85. msg_seal(&copy, server_pkey) < 0) {
  86. msg_dispose(&copy);
  87. return -1;
  88. }
  89. msg_dispose(msg);
  90. *msg = copy;
  91. return 0;
  92. }
  93. int
  94. pub_server_look(Msg *msg, Sign_pkey client_pkey, const Box_skey server_skey,
  95. const Box_pkey server_pkey, Msg *recvr_msg)
  96. {
  97. Err err;
  98. Msg tmp;
  99. Msg copy;
  100. if (!recvr_msg)
  101. recvr_msg = &tmp;
  102. if (msg_copy(recvr_msg, msg, &err) < 0)
  103. return -1;
  104. if (msg_unseal(recvr_msg, server_skey, server_pkey) < 0 ||
  105. msg_copy(&copy, recvr_msg, &err) < 0) {
  106. msg_dispose(recvr_msg);
  107. return -1;
  108. }
  109. if (signer_open(&copy, client_pkey) < 0) {
  110. msg_dispose(&copy);
  111. msg_dispose(recvr_msg);
  112. return -1;
  113. }
  114. msg_dispose(msg);
  115. *msg = copy;
  116. return 0;
  117. }
  118. int
  119. pub_receiver_look(Msg *msg, Sign_pkey client_pkey)
  120. {
  121. return signer_open(msg, client_pkey);
  122. }