Changeset 5308
- Timestamp:
- 02/24/08 10:29:18 (8 months ago)
- Files:
-
- libwired/trunk/libwired/misc/wi-crypto.c (modified) (9 diffs)
- libwired/trunk/libwired/misc/wi-crypto.h (modified) (1 diff)
- libwired/trunk/libwired/p7/wi-p7-socket.c (modified) (20 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
libwired/trunk/libwired/misc/wi-crypto.c
r5284 r5308 608 608 609 609 610 wi_uinteger_t wi_cipher_block_size(wi_cipher_t *cipher) { 611 return EVP_CIPHER_block_size(cipher->cipher); 612 } 613 614 615 610 616 #pragma mark - 611 617 … … 613 619 const void *decrypted_buffer; 614 620 void *encrypted_buffer; 615 wi_uinteger_t decrypted_length, encrypted_length; 621 wi_uinteger_t decrypted_length; 622 wi_integer_t encrypted_length; 616 623 617 624 decrypted_buffer = wi_data_bytes(decrypted_data); 618 625 decrypted_length = wi_data_length(decrypted_data); 619 620 if(!wi_cipher_encrypt_bytes(cipher, decrypted_buffer, decrypted_length, &encrypted_buffer, &encrypted_length)) 626 encrypted_buffer = wi_malloc(wi_cipher_block_size(cipher) + decrypted_length); 627 encrypted_length = wi_cipher_encrypt_bytes(cipher, decrypted_buffer, decrypted_length, encrypted_buffer); 628 629 if(encrypted_length < 0) 621 630 return NULL; 622 631 … … 626 635 627 636 628 wi_boolean_t wi_cipher_encrypt_bytes(wi_cipher_t *cipher, const void *decrypted_buffer, wi_uinteger_t decrypted_length, void **out_buffer, wi_uinteger_t *out_length) { 629 void *encrypted_buffer; 637 wi_integer_t wi_cipher_encrypt_bytes(wi_cipher_t *cipher, const void *decrypted_buffer, wi_uinteger_t decrypted_length, void *encrypted_buffer) { 630 638 int encrypted_length, padded_length; 631 639 632 encrypted_buffer = wi_malloc(decrypted_length + EVP_CIPHER_block_size(cipher->cipher));633 634 640 if(EVP_EncryptUpdate(&cipher->encrypt_ctx, encrypted_buffer, &encrypted_length, decrypted_buffer, decrypted_length) != 1) { 635 641 wi_error_set_openssl_error(); 636 642 637 wi_free(encrypted_buffer); 638 639 return false; 643 return -1; 640 644 } 641 645 … … 643 647 wi_error_set_openssl_error(); 644 648 645 wi_free(encrypted_buffer); 646 647 return false; 649 return -1; 648 650 } 649 651 … … 651 653 wi_error_set_openssl_error(); 652 654 653 wi_free(encrypted_buffer); 654 655 return false; 656 } 657 658 *out_buffer = encrypted_buffer; 659 *out_length = encrypted_length + padded_length; 660 661 return true; 655 return -1; 656 } 657 658 return encrypted_length + padded_length; 662 659 } 663 660 … … 667 664 const void *encrypted_buffer; 668 665 void *decrypted_buffer; 669 wi_uinteger_t encrypted_length, decrypted_length; 666 wi_uinteger_t encrypted_length; 667 wi_integer_t decrypted_length; 670 668 671 669 encrypted_buffer = wi_data_bytes(encrypted_data); 672 670 encrypted_length = wi_data_length(encrypted_data); 673 674 if(!wi_cipher_decrypt_bytes(cipher, encrypted_buffer, encrypted_length, &decrypted_buffer, &decrypted_length)) 671 decrypted_buffer = wi_malloc(wi_cipher_block_size(cipher) + encrypted_length); 672 decrypted_length = wi_cipher_decrypt_bytes(cipher, encrypted_buffer, encrypted_length, decrypted_buffer); 673 674 if(decrypted_length < 0) 675 675 return NULL; 676 676 … … 680 680 681 681 682 wi_boolean_t wi_cipher_decrypt_bytes(wi_cipher_t *cipher, const void *encrypted_buffer, wi_uinteger_t encrypted_length, void **out_buffer, wi_uinteger_t *out_length) { 683 void *decrypted_buffer; 682 wi_integer_t wi_cipher_decrypt_bytes(wi_cipher_t *cipher, const void *encrypted_buffer, wi_uinteger_t encrypted_length, void *decrypted_buffer) { 684 683 int decrypted_length, padded_length; 685 684 686 decrypted_buffer = wi_malloc(encrypted_length + EVP_CIPHER_block_size(cipher->cipher));687 688 685 if(EVP_DecryptUpdate(&cipher->decrypt_ctx, decrypted_buffer, &decrypted_length, encrypted_buffer, encrypted_length) != 1) { 689 686 wi_error_set_openssl_error(); 690 687 691 wi_free(decrypted_buffer); 692 693 return false; 688 return -1; 694 689 } 695 690 … … 697 692 wi_error_set_openssl_error(); 698 693 699 wi_free(decrypted_buffer); 700 701 return false; 694 return -1; 702 695 } 703 696 … … 705 698 wi_error_set_openssl_error(); 706 699 707 wi_free(decrypted_buffer); 708 709 return false; 710 } 711 712 *out_buffer = decrypted_buffer; 713 *out_length = decrypted_length + padded_length; 714 715 return true; 700 return -1; 701 } 702 703 return decrypted_length + padded_length; 716 704 } 717 705 libwired/trunk/libwired/misc/wi-crypto.h
r4697 r5308 77 77 WI_EXPORT wi_string_t * wi_cipher_name(wi_cipher_t *); 78 78 WI_EXPORT wi_uinteger_t wi_cipher_bits(wi_cipher_t *); 79 WI_EXPORT wi_uinteger_t wi_cipher_block_size(wi_cipher_t *); 79 80 80 81 WI_EXPORT wi_data_t * wi_cipher_encrypt(wi_cipher_t *, wi_data_t *); 81 WI_EXPORT wi_ boolean_t wi_cipher_encrypt_bytes(wi_cipher_t *, const void *, wi_uinteger_t, void **, wi_uinteger_t*);82 WI_EXPORT wi_integer_t wi_cipher_encrypt_bytes(wi_cipher_t *, const void *, wi_uinteger_t, void *); 82 83 WI_EXPORT wi_data_t * wi_cipher_decrypt(wi_cipher_t *, wi_data_t *); 83 WI_EXPORT wi_ boolean_t wi_cipher_decrypt_bytes(wi_cipher_t *, const void *, wi_uinteger_t, void **, wi_uinteger_t*);84 WI_EXPORT wi_integer_t wi_cipher_decrypt_bytes(wi_cipher_t *, const void *, wi_uinteger_t, void *); 84 85 85 86 #endif /* WI_CRYPTO_H */ libwired/trunk/libwired/p7/wi-p7-socket.c
r5296 r5308 153 153 wi_p7_boolean_t remote_compatibility_check; 154 154 155 void *compression_buffer; 156 wi_uinteger_t compression_buffer_length; 157 void *encryption_buffer; 158 wi_uinteger_t encryption_buffer_length; 159 void *decryption_buffer; 160 wi_uinteger_t decryption_buffer_length; 161 void *oobdata_read_buffer; 162 wi_uinteger_t oobdata_read_buffer_length; 163 155 164 uint64_t read_raw_bytes, read_processed_bytes; 156 165 uint64_t sent_raw_bytes, sent_processed_bytes; … … 181 190 182 191 static wi_boolean_t _wi_p7_socket_configure_compression_streams(wi_p7_socket_t *); 183 static wi_ boolean_t _wi_p7_socket_xcompress_buffer(wi_p7_socket_t *, _wi_p7_socket_compression_t, const void *, uint32_t, void **, uint32_t *);192 static wi_integer_t _wi_p7_socket_xcompress_buffer(wi_p7_socket_t *, _wi_p7_socket_compression_t, const void *, uint32_t); 184 193 static int _wi_p7_socket_xflate_buffer(z_stream *, _wi_p7_socket_compression_t, const void *, uint32_t, uint32_t *, void *, uint32_t *); 185 194 … … 261 270 wi_p7_socket_t *p7_socket = instance; 262 271 272 wi_free(p7_socket->compression_buffer); 273 wi_free(p7_socket->encryption_buffer); 274 wi_free(p7_socket->decryption_buffer); 275 wi_free(p7_socket->oobdata_read_buffer); 276 263 277 wi_release(p7_socket->socket); 264 278 wi_release(p7_socket->spec); … … 998 1012 static wi_boolean_t _wi_p7_socket_write_binary_message(wi_p7_socket_t *p7_socket, wi_time_interval_t timeout, wi_p7_message_t *p7_message) { 999 1013 const void *send_buffer; 1000 void *compressed_buffer = NULL, *encrypted_buffer = NULL;1001 1014 char length_buffer[_WI_P7_SOCKET_LENGTH_SIZE]; 1002 1015 unsigned char checksum_buffer[_WI_P7_SOCKET_CHECKSUM_LENGTH]; 1003 uint32_t send_size,compressed_size, encrypted_size;1004 wi_boolean_t result = false;1016 wi_integer_t compressed_size, encrypted_size; 1017 uint32_t send_size; 1005 1018 1006 1019 send_size = p7_message->binary_size; … … 1010 1023 1011 1024 if(p7_socket->compression_enabled) { 1012 if(!_wi_p7_socket_xcompress_buffer(p7_socket, 1013 _WI_P7_SOCKET_COMPRESS, 1014 send_buffer, 1015 send_size, 1016 &compressed_buffer, 1017 &compressed_size)) { 1018 goto end; 1019 } 1025 compressed_size = _wi_p7_socket_xcompress_buffer(p7_socket, 1026 _WI_P7_SOCKET_COMPRESS, 1027 send_buffer, 1028 send_size); 1029 1030 if(compressed_size < 0) 1031 return false; 1020 1032 1021 1033 send_size = compressed_size; 1022 send_buffer = compressed_buffer;1034 send_buffer = p7_socket->compression_buffer; 1023 1035 } 1024 1036 1025 1037 if(p7_socket->encryption_enabled) { 1026 if(!wi_cipher_encrypt_bytes(p7_socket->cipher, 1027 send_buffer, 1028 send_size, 1029 &encrypted_buffer, 1030 &encrypted_size)) { 1031 goto end; 1032 } 1038 encrypted_size = send_size + wi_cipher_block_size(p7_socket->cipher); 1039 1040 if(!p7_socket->encryption_buffer) { 1041 p7_socket->encryption_buffer_length = encrypted_size; 1042 p7_socket->encryption_buffer = wi_malloc(p7_socket->encryption_buffer_length); 1043 } 1044 else if((wi_uinteger_t) encrypted_size > p7_socket->encryption_buffer_length) { 1045 p7_socket->encryption_buffer_length = encrypted_size * 2; 1046 p7_socket->encryption_buffer = wi_realloc(p7_socket->encryption_buffer, p7_socket->encryption_buffer_length); 1047 } 1048 1049 encrypted_size = wi_cipher_encrypt_bytes(p7_socket->cipher, 1050 send_buffer, 1051 send_size, 1052 p7_socket->encryption_buffer); 1053 1054 if(encrypted_size < 0) 1055 return false; 1033 1056 1034 1057 send_size = encrypted_size; 1035 send_buffer = encrypted_buffer;1058 send_buffer = p7_socket->encryption_buffer; 1036 1059 } 1037 1060 … … 1041 1064 1042 1065 if(wi_socket_write_buffer(p7_socket->socket, timeout, length_buffer, sizeof(length_buffer)) < 0) 1043 goto end;1066 return false; 1044 1067 1045 1068 if(wi_socket_write_buffer(p7_socket->socket, timeout, send_buffer, send_size) < 0) 1046 goto end;1069 return false; 1047 1070 1048 1071 if(p7_socket->checksum_enabled) { … … 1050 1073 1051 1074 if(wi_socket_write_buffer(p7_socket->socket, timeout, checksum_buffer, p7_socket->checksum_length) < 0) 1052 goto end; 1053 } 1054 1055 result = true; 1056 1057 end: 1058 wi_free(compressed_buffer); 1059 wi_free(encrypted_buffer); 1060 1061 return result; 1075 return false; 1076 } 1077 1078 return true; 1062 1079 } 1063 1080 … … 1078 1095 static wi_p7_message_t * _wi_p7_socket_read_binary_message(wi_p7_socket_t *p7_socket, wi_time_interval_t timeout, uint32_t message_size) { 1079 1096 wi_p7_message_t *p7_message; 1080 void *decompressed_buffer, *decrypted_buffer;1081 1097 unsigned char local_checksum_buffer[_WI_P7_SOCKET_CHECKSUM_LENGTH]; 1082 1098 unsigned char remote_checksum_buffer[_WI_P7_SOCKET_CHECKSUM_LENGTH]; 1083 uint32_tdecompressed_size, decrypted_size;1099 wi_integer_t decompressed_size, decrypted_size; 1084 1100 int32_t length; 1085 1101 … … 1092 1108 p7_message = wi_autorelease(wi_p7_message_init_with_serialization(wi_p7_message_alloc(), 1093 1109 WI_P7_BINARY, wi_p7_socket_spec(p7_socket))); 1094 1095 if(!p7_message->binary_buffer) { 1096 p7_message->binary_capacity = message_size; 1097 p7_message->binary_buffer = wi_malloc(p7_message->binary_capacity); 1098 } 1099 else if(message_size > p7_message->binary_capacity) { 1100 p7_message->binary_capacity = message_size; 1101 p7_message->binary_buffer = wi_realloc(p7_message->binary_buffer, message_size); 1102 } 1110 p7_message->binary_capacity = message_size; 1111 p7_message->binary_buffer = wi_malloc(p7_message->binary_capacity); 1103 1112 1104 1113 length = wi_socket_read_buffer(p7_socket->socket, timeout, p7_message->binary_buffer, message_size); … … 1111 1120 1112 1121 if(p7_socket->encryption_enabled) { 1113 if(!wi_cipher_decrypt_bytes(p7_socket->cipher, 1114 p7_message->binary_buffer, 1115 p7_message->binary_size, 1116 &decrypted_buffer, 1117 &decrypted_size)) { 1122 decrypted_size = p7_message->binary_size + wi_cipher_block_size(p7_socket->cipher); 1123 1124 if(!p7_socket->decryption_buffer) { 1125 p7_socket->decryption_buffer_length = decrypted_size; 1126 p7_socket->decryption_buffer = wi_malloc(p7_socket->decryption_buffer_length); 1127 } 1128 else if((wi_uinteger_t) decrypted_size > p7_socket->decryption_buffer_length) { 1129 p7_socket->decryption_buffer_length = decrypted_size * 2; 1130 p7_socket->decryption_buffer = wi_realloc(p7_socket->decryption_buffer, p7_socket->decryption_buffer_length); 1131 } 1132 1133 decrypted_size = wi_cipher_decrypt_bytes(p7_socket->cipher, 1134 p7_message->binary_buffer, 1135 p7_message->binary_size, 1136 p7_socket->decryption_buffer); 1137 1138 if(decrypted_size < 0) 1118 1139 return NULL; 1119 } 1120 1121 wi_free(p7_message->binary_buffer); 1122 1123 p7_message->binary_size = decrypted_size; 1124 p7_message->binary_capacity = decrypted_size; 1125 p7_message->binary_buffer = decrypted_buffer; 1140 1141 if((wi_uinteger_t) decrypted_size > p7_message->binary_capacity) { 1142 p7_message->binary_capacity = decrypted_size; 1143 p7_message->binary_buffer = wi_realloc(p7_message->binary_buffer, p7_message->binary_capacity); 1144 } 1145 1146 memcpy(p7_message->binary_buffer, p7_socket->decryption_buffer, decrypted_size); 1147 1148 p7_message->binary_size = decrypted_size; 1126 1149 } 1127 1150 1128 1151 if(p7_socket->compression_enabled) { 1129 if(!_wi_p7_socket_xcompress_buffer(p7_socket,1130 _WI_P7_SOCKET_DECOMPRESS,1131 p7_message->binary_buffer,1132 p7_message->binary_size,1133 &decompressed_buffer,1134 &decompressed_size)) {1152 decompressed_size = _wi_p7_socket_xcompress_buffer(p7_socket, 1153 _WI_P7_SOCKET_DECOMPRESS, 1154 p7_message->binary_buffer, 1155 p7_message->binary_size); 1156 1157 if(decompressed_size < 0) 1135 1158 return NULL; 1136 } 1137 1138 wi_free(p7_message->binary_buffer); 1139 1140 p7_message->binary_size = decompressed_size; 1141 p7_message->binary_capacity = decompressed_size; 1142 p7_message->binary_buffer = decompressed_buffer; 1159 1160 if((wi_uinteger_t) decompressed_size > p7_message->binary_capacity) { 1161 p7_message->binary_capacity = decompressed_size; 1162 p7_message->binary_buffer = wi_realloc(p7_message->binary_buffer, p7_message->binary_capacity); 1163 } 1164 1165 memcpy(p7_message->binary_buffer, p7_socket->compression_buffer, decompressed_size); 1166 1167 p7_message->binary_size = decompressed_size; 1143 1168 } 1144 1169 … … 1236 1261 1237 1262 1238 static wi_boolean_t _wi_p7_socket_xcompress_buffer(wi_p7_socket_t *p7_socket, _wi_p7_socket_compression_t compression, const void *in_buffer, uint32_t in_size, void **out_buffer, uint32_t *out_size) { 1239 const void *input; 1240 void *output, *working_buffer; 1241 z_stream *stream; 1242 uint32_t offset, input_size, input_processed, output_size, working_size, total_working_size; 1243 int err; 1244 1245 stream = (compression == _WI_P7_SOCKET_COMPRESS) ? &p7_socket->compression_stream : &p7_socket->decompression_stream; 1246 working_size = (compression == _WI_P7_SOCKET_COMPRESS) ? in_size : 4 * in_size; 1247 working_buffer = wi_malloc(working_size); 1248 input = in_buffer; 1249 input_size = in_size; 1250 output = working_buffer; 1251 output_size = working_size; 1252 total_working_size = 0; 1263 static wi_integer_t _wi_p7_socket_xcompress_buffer(wi_p7_socket_t *p7_socket, _wi_p7_socket_compression_t compression, const void *in_buffer, uint32_t in_size) { 1264 const void *input; 1265 void *output; 1266 z_stream *stream; 1267 uint32_t offset, input_size, input_processed, output_size, total_size; 1268 int err; 1269 1270 if(!p7_socket->compression_buffer) { 1271 p7_socket->compression_buffer_length = in_size * 4; 1272 p7_socket->compression_buffer = wi_malloc(p7_socket->compression_buffer_length); 1273 } 1274 else if(in_size > p7_socket->compression_buffer_length) { 1275 p7_socket->compression_buffer_length = in_size * 4; 1276 p7_socket->compression_buffer = wi_realloc(p7_socket->compression_buffer, p7_socket->compression_buffer_length); 1277 } 1278 1279 stream = (compression == _WI_P7_SOCKET_COMPRESS) ? &p7_socket->compression_stream : &p7_socket->decompression_stream; 1280 input = in_buffer; 1281 input_size = in_size; 1282 output = p7_socket->compression_buffer; 1283 output_size = p7_socket->compression_buffer_length; 1284 total_size = 0; 1253 1285 1254 1286 while(true) { … … 1258 1290 wi_error_set_zlib_error(err); 1259 1291 1260 wi_free(working_buffer); 1261 1262 return false; 1263 } 1264 1265 total_working_size += output_size; 1292 return -1; 1293 } 1294 1295 total_size += output_size; 1266 1296 1267 1297 if(stream->avail_out > 0) 1268 1298 break; 1269 1299 1270 offset = (output - working_buffer) + output_size; 1271 working_size *= 4; 1272 working_buffer = wi_realloc(working_buffer, working_size); 1273 output = working_buffer + offset; 1274 output_size = working_size - offset; 1275 input += input_processed; 1276 input_size -= input_processed; 1300 p7_socket->compression_buffer_length *= 4; 1301 p7_socket->compression_buffer = wi_realloc(p7_socket->compression_buffer, p7_socket->compression_buffer_length); 1302 1303 offset = (output - p7_socket->compression_buffer) + output_size; 1304 output = p7_socket->compression_buffer + offset; 1305 output_size = p7_socket->compression_buffer_length - offset; 1306 input += input_processed; 1307 input_size -= input_processed; 1277 1308 1278 1309 if(compression == _WI_P7_SOCKET_DECOMPRESS && input_size == 0) … … 1280 1311 } 1281 1312 1282 *out_buffer = working_buffer; 1283 *out_size = total_working_size; 1284 1285 return true; 1313 return total_size; 1286 1314 } 1287 1315 … … 1508 1536 wi_boolean_t wi_p7_socket_write_oobdata(wi_p7_socket_t *p7_socket, wi_time_interval_t timeout, const void *buffer, uint32_t size) { 1509 1537 const void *send_buffer; 1510 void *compressed_buffer = NULL, *encrypted_buffer = NULL;1511 1538 char length_buffer[_WI_P7_SOCKET_LENGTH_SIZE]; 1512 1539 unsigned char checksum_buffer[_WI_P7_SOCKET_CHECKSUM_LENGTH]; 1513 uint32_t send_size,compressed_size, encrypted_size;1514 wi_boolean_t result = false;1540 wi_integer_t compressed_size, encrypted_size; 1541 uint32_t send_size; 1515 1542 1516 1543 send_size = size; … … 1521 1548 1522 1549 if(p7_socket->compression_enabled) { 1523 if(!_wi_p7_socket_xcompress_buffer(p7_socket, 1524 _WI_P7_SOCKET_COMPRESS, 1525 send_buffer, 1526 send_size, 1527 &compressed_buffer, 1528 &compressed_size)) { 1529 goto end; 1530 } 1550 compressed_size = _wi_p7_socket_xcompress_buffer(p7_socket, 1551 _WI_P7_SOCKET_COMPRESS, 1552 send_buffer, 1553 send_size); 1554 1555 if(compressed_size < 0) 1556 return false; 1531 1557 1532 1558 send_size = compressed_size; 1533 send_buffer = compressed_buffer;1559 send_buffer = p7_socket->compression_buffer; 1534 1560 } 1535 1561 1536 1562 if(p7_socket->encryption_enabled) { 1537 if(!wi_cipher_encrypt_bytes(p7_socket->cipher, 1538 send_buffer, 1539 send_size, 1540 &encrypted_buffer, 1541 &encrypted_size)) { 1542 goto end; 1543 } 1563 encrypted_size = send_size + wi_cipher_block_size(p7_socket->cipher); 1564 1565 if(!p7_socket->encryption_buffer) { 1566 p7_socket->encryption_buffer_length = encrypted_size; 1567 p7_socket->encryption_buffer = wi_malloc(p7_socket->encryption_buffer_length); 1568 } 1569 else if((wi_uinteger_t) encrypted_size > p7_socket->encryption_buffer_length) { 1570 p7_socket->encryption_buffer_length = encrypted_size * 2; 1571 p7_socket->encryption_buffer = wi_realloc(p7_socket->encryption_buffer, p7_socket->encryption_buffer_length); 1572 } 1573 1574 encrypted_size = wi_cipher_encrypt_bytes(p7_socket->cipher, 1575 send_buffer, 1576 send_size, 1577 p7_socket->encryption_buffer); 1578 1579 if(encrypted_size < 0) 1580 return false; 1544 1581 1545 1582 send_size = encrypted_size; 1546 send_buffer = encrypted_buffer;1583 send_buffer = p7_socket->encryption_buffer; 1547 1584 } 1548 1585 … … 1550 1587 1551 1588 if(wi_socket_write_buffer(p7_socket->socket, timeout, length_buffer, sizeof(length_buffer)) < 0) 1552 goto end;1589 return false; 1553 1590 1554 1591 if(wi_socket_write_buffer(p7_socket->socket, timeout, send_buffer, send_size) < 0) 1555 goto end;1592 return false; 1556 1593 1557 1594 if(p7_socket->checksum_enabled) { 1558 1595 if(wi_socket_write_buffer(p7_socket->socket, timeout, checksum_buffer, p7_socket->checksum_length) < 0) 1559 goto end; 1560 } 1561 1562 result = true; 1563 1564 end: 1565 wi_free(compressed_buffer); 1566 wi_free(encrypted_buffer); 1567 1568 return result; 1596 return false; 1597 } 1598 1599 return true; 1569 1600 } 1570 1601 … … 1572 1603 1573 1604 wi_integer_t wi_p7_socket_read_oobdata(wi_p7_socket_t *p7_socket, wi_time_interval_t timeout, void **out_buffer) { 1574 void *receive_buffer = NULL, *decrypted_buffer, *decompressed_buffer;1605 void *receive_buffer; 1575 1606 char length_buffer[_WI_P7_SOCKET_LENGTH_SIZE]; 1576 1607 unsigned char local_checksum_buffer[_WI_P7_SOCKET_CHECKSUM_LENGTH]; 1577 1608 unsigned char remote_checksum_buffer[_WI_P7_SOCKET_CHECKSUM_LENGTH]; 1578 wi_integer_t result ;1579 uint32_t receive_size, received_size , decompressed_size, decrypted_size;1609 wi_integer_t result, decompressed_size, decrypted_size; 1610 uint32_t receive_size, received_size; 1580 1611 1581 1612 result = wi_socket_read_buffer(p7_socket->socket, timeout, length_buffer, sizeof(length_buffer)); … … 1594 1625 } 1595 1626 1596 receive_buffer = wi_malloc(receive_size); 1627 if(!p7_socket->oobdata_read_buffer) { 1628 p7_socket->oobdata_read_buffer_length = receive_size * 2; 1629 p7_socket->oobdata_read_buffer = wi_malloc(p7_socket->oobdata_read_buffer_length); 1630 } 1631 else if(receive_size > p7_socket->oobdata_read_buffer_length) { 1632 p7_socket->oobdata_read_buffer_length = receive_size * 2; 1633 p7_socket->oobdata_read_buffer = wi_realloc(p7_socket->oobdata_read_buffer, p7_socket->oobdata_read_buffer_length); 1634 } 1635 1636 receive_buffer = p7_socket->oobdata_read_buffer; 1597 1637 received_size = 0; 1598 1638 … … 1607 1647 1608 1648 if(p7_socket->encryption_enabled) { 1609 if(!wi_cipher_decrypt_bytes(p7_socket->cipher, 1610 receive_buffer, 1611 receive_size, 1612 &decrypted_buffer, 1613 &decrypted_size)) { 1649 decrypted_size = receive_size + wi_cipher_block_size(p7_socket->cipher); 1650 1651 if(!p7_socket->decryption_buffer) { 1652 p7_socket->decryption_buffer_length = decrypted_size; 1653 p7_socket->decryption_buffer = wi_malloc(p7_socket->decryption_buffer_length); 1654 } 1655 else if((wi_uinteger_t) decrypted_size > p7_socket->decryption_buffer_length) { 1656 p7_socket->decryption_buffer_length = decrypted_size * 2; 1657 p7_socket->decryption_buffer = wi_realloc(p7_socket->decryption_buffer, p7_socket->decryption_buffer_length); 1658 } 1659 1660 decrypted_size = wi_cipher_decrypt_bytes(p7_socket->cipher, 1661 receive_buffer, 1662 receive_size, 1663 p7_socket->decryption_buffer); 1664 1665 if(decrypted_size < 0) { 1614 1666 result = -1; 1615 1667 1616 1668 goto end; 1617 1669 } 1618 1619 wi_free(receive_buffer);1620 1670 1621 1671 receive_size = decrypted_size; 1622 receive_buffer = decrypted_buffer;1672 receive_buffer = p7_socket->decryption_buffer; 1623 1673 } 1624 1674 1625 1675 if(p7_socket->compression_enabled) { 1626 if(!_wi_p7_socket_xcompress_buffer(p7_socket,1627 _WI_P7_SOCKET_DECOMPRESS,1628 receive_buffer,1629 receive_size,1630 &decompressed_buffer,1631 &decompressed_size)) {1676 decompressed_size = _wi_p7_socket_xcompress_buffer(p7_socket, 1677 _WI_P7_SOCKET_DECOMPRESS, 1678 receive_buffer, 1679 receive_size); 1680 1681 if(decompressed_size < 0) { 1632 1682 result = -1; 1633 1683 1634 1684 goto end; 1635 1685 } 1636 1637 wi_free(receive_buffer);1638 1686 1639 1687 receive_size = decompressed_size; 1640 receive_buffer = decompressed_buffer;1688 receive_buffer = p7_socket->compression_buffer; 1641 1689 } 1642 1690 … … 1663 1711 if(result > 0) 1664 1712 *out_buffer = receive_buffer; 1665 else if(receive_buffer)1666 wi_free(receive_buffer);1667 1713 1668 1714 return result;
