sasl_io.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. /** BEGIN COPYRIGHT BLOCK
  2. * Copyright 2004 Netscape Communications Corporation.
  3. * All rights reserved.
  4. * END COPYRIGHT BLOCK **/
  5. #undef CYRUS_SASL
  6. #include "slap.h"
  7. #include "slapi-plugin.h"
  8. #include "fe.h"
  9. #include <sasl.h>
  10. /*
  11. * I/O Shim Layer for SASL Encryption
  12. * The 'handle' is a pointer to a sasl_connection structure.
  13. */
  14. #define SASL_IO_BUFFER_SIZE 1024
  15. /*
  16. * SASL sends its encrypted PDU's with an embedded 4-byte length
  17. * at the beginning (in network byte order). We peek inside the
  18. * received data off the wire to find this length, and use it
  19. * to determine when we have read an entire SASL PDU.
  20. * So when we have that there is no need for the SASL layer
  21. * to do any fancy buffering with it, we always hand it
  22. * a full packet.
  23. */
  24. struct _sasl_io_private {
  25. struct lextiof_socket_private *real_handle;
  26. struct lber_x_ext_io_fns *real_iofns;
  27. char *decrypted_buffer;
  28. size_t decrypted_buffer_size;
  29. size_t decrypted_buffer_count;
  30. size_t decrypted_buffer_offset;
  31. char *encrypted_buffer;
  32. size_t encrypted_buffer_size;
  33. size_t encrypted_buffer_count;
  34. size_t encrypted_buffer_offset;
  35. Connection *conn;
  36. };
  37. int
  38. sasl_io_enable(Connection *c)
  39. {
  40. int ret = 0;
  41. LDAPDebug( LDAP_DEBUG_CONNS,
  42. "sasl_io_enable for connection %d\n", c->c_connid, 0, 0 );
  43. /* Flag that we should enable SASL I/O for the next read operation on this connection */
  44. c->c_enable_sasl_io = 1;
  45. return ret;
  46. }
  47. static void
  48. sasl_io_init_buffers(sasl_io_private *sp)
  49. {
  50. sp->decrypted_buffer = slapi_ch_malloc(SASL_IO_BUFFER_SIZE);
  51. sp->decrypted_buffer_size = SASL_IO_BUFFER_SIZE;
  52. sp->encrypted_buffer = slapi_ch_malloc(SASL_IO_BUFFER_SIZE);
  53. sp->encrypted_buffer_size = SASL_IO_BUFFER_SIZE;
  54. }
  55. /* This function should be called under the connection mutex */
  56. int
  57. sasl_io_setup(Connection *c)
  58. {
  59. int ret = 0;
  60. struct lber_x_ext_io_fns *func_pointers = NULL;
  61. sasl_io_private *sp = (sasl_io_private*) slapi_ch_calloc(1, sizeof(sasl_io_private));
  62. LDAPDebug( LDAP_DEBUG_CONNS,
  63. "sasl_io_setup for connection %d\n", c->c_connid, 0, 0 );
  64. /* Get the current functions and store them for later */
  65. ber_sockbuf_get_option( c->c_sb, LBER_SOCKBUF_OPT_EXT_IO_FNS, &func_pointers);
  66. sp->real_iofns = func_pointers;
  67. func_pointers = NULL;
  68. /* Set up the private structure */
  69. sp->real_handle = (struct lextiof_socket_private*) c->c_prfd;
  70. sp->conn = c;
  71. /* Store the private structure in the connection */
  72. c->c_sasl_io_private = sp;
  73. /* Insert the sasl i/o functions into the ber layer */
  74. func_pointers = (struct lber_x_ext_io_fns *) slapi_ch_malloc(LBER_X_EXTIO_FNS_SIZE);
  75. func_pointers->lbextiofn_size = LBER_X_EXTIO_FNS_SIZE;
  76. func_pointers->lbextiofn_read = sasl_read_function;
  77. func_pointers->lbextiofn_write = sasl_write_function;
  78. func_pointers->lbextiofn_writev = NULL;
  79. func_pointers->lbextiofn_socket_arg = (struct lextiof_socket_private *) sp;
  80. ber_sockbuf_set_option( c->c_sb, LBER_SOCKBUF_OPT_EXT_IO_FNS, func_pointers);
  81. /* Setup the data buffers for the fast read path */
  82. sasl_io_init_buffers(sp);
  83. /* Reset the enable flag, so we don't process it again */
  84. c->c_enable_sasl_io = 0;
  85. /* Mark the connection as having SASL I/O */
  86. c->c_sasl_io = 1;
  87. return ret;
  88. }
  89. int
  90. sasl_io_cleanup(Connection *c)
  91. {
  92. int ret = 0;
  93. sasl_io_private *sp = c->c_sasl_io_private;
  94. if (sp) {
  95. LDAPDebug( LDAP_DEBUG_CONNS,
  96. "sasl_io_cleanup for connection %d\n", c->c_connid, 0, 0 );
  97. /* Free the buffers */
  98. slapi_ch_free((void**)&(sp->encrypted_buffer));
  99. slapi_ch_free((void**)&(sp->decrypted_buffer));
  100. /* Put the I/O functions back how they were */
  101. ber_sockbuf_set_option( c->c_sb, LBER_SOCKBUF_OPT_EXT_IO_FNS, sp->real_iofns);
  102. slapi_ch_free((void**)&sp);
  103. c->c_sasl_io_private = NULL;
  104. c->c_enable_sasl_io = 0;
  105. c->c_sasl_io = 0;
  106. }
  107. return ret;
  108. }
  109. static void sasl_io_resize_encrypted_buffer(sasl_io_private *sp, size_t requested_size)
  110. {
  111. if (requested_size > sp->encrypted_buffer_size) {
  112. sp->encrypted_buffer = slapi_ch_realloc(sp->encrypted_buffer, requested_size);
  113. sp->encrypted_buffer_size = requested_size;
  114. }
  115. }
  116. static void sasl_io_resize_decrypted_buffer(sasl_io_private *sp, size_t requested_size)
  117. {
  118. if (requested_size > sp->decrypted_buffer_size) {
  119. sp->decrypted_buffer = slapi_ch_realloc(sp->decrypted_buffer, requested_size);
  120. sp->decrypted_buffer_size = requested_size;
  121. }
  122. }
  123. static int
  124. sasl_io_reading_packet(sasl_io_private *sp)
  125. {
  126. return (sp->encrypted_buffer_count > 0);
  127. }
  128. static int
  129. sasl_io_finished_packet(sasl_io_private *sp)
  130. {
  131. return (sp->encrypted_buffer_count && (sp->encrypted_buffer_offset == sp->encrypted_buffer_count) );
  132. }
  133. static int
  134. sasl_io_start_packet(Connection *c, PRInt32 *err)
  135. {
  136. int ret = 0;
  137. unsigned char buffer[4];
  138. size_t packet_length = 0;
  139. ret = PR_Recv(c->c_prfd,buffer,sizeof(buffer),0,PR_INTERVAL_NO_WAIT);
  140. if (ret < 0) {
  141. *err = PR_GetError();
  142. return -1;
  143. }
  144. if (ret != 0 && ret < sizeof(buffer)) {
  145. LDAPDebug( LDAP_DEBUG_ANY,
  146. "failed to read sasl packet length on connection %d\n", c->c_connid, 0, 0 );
  147. return -1;
  148. }
  149. #ifdef CYRUS_SASL
  150. if (ret == sizeof(buffer)) {
  151. /* Decode the length (could use ntohl here ??) */
  152. packet_length = buffer[0] << 24 | buffer[1] << 16 | buffer[2] << 8 | buffer[3];
  153. /* add length itself (for Cyrus SASL library) */
  154. packet_length += 4;
  155. LDAPDebug( LDAP_DEBUG_CONNS,
  156. "read sasl packet length %ld on connection %d\n", packet_length, c->c_connid, 0 );
  157. sasl_io_resize_encrypted_buffer(c->c_sasl_io_private, packet_length);
  158. /* Cyrus SASL implementation expects to have the length at the first
  159. 4 bytes */
  160. memcpy(c->c_sasl_io_private->encrypted_buffer, buffer, 4);
  161. c->c_sasl_io_private->encrypted_buffer_count = packet_length;
  162. c->c_sasl_io_private->encrypted_buffer_offset = 4;
  163. }
  164. #else
  165. if (ret == sizeof(buffer)) {
  166. /* Decode the length (could use ntohl here ??) */
  167. packet_length = buffer[0] << 24 | buffer[1] << 16 | buffer[2] << 8 | buffer[3];
  168. LDAPDebug( LDAP_DEBUG_CONNS,
  169. "read sasl packet length %ld on connection %d\n", packet_length, c->c_connid, 0 );
  170. sasl_io_resize_encrypted_buffer(c->c_sasl_io_private, packet_length);
  171. c->c_sasl_io_private->encrypted_buffer_count = packet_length;
  172. c->c_sasl_io_private->encrypted_buffer_offset = 0;
  173. }
  174. #endif
  175. return 0;
  176. }
  177. static int
  178. sasl_io_read_packet(Connection *c, PRInt32 *err)
  179. {
  180. size_t ret = 0;
  181. sasl_io_private *sp = c->c_sasl_io_private;
  182. size_t bytes_remaining_to_read = sp->encrypted_buffer_count - sp->encrypted_buffer_offset;
  183. ret = PR_Recv(c->c_prfd,sp->encrypted_buffer + sp->encrypted_buffer_offset,bytes_remaining_to_read,0,PR_INTERVAL_NO_WAIT);
  184. if (ret < 0) {
  185. *err = PR_GetError();
  186. return -1;
  187. }
  188. if (ret > 0) {
  189. sp->encrypted_buffer_offset += ret;
  190. }
  191. return ret;
  192. }
  193. /* Special recv function for the server connection code */
  194. /* Here, we return bytes to the caller, either the bytes
  195. remaining in the decrypted data buffer, from 'before',
  196. or the number of bytes we get decrypted from sasl,
  197. or the requested number of bytes whichever is lower.
  198. */
  199. int
  200. sasl_recv_connection(Connection *c, char *buffer, size_t count,PRInt32 *err)
  201. {
  202. int ret = 0;
  203. size_t bytes_in_buffer = 0;
  204. sasl_io_private *sp = c->c_sasl_io_private;
  205. *err = 0;
  206. LDAPDebug( LDAP_DEBUG_CONNS,
  207. "sasl_recv_connection for connection %d\n", c->c_connid, 0, 0 );
  208. /* Do we have decrypted data buffered from 'before' ? */
  209. bytes_in_buffer = sp->decrypted_buffer_count - sp->decrypted_buffer_offset;
  210. if (0 == bytes_in_buffer) {
  211. /* If there wasn't buffered decrypted data, we need to get some... */
  212. if (!sasl_io_reading_packet(sp)) {
  213. /* First read the packet length and so on */
  214. ret = sasl_io_start_packet(c, err);
  215. if (0 != ret) {
  216. /* Most likely the i/o timed out */
  217. return ret;
  218. }
  219. }
  220. /* We now have the packet length
  221. * we now must read more data off the wire until we have the complete packet
  222. */
  223. ret = sasl_io_read_packet(c,err);
  224. if (0 == ret || -1 == ret) {
  225. return ret;
  226. }
  227. /* Are we there yet ? */
  228. if (sasl_io_finished_packet(sp)) {
  229. const char *output_buffer = NULL;
  230. unsigned int output_length = 0;
  231. LDAPDebug( LDAP_DEBUG_CONNS,
  232. "sasl_recv_connection finished reading packet for connection %d\n", c->c_connid, 0, 0 );
  233. /* Now decode it */
  234. ret = sasl_decode(c->c_sasl_conn,sp->encrypted_buffer,sp->encrypted_buffer_count,&output_buffer,&output_length);
  235. if (SASL_OK == ret) {
  236. LDAPDebug( LDAP_DEBUG_CONNS,
  237. "sasl_recv_connection decoded packet length %d for connection %d\n", output_length, c->c_connid, 0 );
  238. if (output_length) {
  239. sasl_io_resize_decrypted_buffer(sp,output_length);
  240. memcpy(sp->decrypted_buffer,output_buffer,output_length);
  241. sp->decrypted_buffer_count = output_length;
  242. sp->decrypted_buffer_offset = 0;
  243. sp->encrypted_buffer_offset = 0;
  244. sp->encrypted_buffer_count = 0;
  245. }
  246. } else {
  247. LDAPDebug( LDAP_DEBUG_ANY,
  248. "sasl_recv_connection failed to decode packet for connection %d\n", c->c_connid, 0, 0 );
  249. }
  250. }
  251. }
  252. /* Finally, return data from the buffer to the caller */
  253. {
  254. size_t bytes_to_return = sp->decrypted_buffer_count - sp->decrypted_buffer_offset;
  255. if (bytes_to_return > count) {
  256. bytes_to_return = count;
  257. }
  258. memcpy(buffer, sp->decrypted_buffer, bytes_to_return);
  259. if (bytes_in_buffer == bytes_to_return) {
  260. sp->decrypted_buffer_offset = 0;
  261. sp->decrypted_buffer_count = 0;
  262. } else {
  263. sp->decrypted_buffer_offset += bytes_to_return;
  264. }
  265. ret = bytes_to_return;
  266. }
  267. return ret;
  268. }
  269. int
  270. sasl_read_function(int ignore, void *buffer, int count, struct lextiof_socket_private *handle )
  271. {
  272. int ret = 0;
  273. sasl_io_private *sp = (sasl_io_private*) handle;
  274. /* First we look to see if we have buffered data that we can return to the caller */
  275. if ( (NULL == sp->decrypted_buffer) || ((sp->decrypted_buffer_count - sp->decrypted_buffer_offset) <= 0) ) {
  276. /* If we didn't have buffered data, we need to perform I/O and decrypt */
  277. PRUint32 buffer_length = 0;
  278. /* Read the packet length */
  279. ret = read_function(0, &buffer_length, sizeof(buffer_length), sp->real_handle);
  280. if (ret) {
  281. }
  282. /* Read the payload */
  283. ret = read_function(0, sp->encrypted_buffer, buffer_length, sp->real_handle);
  284. if (ret) {
  285. }
  286. /* Now we can call sasl to decrypt */
  287. /* ret = sasl_decode(sp->conn->c_sasl_conn,sp->encrypted_buffer, buffer_length, sp->decrypted_buffer, &sp->decrypted_buffer_count ); */
  288. }
  289. /* If things went well, copy the payload for the caller */
  290. if ( 0 == ret ) {
  291. /* size_t real_count = 0;
  292. if (count >= (sp->buffer_count - sp->buffer_offset) ) {
  293. real_count = count;
  294. } else {
  295. real_count = (sp->buffer_count - sp->buffer_offset);
  296. }
  297. memcpy(buffer, sp->buffer, real_count);
  298. sp->buffer_offset += real_count; */
  299. }
  300. return ret;
  301. }
  302. int
  303. sasl_write_function(int ignore, const void *buffer, int count, struct lextiof_socket_private *handle)
  304. {
  305. int ret = 0;
  306. sasl_io_private *sp = (sasl_io_private*) handle;
  307. const char *crypt_buffer = NULL;
  308. unsigned crypt_buffer_size = 0;
  309. LDAPDebug( LDAP_DEBUG_CONNS,
  310. "sasl_write_function writing %d bytes\n", count, 0, 0 );
  311. /* Get SASL to encrypt the buffer */
  312. ret = sasl_encode(sp->conn->c_sasl_conn, buffer, count, &crypt_buffer, &crypt_buffer_size);
  313. LDAPDebug( LDAP_DEBUG_CONNS,
  314. "sasl_write_function encoded as %d bytes\n", crypt_buffer_size, 0, 0 );
  315. ret = write_function(0, crypt_buffer, crypt_buffer_size, sp->real_handle);
  316. if (ret) {
  317. }
  318. return ret;
  319. }