PeerChat.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. /**
  2. * @file PeerChat.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * This file is part of BadVPN.
  8. *
  9. * BadVPN is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2
  11. * as published by the Free Software Foundation.
  12. *
  13. * BadVPN is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. */
  22. #include <string.h>
  23. #include <ssl.h>
  24. #include <sslerr.h>
  25. #include <misc/byteorder.h>
  26. #include <base/BLog.h>
  27. #include <security/BRandom.h>
  28. #include "PeerChat.h"
  29. #include <generated/blog_channel_PeerChat.h>
  30. #define PeerLog(_o, ...) ((_o)->logfunc((_o)->user), BLog_LogToChannel(BLOG_CURRENT_CHANNEL, __VA_ARGS__))
  31. static void report_error (PeerChat *o)
  32. {
  33. DebugError_AssertNoError(&o->d_err);
  34. DEBUGERROR(&o->d_err, o->handler_error(o->user))
  35. return;
  36. }
  37. static void recv_job_handler (PeerChat *o)
  38. {
  39. DebugObject_Access(&o->d_obj);
  40. DebugError_AssertNoError(&o->d_err);
  41. ASSERT(o->recv_data_len >= 0)
  42. ASSERT(o->recv_data_len <= SC_MAX_MSGLEN)
  43. int data_len = o->recv_data_len;
  44. // set no received data
  45. o->recv_data_len = -1;
  46. #ifdef PEERCHAT_SIMULATE_ERROR
  47. uint8_t x;
  48. BRandom_randomize(&x, sizeof(x));
  49. if (x < PEERCHAT_SIMULATE_ERROR) {
  50. PeerLog(o, BLOG_ERROR, "simulate error");
  51. report_error(o);
  52. return;
  53. }
  54. #endif
  55. if (o->ssl_mode != PEERCHAT_SSL_NONE) {
  56. // buffer data
  57. if (!SimpleStreamBuffer_Write(&o->ssl_recv_buf, o->recv_data, data_len)) {
  58. PeerLog(o, BLOG_ERROR, "out of recv buffer");
  59. report_error(o);
  60. return;
  61. }
  62. } else {
  63. // call message handler
  64. o->handler_message(o->user, o->recv_data, data_len);
  65. return;
  66. }
  67. }
  68. static void ssl_con_handler (PeerChat *o, int event)
  69. {
  70. DebugObject_Access(&o->d_obj);
  71. DebugError_AssertNoError(&o->d_err);
  72. ASSERT(o->ssl_mode == PEERCHAT_SSL_CLIENT || o->ssl_mode == PEERCHAT_SSL_SERVER)
  73. ASSERT(event == BSSLCONNECTION_EVENT_ERROR)
  74. PeerLog(o, BLOG_ERROR, "SSL error");
  75. report_error(o);
  76. return;
  77. }
  78. static SECStatus client_auth_data_callback (PeerChat *o, PRFileDesc *fd, CERTDistNames *caNames, CERTCertificate **pRetCert, SECKEYPrivateKey **pRetKey)
  79. {
  80. DebugObject_Access(&o->d_obj);
  81. ASSERT(o->ssl_mode == PEERCHAT_SSL_CLIENT)
  82. CERTCertificate *cert = CERT_DupCertificate(o->ssl_cert);
  83. if (!cert) {
  84. PeerLog(o, BLOG_ERROR, "CERT_DupCertificate failed");
  85. goto fail0;
  86. }
  87. SECKEYPrivateKey *key = SECKEY_CopyPrivateKey(o->ssl_key);
  88. if (!key) {
  89. PeerLog(o, BLOG_ERROR, "SECKEY_CopyPrivateKey failed");
  90. goto fail1;
  91. }
  92. *pRetCert = cert;
  93. *pRetKey = key;
  94. return SECSuccess;
  95. fail1:
  96. CERT_DestroyCertificate(cert);
  97. fail0:
  98. return SECFailure;
  99. }
  100. static SECStatus auth_certificate_callback (PeerChat *o, PRFileDesc *fd, PRBool checkSig, PRBool isServer)
  101. {
  102. DebugObject_Access(&o->d_obj);
  103. ASSERT(o->ssl_mode == PEERCHAT_SSL_CLIENT || o->ssl_mode == PEERCHAT_SSL_SERVER)
  104. // This callback is used to bypass checking the server's domain name, as peers
  105. // don't have domain names. We byte-compare the certificate to the one reported
  106. // by the server anyway.
  107. SECStatus ret = SECFailure;
  108. CERTCertificate *cert = SSL_PeerCertificate(o->ssl_prfd);
  109. if (!cert) {
  110. PeerLog(o, BLOG_ERROR, "SSL_PeerCertificate failed");
  111. PORT_SetError(SSL_ERROR_BAD_CERTIFICATE);
  112. goto fail1;
  113. }
  114. SECCertUsage cert_usage = (o->ssl_mode == PEERCHAT_SSL_CLIENT ? certUsageSSLServer : certUsageSSLClient);
  115. if (CERT_VerifyCertNow(CERT_GetDefaultCertDB(), cert, PR_TRUE, cert_usage, SSL_RevealPinArg(o->ssl_prfd)) != SECSuccess) {
  116. goto fail2;
  117. }
  118. // compare to certificate provided by the server
  119. SECItem der = cert->derCert;
  120. if (der.len != o->ssl_peer_cert_len || memcmp(der.data, o->ssl_peer_cert, der.len)) {
  121. PeerLog(o, BLOG_ERROR, "peer certificate doesn't match");
  122. PORT_SetError(SSL_ERROR_BAD_CERTIFICATE);
  123. goto fail2;
  124. }
  125. ret = SECSuccess;
  126. fail2:
  127. CERT_DestroyCertificate(cert);
  128. fail1:
  129. return ret;
  130. }
  131. static void ssl_recv_if_handler_send (PeerChat *o, uint8_t *data, int data_len)
  132. {
  133. DebugObject_Access(&o->d_obj);
  134. DebugError_AssertNoError(&o->d_err);
  135. ASSERT(o->ssl_mode == PEERCHAT_SSL_CLIENT || o->ssl_mode == PEERCHAT_SSL_SERVER)
  136. ASSERT(data_len >= 0)
  137. ASSERT(data_len <= SC_MAX_MSGLEN)
  138. // accept packet
  139. PacketPassInterface_Done(&o->ssl_recv_if);
  140. // call message handler
  141. o->handler_message(o->user, data, data_len);
  142. return;
  143. }
  144. static void ssl_recv_decoder_handler_error (PeerChat *o)
  145. {
  146. DebugObject_Access(&o->d_obj);
  147. DebugError_AssertNoError(&o->d_err);
  148. ASSERT(o->ssl_mode == PEERCHAT_SSL_CLIENT || o->ssl_mode == PEERCHAT_SSL_SERVER)
  149. PeerLog(o, BLOG_ERROR, "decoder error");
  150. report_error(o);
  151. return;
  152. }
  153. int PeerChat_Init (PeerChat *o, peerid_t peer_id, int ssl_mode, CERTCertificate *ssl_cert, SECKEYPrivateKey *ssl_key,
  154. uint8_t *ssl_peer_cert, int ssl_peer_cert_len, BPendingGroup *pg, void *user,
  155. PeerChat_logfunc logfunc,
  156. PeerChat_handler_error handler_error,
  157. PeerChat_handler_message handler_message)
  158. {
  159. ASSERT(ssl_mode == PEERCHAT_SSL_NONE || ssl_mode == PEERCHAT_SSL_CLIENT || ssl_mode == PEERCHAT_SSL_SERVER)
  160. ASSERT(ssl_mode == PEERCHAT_SSL_NONE || ssl_peer_cert_len >= 0)
  161. ASSERT(handler_error)
  162. ASSERT(handler_message)
  163. // init arguments
  164. o->ssl_mode = ssl_mode;
  165. o->ssl_cert = ssl_cert;
  166. o->ssl_key = ssl_key;
  167. o->ssl_peer_cert = ssl_peer_cert;
  168. o->ssl_peer_cert_len = ssl_peer_cert_len;
  169. o->user = user;
  170. o->logfunc = logfunc;
  171. o->handler_error = handler_error;
  172. o->handler_message = handler_message;
  173. // init copier
  174. PacketCopier_Init(&o->copier, SC_MAX_MSGLEN, pg);
  175. // init SC encoder
  176. SCOutmsgEncoder_Init(&o->sc_encoder, peer_id, PacketCopier_GetOutput(&o->copier), pg);
  177. // init PacketProto encoder
  178. PacketProtoEncoder_Init(&o->pp_encoder, SCOutmsgEncoder_GetOutput(&o->sc_encoder), pg);
  179. // init recv job
  180. BPending_Init(&o->recv_job, pg, (BPending_handler)recv_job_handler, o);
  181. // set no received data
  182. o->recv_data_len = -1;
  183. if (o->ssl_mode != PEERCHAT_SSL_NONE) {
  184. // init receive buffer
  185. if (!SimpleStreamBuffer_Init(&o->ssl_recv_buf, PEERCHAT_SSL_RECV_BUF_SIZE, pg)) {
  186. PeerLog(o, BLOG_ERROR, "SimpleStreamBuffer_Init failed");
  187. goto fail1;
  188. }
  189. // init SSL StreamPacketSender
  190. StreamPacketSender_Init(&o->ssl_sp_sender, PacketCopier_GetInput(&o->copier), pg);
  191. // init SSL bottom prfd
  192. if (!BSSLConnection_MakeBackend(&o->ssl_bottom_prfd, StreamPacketSender_GetInput(&o->ssl_sp_sender), SimpleStreamBuffer_GetOutput(&o->ssl_recv_buf))) {
  193. PeerLog(o, BLOG_ERROR, "BSSLConnection_MakeBackend failed");
  194. goto fail2;
  195. }
  196. // init SSL prfd
  197. if (!(o->ssl_prfd = SSL_ImportFD(NULL, &o->ssl_bottom_prfd))) {
  198. ASSERT_FORCE(PR_Close(&o->ssl_bottom_prfd) == PR_SUCCESS)
  199. PeerLog(o, BLOG_ERROR, "SSL_ImportFD failed");
  200. goto fail2;
  201. }
  202. // set client or server mode
  203. if (SSL_ResetHandshake(o->ssl_prfd, (o->ssl_mode == PEERCHAT_SSL_SERVER ? PR_TRUE : PR_FALSE)) != SECSuccess) {
  204. PeerLog(o, BLOG_ERROR, "SSL_ResetHandshake failed");
  205. goto fail3;
  206. }
  207. if (o->ssl_mode == PEERCHAT_SSL_SERVER) {
  208. // set server certificate
  209. if (SSL_ConfigSecureServer(o->ssl_prfd, o->ssl_cert, o->ssl_key, NSS_FindCertKEAType(o->ssl_cert)) != SECSuccess) {
  210. PeerLog(o, BLOG_ERROR, "SSL_ConfigSecureServer failed");
  211. goto fail3;
  212. }
  213. // set require client certificate
  214. if (SSL_OptionSet(o->ssl_prfd, SSL_REQUEST_CERTIFICATE, PR_TRUE) != SECSuccess) {
  215. PeerLog(o, BLOG_ERROR, "SSL_OptionSet(SSL_REQUEST_CERTIFICATE) failed");
  216. goto fail3;
  217. }
  218. if (SSL_OptionSet(o->ssl_prfd, SSL_REQUIRE_CERTIFICATE, PR_TRUE) != SECSuccess) {
  219. PeerLog(o, BLOG_ERROR, "SSL_OptionSet(SSL_REQUIRE_CERTIFICATE) failed");
  220. goto fail3;
  221. }
  222. } else {
  223. // set client certificate callback
  224. if (SSL_GetClientAuthDataHook(o->ssl_prfd, (SSLGetClientAuthData)client_auth_data_callback, o) != SECSuccess) {
  225. PeerLog(o, BLOG_ERROR, "SSL_GetClientAuthDataHook failed");
  226. goto fail3;
  227. }
  228. }
  229. // set verify peer certificate hook
  230. if (SSL_AuthCertificateHook(o->ssl_prfd, (SSLAuthCertificate)auth_certificate_callback, o) != SECSuccess) {
  231. PeerLog(o, BLOG_ERROR, "SSL_AuthCertificateHook failed");
  232. goto fail3;
  233. }
  234. // init SSL connection
  235. BSSLConnection_Init(&o->ssl_con, o->ssl_prfd, 0, pg, o, (BSSLConnection_handler)ssl_con_handler);
  236. // init SSL PacketStreamSender
  237. PacketStreamSender_Init(&o->ssl_ps_sender, BSSLConnection_GetSendIf(&o->ssl_con), sizeof(struct packetproto_header) + SC_MAX_MSGLEN, pg);
  238. // init SSL copier
  239. PacketCopier_Init(&o->ssl_copier, SC_MAX_MSGLEN, pg);
  240. // init SSL encoder
  241. PacketProtoEncoder_Init(&o->ssl_encoder, PacketCopier_GetOutput(&o->ssl_copier), pg);
  242. // init SSL buffer
  243. if (!SinglePacketBuffer_Init(&o->ssl_buffer, PacketProtoEncoder_GetOutput(&o->ssl_encoder), PacketStreamSender_GetInput(&o->ssl_ps_sender), pg)) {
  244. PeerLog(o, BLOG_ERROR, "SinglePacketBuffer_Init failed");
  245. goto fail4;
  246. }
  247. // init receive interface
  248. PacketPassInterface_Init(&o->ssl_recv_if, SC_MAX_MSGLEN, (PacketPassInterface_handler_send)ssl_recv_if_handler_send, o, pg);
  249. // init receive decoder
  250. if (!PacketProtoDecoder_Init(&o->ssl_recv_decoder, BSSLConnection_GetRecvIf(&o->ssl_con), &o->ssl_recv_if, pg, o, (PacketProtoDecoder_handler_error)ssl_recv_decoder_handler_error)) {
  251. PeerLog(o, BLOG_ERROR, "PacketProtoDecoder_Init failed");
  252. goto fail5;
  253. }
  254. }
  255. DebugError_Init(&o->d_err, pg);
  256. DebugObject_Init(&o->d_obj);
  257. return 1;
  258. if (o->ssl_mode != PEERCHAT_SSL_NONE) {
  259. fail5:
  260. PacketPassInterface_Free(&o->ssl_recv_if);
  261. SinglePacketBuffer_Free(&o->ssl_buffer);
  262. fail4:
  263. PacketProtoEncoder_Free(&o->ssl_encoder);
  264. PacketCopier_Free(&o->ssl_copier);
  265. PacketStreamSender_Free(&o->ssl_ps_sender);
  266. BSSLConnection_Free(&o->ssl_con);
  267. fail3:
  268. ASSERT_FORCE(PR_Close(o->ssl_prfd) == PR_SUCCESS)
  269. fail2:
  270. StreamPacketSender_Free(&o->ssl_sp_sender);
  271. SimpleStreamBuffer_Free(&o->ssl_recv_buf);
  272. }
  273. fail1:
  274. BPending_Free(&o->recv_job);
  275. PacketProtoEncoder_Free(&o->pp_encoder);
  276. SCOutmsgEncoder_Free(&o->sc_encoder);
  277. PacketCopier_Free(&o->copier);
  278. return 0;
  279. }
  280. void PeerChat_Free (PeerChat *o)
  281. {
  282. DebugObject_Free(&o->d_obj);
  283. DebugError_Free(&o->d_err);
  284. if (o->ssl_mode != PEERCHAT_SSL_NONE) {
  285. PacketProtoDecoder_Free(&o->ssl_recv_decoder);
  286. PacketPassInterface_Free(&o->ssl_recv_if);
  287. SinglePacketBuffer_Free(&o->ssl_buffer);
  288. PacketProtoEncoder_Free(&o->ssl_encoder);
  289. PacketCopier_Free(&o->ssl_copier);
  290. PacketStreamSender_Free(&o->ssl_ps_sender);
  291. BSSLConnection_Free(&o->ssl_con);
  292. ASSERT_FORCE(PR_Close(o->ssl_prfd) == PR_SUCCESS)
  293. StreamPacketSender_Free(&o->ssl_sp_sender);
  294. SimpleStreamBuffer_Free(&o->ssl_recv_buf);
  295. }
  296. BPending_Free(&o->recv_job);
  297. PacketProtoEncoder_Free(&o->pp_encoder);
  298. SCOutmsgEncoder_Free(&o->sc_encoder);
  299. PacketCopier_Free(&o->copier);
  300. }
  301. PacketPassInterface * PeerChat_GetSendInput (PeerChat *o)
  302. {
  303. DebugObject_Access(&o->d_obj);
  304. if (o->ssl_mode != PEERCHAT_SSL_NONE) {
  305. return PacketCopier_GetInput(&o->ssl_copier);
  306. } else {
  307. return PacketCopier_GetInput(&o->copier);
  308. }
  309. }
  310. PacketRecvInterface * PeerChat_GetSendOutput (PeerChat *o)
  311. {
  312. DebugObject_Access(&o->d_obj);
  313. return PacketProtoEncoder_GetOutput(&o->pp_encoder);
  314. }
  315. void PeerChat_InputReceived (PeerChat *o, uint8_t *data, int data_len)
  316. {
  317. DebugObject_Access(&o->d_obj);
  318. DebugError_AssertNoError(&o->d_err);
  319. ASSERT(o->recv_data_len == -1)
  320. ASSERT(data_len >= 0)
  321. ASSERT(data_len <= SC_MAX_MSGLEN)
  322. // remember data
  323. o->recv_data = data;
  324. o->recv_data_len = data_len;
  325. // set received job
  326. BPending_Set(&o->recv_job);
  327. }