コード例 #1
0
ファイル: wi-crypto.c プロジェクト: ProfDrLuigi/zanka
wi_cipher_t * wi_cipher_init_with_key(wi_cipher_t *cipher, wi_cipher_type_t type, wi_data_t *key, wi_data_t *iv) {
	unsigned char		*key_buffer, *iv_buffer;
	
	cipher->type	= type;
	cipher->cipher	= _wi_cipher_cipher(cipher);
	
	key_buffer		= (unsigned char *) wi_data_bytes(key);
	iv_buffer		= iv ? (unsigned char *) wi_data_bytes(iv) : NULL;
	
	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;
	}
	
	cipher->key		= wi_retain(key);
	cipher->iv		= wi_retain(iv);

	return cipher;
}
コード例 #2
0
ファイル: wi-sqlite3.c プロジェクト: ProfDrLuigi/zanka
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++;
	}
}
コード例 #3
0
ファイル: wi-md5.c プロジェクト: axelandersson/libwired
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);
}
コード例 #4
0
ファイル: wi-sha2.c プロジェクト: haifenghuang/libwired
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);
}
コード例 #5
0
ファイル: wi-crypto.c プロジェクト: ProfDrLuigi/zanka
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);
}
コード例 #6
0
ファイル: wi-crypto.c プロジェクト: ProfDrLuigi/zanka
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);
}
コード例 #7
0
ファイル: wi-cipher.c プロジェクト: ProfDrLuigi/zanka
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);
}
コード例 #8
0
ファイル: wi-p7-message.c プロジェクト: ProfDrLuigi/zanka
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;
}
コード例 #9
0
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
}
コード例 #10
0
ファイル: wi-crypto.c プロジェクト: ProfDrLuigi/zanka
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;
}
コード例 #11
0
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
}
コード例 #12
0
ファイル: wi-p7-message.c プロジェクト: ProfDrLuigi/zanka
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;
}
コード例 #13
0
ファイル: wi-p7-message.c プロジェクト: ProfDrLuigi/zanka
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);
}
コード例 #14
0
ファイル: wi-file.c プロジェクト: axelandersson/libwired
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));
}
コード例 #15
0
ファイル: wi-cipher.c プロジェクト: ProfDrLuigi/zanka
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;
}
コード例 #16
0
ファイル: wi-cipher.c プロジェクト: ProfDrLuigi/zanka
wi_integer_t wi_cipher_decrypt_bytes(wi_cipher_t *cipher, const void *encrypted_buffer, wi_uinteger_t encrypted_length, void *decrypted_buffer) {
#ifdef WI_CIPHER_OPENSSL
	int			decrypted_length, padded_length;
	
	if(EVP_DecryptUpdate(&cipher->decrypt_ctx, decrypted_buffer, &decrypted_length, encrypted_buffer, encrypted_length) != 1) {
		wi_error_set_openssl_error();
		
		return -1;
	}
	
	if(EVP_DecryptFinal_ex(&cipher->decrypt_ctx, decrypted_buffer + decrypted_length, &padded_length) != 1) {
		wi_error_set_openssl_error();
		
		return -1;
	}
	
	if(EVP_DecryptInit_ex(&cipher->decrypt_ctx, NULL, NULL, NULL, NULL) != 1) {
		wi_error_set_openssl_error();
		
		return -1;
	}
	
	return decrypted_length + padded_length;
#endif

#ifdef WI_CIPHER_COMMONCRYPTO
	CCCryptorStatus		status;
	size_t				available_length, decrypted_length, padded_length;
	
	available_length = wi_cipher_block_size(cipher) + encrypted_length;
	
	status = CCCryptorUpdate(cipher->decrypt_ref,
							 encrypted_buffer,
							 encrypted_length,
							 decrypted_buffer,
							 available_length,
							 &decrypted_length);
	
	if(status != kCCSuccess) {
		wi_error_set_commoncrypto_error(status);
		
		return -1;
	}

	status = CCCryptorFinal(cipher->decrypt_ref,
							decrypted_buffer + decrypted_length,
							available_length - decrypted_length,
							&padded_length);
	
	if(status != kCCSuccess) {
		wi_error_set_commoncrypto_error(status);
		
		return -1;
	}
	
	status = CCCryptorReset(cipher->decrypt_ref, cipher->iv ? wi_data_bytes(cipher->iv) : NULL);
	
	if(status != kCCSuccess) {
		wi_error_set_commoncrypto_error(status);
		
		return -1;
	}
	
	return decrypted_length + padded_length;
#endif
}
コード例 #17
0
ファイル: wi-string.c プロジェクト: ProfDrLuigi/zanka
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));
}
コード例 #18
0
ファイル: wi-socket.c プロジェクト: ProfDrLuigi/zanka
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));
}
コード例 #19
0
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));
}