void wi_test_dsa_accessors(void) { #ifdef WI_DSA wi_dsa_t *dsa; dsa = wi_autorelease(wi_dsa_init_with_bits(wi_dsa_alloc(), 512)); WI_TEST_ASSERT_TRUE(wi_data_length(wi_dsa_public_key(dsa)) > 0, ""); WI_TEST_ASSERT_TRUE(wi_data_length(wi_dsa_private_key(dsa)) > 0, ""); WI_TEST_ASSERT_EQUALS(wi_dsa_bits(dsa), 384U, ""); #endif }
static void _wi_sqlite3_bind_statement(wi_sqlite3_statement_t *statement, va_list ap) { wi_string_t *string; wi_runtime_instance_t *instance; wi_runtime_id_t id; wi_uinteger_t index; int result; index = 1; while((instance = va_arg(ap, wi_runtime_instance_t *))) { id = wi_runtime_id(instance); result = SQLITE_OK; if(id == wi_string_runtime_id()) { result = sqlite3_bind_text(statement->statement, index, wi_string_cstring(instance), wi_string_length(instance), SQLITE_STATIC); } else if(id == wi_number_runtime_id()) { switch(wi_number_storage_type(instance)) { case WI_NUMBER_STORAGE_INT8: case WI_NUMBER_STORAGE_INT16: case WI_NUMBER_STORAGE_INT32: case WI_NUMBER_STORAGE_INT64: result = sqlite3_bind_int64(statement->statement, index, wi_number_int64(instance)); break; case WI_NUMBER_STORAGE_FLOAT: case WI_NUMBER_STORAGE_DOUBLE: result = sqlite3_bind_double(statement->statement, index, wi_number_double(instance)); break; } } else if(id == wi_uuid_runtime_id()) { string = wi_uuid_string(instance); result = sqlite3_bind_text(statement->statement, index, wi_string_cstring(string), wi_string_length(string), SQLITE_STATIC); } else if(id == wi_date_runtime_id()) { string = wi_date_sqlite3_string(instance); result = sqlite3_bind_text(statement->statement, index, wi_string_cstring(string), wi_string_length(string), SQLITE_STATIC); } else if(id == wi_null_runtime_id()) { result = sqlite3_bind_null(statement->statement, index); } else if(id == wi_data_runtime_id()) { result = sqlite3_bind_blob(statement->statement, index, wi_data_bytes(instance), wi_data_length(instance), SQLITE_STATIC); } else { WI_ASSERT(0, "%@ is not a supported data type", instance); } WI_ASSERT(result == SQLITE_OK, "error %d while binding parameter %u", result, index); index++; } }
wi_string_t * wi_sha2_digest_string(wi_sha2_bits_t bits, wi_data_t *data) { wi_sha2_t *sha2; sha2 = wi_sha2_with_bits(bits); wi_sha2_update(sha2, wi_data_bytes(data), wi_data_length(data)); wi_sha2_close(sha2); return wi_sha2_string(sha2); }
wi_string_t * wi_md5_digest_string(wi_data_t *data) { wi_md5_t *md5; md5 = wi_md5(); wi_md5_update(md5, wi_data_bytes(data), wi_data_length(data)); wi_md5_close(md5); return wi_md5_string(md5); }
wi_data_t * wi_rsa_encrypt(wi_rsa_t *rsa, wi_data_t *decrypted_data) { const void *decrypted_buffer; void *encrypted_buffer; wi_uinteger_t decrypted_length, encrypted_length; decrypted_buffer = wi_data_bytes(decrypted_data); decrypted_length = wi_data_length(decrypted_data); if(!wi_rsa_encrypt_bytes(rsa, decrypted_buffer, decrypted_length, &encrypted_buffer, &encrypted_length)) return NULL; return wi_data_with_bytes_no_copy(encrypted_buffer, encrypted_length, true); }
wi_data_t * wi_cipher_decrypt(wi_cipher_t *cipher, wi_data_t *encrypted_data) { const void *encrypted_buffer; void *decrypted_buffer; wi_uinteger_t encrypted_length, decrypted_length; encrypted_buffer = wi_data_bytes(encrypted_data); encrypted_length = wi_data_length(encrypted_data); if(!wi_cipher_decrypt_bytes(cipher, encrypted_buffer, encrypted_length, &decrypted_buffer, &decrypted_length)) return NULL; return wi_data_with_bytes_no_copy(decrypted_buffer, decrypted_length, true); }
wi_data_t * wi_cipher_decrypt(wi_cipher_t *cipher, wi_data_t *encrypted_data) { const void *encrypted_buffer; void *decrypted_buffer; wi_uinteger_t encrypted_length; wi_integer_t decrypted_length; encrypted_buffer = wi_data_bytes(encrypted_data); encrypted_length = wi_data_length(encrypted_data); decrypted_buffer = wi_malloc(wi_cipher_block_size(cipher) + encrypted_length); decrypted_length = wi_cipher_decrypt_bytes(cipher, encrypted_buffer, encrypted_length, decrypted_buffer); if(decrypted_length < 0) return NULL; return wi_data_with_bytes_no_copy(decrypted_buffer, decrypted_length, true); }
wi_boolean_t wi_p7_message_set_data_for_name(wi_p7_message_t *p7_message, wi_data_t *data, wi_string_t *field_name) { unsigned char *binary; uint32_t field_size, field_id; if(!data) data = wi_data(); field_size = wi_data_length(data); if(!_wi_p7_message_get_binary_buffer_for_writing_for_name(p7_message, field_name, field_size, &binary, &field_id)) return false; wi_write_swap_host_to_big_int32(binary, 0, field_id); wi_write_swap_host_to_big_int32(binary, 4, field_size); memcpy(binary + 8, wi_data_bytes(data), field_size); return true; }
void wi_test_string_encoding_conversion(void) { #ifdef WI_STRING_ENCODING wi_string_t *string, *path; wi_data_t *data; wi_string_encoding_t *encoding; encoding = wi_string_encoding_with_charset(WI_STR("ISO-8859-1"), 0); string = wi_string_with_c_string("hello world", encoding); WI_TEST_ASSERT_EQUAL_INSTANCES(string, WI_STR("hello world"), ""); data = wi_data_with_base64_string(WI_STR("aGVsbG8gd29ybGQ=")); string = wi_string_with_data(data, encoding); WI_TEST_ASSERT_EQUAL_INSTANCES(string, WI_STR("hello world"), ""); string = wi_string_with_bytes(wi_data_bytes(data), wi_data_length(data), encoding); WI_TEST_ASSERT_EQUAL_INSTANCES(string, WI_STR("hello world"), ""); path = wi_string_by_appending_path_component(wi_test_fixture_path, WI_STR("wi-string-encoding-tests-1.txt")); string = wi_string_with_contents_of_file(path, encoding); WI_TEST_ASSERT_NOT_NULL(string, ""); WI_TEST_ASSERT_EQUALS(wi_string_character_at_index(string, 0), 'h', ""); WI_TEST_ASSERT_EQUALS(wi_string_length(string), 14, ""); data = wi_string_data(WI_STR("hello world"), encoding); WI_TEST_ASSERT_EQUAL_INSTANCES(data, wi_data_with_base64_string(WI_STR("aGVsbG8gd29ybGQ=")), ""); encoding = wi_string_encoding_with_charset(WI_STR("ASCII"), 0); string = wi_string_with_contents_of_file(path, encoding); WI_TEST_ASSERT_NULL(string, ""); encoding = wi_string_encoding_with_charset(WI_STR("hello world"), 0); string = wi_string_with_contents_of_file(path, encoding); WI_TEST_ASSERT_NULL(string, ""); #endif }
wi_rsa_t * wi_rsa_init_with_public_key(wi_rsa_t *rsa, wi_data_t *data) { const unsigned char *buffer; long length; buffer = wi_data_bytes(data); length = wi_data_length(data); rsa->rsa = d2i_RSAPublicKey(NULL, (const unsigned char **) &buffer, length); if(!rsa->rsa) { wi_error_set_openssl_error(); wi_release(rsa); return NULL; } rsa->public_key = wi_retain(data); return rsa; }
void wi_test_sha2_digest(void) { #ifdef WI_SHA1 wi_sha2_t *sha2; wi_data_t *data; unsigned char buffer[WI_SHA2_MAX_LENGTH]; data = wi_data(); wi_sha2_digest(WI_SHA2_256, wi_data_bytes(data), wi_data_length(data), buffer); WI_TEST_ASSERT_EQUAL_INSTANCES(wi_data_with_bytes(buffer, WI_SHA2_256_LENGTH), wi_data_with_base64_string(WI_STR("47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU=")), ""); WI_TEST_ASSERT_EQUAL_INSTANCES(wi_sha2_digest_string(WI_SHA2_256, data), WI_STR("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"), ""); sha2 = wi_sha2_with_bits(WI_SHA2_512); wi_sha2_close(sha2); wi_sha2_get_data(sha2, buffer); WI_TEST_ASSERT_EQUAL_INSTANCES(wi_data_with_bytes(buffer, WI_SHA2_512_LENGTH), wi_data_with_base64_string(WI_STR("z4PhNX7vuL3xVChQ1m2AB9Yg5AULVxXcg/SpIdNs6c5H0NE8XYXysP+DGNKHfuwvY7kxvUdBeoGlODJ6+SfaPg==")), ""); WI_TEST_ASSERT_EQUAL_INSTANCES(wi_sha2_data(sha2), wi_data_with_base64_string(WI_STR("z4PhNX7vuL3xVChQ1m2AB9Yg5AULVxXcg/SpIdNs6c5H0NE8XYXysP+DGNKHfuwvY7kxvUdBeoGlODJ6+SfaPg==")), ""); #endif }
wi_boolean_t wi_p7_message_set_data_for_name(wi_p7_message_t *p7_message, wi_data_t *data, wi_string_t *field_name) { unsigned char *binary; uint32_t field_size, field_id; if(!data) data = wi_data(); if(p7_message->serialization == WI_P7_BINARY) { field_size = wi_data_length(data); if(!_wi_p7_message_get_binary_buffer_for_writing_for_name(p7_message, field_name, field_size, &binary, &field_id)) return false; wi_write_swap_host_to_big_int32(binary, 0, field_id); wi_write_swap_host_to_big_int32(binary, 4, field_size); memcpy(binary + 8, wi_data_bytes(data), field_size); } else { _wi_p7_message_set_xml_field(p7_message, WI_P7_DATA, field_name, wi_data_base64(data)); } return true; }
wi_string_t * wi_string_init_with_data(wi_string_t *string, wi_data_t *data) { return wi_string_init_with_bytes(string, wi_data_bytes(data), wi_data_length(data)); }
wi_integer_t wi_file_write(wi_file_t *file, wi_data_t *data) { return wi_file_write_bytes(file, wi_data_bytes(data), wi_data_length(data)); }
static wi_cipher_t * _wi_cipher_init_with_key(wi_cipher_t *cipher, wi_data_t *key, wi_data_t *iv) { #ifdef WI_CIPHER_COMMONCRYPTO CCCryptorStatus status; #endif unsigned char *key_buffer, *iv_buffer; key_buffer = (unsigned char *) wi_data_bytes(key); iv_buffer = iv ? (unsigned char *) wi_data_bytes(iv) : NULL; cipher->key = wi_retain(key); cipher->iv = wi_retain(iv); #ifdef WI_CIPHER_OPENSSL if(EVP_EncryptInit(&cipher->encrypt_ctx, cipher->cipher, NULL, NULL) != 1) { wi_error_set_openssl_error(); wi_release(cipher); return NULL; } if(EVP_DecryptInit(&cipher->decrypt_ctx, cipher->cipher, NULL, NULL) != 1) { wi_error_set_openssl_error(); wi_release(cipher); return NULL; } _wi_cipher_configure_cipher(cipher); if(EVP_EncryptInit(&cipher->encrypt_ctx, cipher->cipher, key_buffer, iv_buffer) != 1) { wi_error_set_openssl_error(); wi_release(cipher); return NULL; } if(EVP_DecryptInit(&cipher->decrypt_ctx, cipher->cipher, key_buffer, iv_buffer) != 1) { wi_error_set_openssl_error(); wi_release(cipher); return NULL; } #endif #ifdef WI_CIPHER_COMMONCRYPTO status = CCCryptorCreate(kCCEncrypt, cipher->algorithm, kCCOptionPKCS7Padding, key_buffer, wi_data_length(cipher->key), iv_buffer, &cipher->encrypt_ref); if(status != kCCSuccess) { wi_error_set_commoncrypto_error(status); wi_release(cipher); return NULL; } status = CCCryptorCreate(kCCDecrypt, cipher->algorithm, kCCOptionPKCS7Padding, key_buffer, wi_data_length(cipher->key), iv_buffer, &cipher->decrypt_ref); if(status != kCCSuccess) { wi_error_set_commoncrypto_error(status); wi_release(cipher); return NULL; } #endif return cipher; }
static wi_boolean_t wd_transfers_run_upload(wd_transfer_t *transfer, wd_user_t *user, wi_p7_message_t *message) { wi_p7_message_t *reply; wi_string_t *path; wi_p7_uint32_t transaction; wi_boolean_t result; reply = wi_p7_message_with_name(WI_STR("wired.transfer.upload_ready"), wd_p7_spec); wi_p7_message_set_string_for_name(reply, transfer->path, WI_STR("wired.file.path")); wi_p7_message_set_oobdata_for_name(reply, transfer->dataoffset, WI_STR("wired.transfer.data_offset")); wi_p7_message_set_oobdata_for_name(reply, transfer->rsrcoffset, WI_STR("wired.transfer.rsrc_offset")); if(wi_p7_message_get_uint32_for_name(message, &transaction, WI_STR("wired.transaction"))) wi_p7_message_set_uint32_for_name(reply, transaction, WI_STR("wired.transaction")); if(!wd_user_write_message(user, 30.0, reply)) { wi_log_error(WI_STR("Could not write message \"%@\" to %@: %m"), wi_p7_message_name(reply), wd_user_identifier(user)); return false; } reply = wd_user_read_message(user, 30.0); if(!reply) { wi_log_warn(WI_STR("Could not read message from %@ while waiting for upload: %m"), wd_user_identifier(user)); return false; } if(!wi_p7_spec_verify_message(wd_p7_spec, reply)) { wi_log_error(WI_STR("Could not verify message from %@ while waiting for upload: %m"), wd_user_identifier(user)); wd_user_reply_error(user, WI_STR("wired.error.invalid_message"), reply); return false; } if(!wi_is_equal(wi_p7_message_name(reply), WI_STR("wired.transfer.upload"))) { wi_log_error(WI_STR("Could not accept message %@ from %@: Expected \"wired.transfer.upload\""), wi_p7_message_name(reply), wd_user_identifier(user)); wd_user_reply_error(user, WI_STR("wired.error.invalid_message"), reply); return false; } wi_p7_message_get_uint64_for_name(reply, &transfer->remainingdatasize, WI_STR("wired.transfer.data")); wi_p7_message_get_uint64_for_name(reply, &transfer->remainingrsrcsize, WI_STR("wired.transfer.rsrc")); transfer->finderinfo = wi_retain(wi_p7_message_data_for_name(reply, WI_STR("wired.transfer.finderinfo"))); wi_socket_set_interactive(wd_user_socket(user), false); result = wd_transfer_upload(transfer); wi_socket_set_interactive(wd_user_socket(user), true); if(transfer->transferred == transfer->datasize + transfer->rsrcsize) { path = wi_string_by_deleting_path_extension(transfer->realdatapath); if(wi_fs_rename_path(transfer->realdatapath, path)) { if(transfer->executable) { if(!wi_fs_set_mode_for_path(path, 0755)) wi_log_error(WI_STR("Could not set mode for \"%@\": %m"), path); } wd_files_move_comment(transfer->realdatapath, path, NULL, NULL); wd_files_move_label(transfer->realdatapath, path, NULL, NULL); if(wi_data_length(transfer->finderinfo) > 0) wi_fs_set_finder_info_for_path(transfer->finderinfo, path); wd_index_add_file(path); } else { wi_log_error(WI_STR("Could not move \"%@\" to \"%@\": %m"), transfer->realdatapath, path); } wd_accounts_add_upload_statistics(wd_user_account(user), true, transfer->actualtransferred); } else { wd_accounts_add_upload_statistics(wd_user_account(user), false, transfer->actualtransferred); } return result; }
wi_integer_t wi_socket_sendto_data(wi_socket_t *socket, wi_data_t *data) { return wi_socket_sendto_buffer(socket, wi_data_bytes(data), wi_data_length(data)); }
wi_p7_message_t * wi_p7_message_init_with_data(wi_p7_message_t *p7_message, wi_data_t *data, wi_p7_serialization_t serialization, wi_p7_spec_t *p7_spec) { return wi_p7_message_init_with_bytes(p7_message, wi_data_bytes(data), wi_data_length(data), serialization, p7_spec); }
wi_string_t * wi_string_encoding_utf8_string_from_data(wi_string_encoding_t *encoding, wi_data_t *data) { return wi_string_encoding_utf8_string_from_bytes(encoding, wi_data_bytes(data), wi_data_length(data)); }