Commit 417c895b authored by yiwenshao's avatar yiwenshao

able to use wrapper

parent a6614fec
......@@ -77,7 +77,7 @@ $(OBJDIR)/%.o: $(OBJDIR)/%.cc
$(CXX) -MD $(CXXFLAGS) -c $< -o $@
##rules for .cc in debug directory
mtl/%:$(OBJDIR)/debug/%.o
mtl/%:$(OBJDIR)/debug/%.o $(OBJDIR)/libwrapper.so
@mkdir -p $(@D)
$(CXX) -g -o $@ $^ $(CXXFLAGS) $(LDFLAGS) -L/$(MYBUILD)/libmysqld -lmysqld -laio -lz -ldl -lm -lcrypt -lpthread -lwrapper -lcryptdb -ledbcrypto -ledbutil -ledbparser -lntl -lcrypto
......
#include "debug/load.hh"
#include "wrapper/common.hh"
static
Item_null *
make_null(const std::string &name){
char *const n = current_thd->strdup(name.c_str());
return new Item_null(n);
}
static
std::vector<Item *>
itemNullVector(unsigned int count)
{
std::vector<Item *> out;
for (unsigned int i = 0; i < count; ++i) {
out.push_back(make_null(""));
}
return out;
}
static
ResType MygetResTypeFromLuaTable(bool isNULL,rawMySQLReturnValue *inRow = NULL,int in_last_insert_id = 0){
std::vector<std::string> names;
std::vector<enum_field_types> types;
std::vector<std::vector<Item *>> rows;
//return NULL restype
if(isNULL){
return ResType(true,0,0,std::move(names),
std::move(types),std::move(rows));
} else {
for(auto inNames:inRow->fieldNames){
names.push_back(inNames);
}
for(auto inTypes:inRow->fieldTypes){
types.push_back(static_cast<enum_field_types>(inTypes));
}
for(auto inRows:inRow->rowValues) {
std::vector<Item *> curTempRow = itemNullVector(types.size());
for(int i=0;i< (int)(inRows.size());i++){
curTempRow[i] = (MySQLFieldTypeToItem(types[i],inRows[i]) );
}
rows.push_back(curTempRow);
}
return ResType(true,0,in_last_insert_id,std::move(names),std::move(types),std::move(rows));
}
}
//transform rawMySQLReturnValue to ResType
//first step of back
static std::vector<FieldMeta *> getFieldMeta(SchemaInfo &schema,std::string db = "tdb",
std::string table="student1"){
const std::unique_ptr<AES_KEY> &TK = std::unique_ptr<AES_KEY>(getKey(std::string("113341234")));
Analysis analysis(db,schema,TK,
SECURITY_RATING::SENSITIVE);
if(analysis.databaseMetaExists(db)){
const DatabaseMeta & dbm = analysis.getDatabaseMeta(db);
TableMeta & tbm = *dbm.getChild(IdentityMetaKey(table));
return tbm.orderedFieldMetas();
}else{
std::cout<<"data base not exists"<<std::endl;
return std::vector<FieldMeta *>();
}
}
//representation of one field.
struct transField{
bool hasSalt;
FieldMeta *originalFm;
vector<int> choosenOnions;
//used to construct return meta
int onionIndex = 0;
int numOfOnions=0;
//onions
std::vector<std::string> fields;
std::vector<onion> onions;
std::vector<OnionMeta*>originalOm;
void show(){
for(auto i=0U;i<fields.size();i++){
//cout<<fields[i]<<" : "<<gmp2[onions[i]]<<"\t";
}
cout<<endl;
if(hasSalt){
cout<<"has salt"<<endl;
}else cout<<"do not have salt"<<endl;
}
};
/*for each field, convert the format to transField*/
static std::vector<transField> getTransField(std::vector<FieldMeta *> pfms){
std::vector<transField> res;
//for every field
for(auto pfm:pfms){
transField tf;
tf.originalFm = pfm;
for(std::pair<const OnionMetaKey *, OnionMeta *> &ompair:pfm->orderedOnionMetas()){
tf.numOfOnions++;
tf.fields.push_back((ompair.second)->getAnonOnionName());
tf.onions.push_back(ompair.first->getValue());
tf.originalOm.push_back(ompair.second);
}
if(pfm->getHasSalt()){
tf.hasSalt=true;
tf.fields.push_back(pfm->getSaltName());
}
res.push_back(tf);
}
return res;
}
static std::unique_ptr<SchemaInfo> myLoadSchemaInfo() {
std::unique_ptr<Connect> e_conn(Connect::getEmbedded(embeddedDir));
std::unique_ptr<SchemaInfo> schema(new SchemaInfo());
std::function<DBMeta *(DBMeta *const)> loadChildren =
[&loadChildren, &e_conn](DBMeta *const parent) {
auto kids = parent->fetchChildren(e_conn);
for (auto it : kids) {
loadChildren(it);
}
return parent;
};
//load all metadata and then store it in schema
loadChildren(schema.get());
Analysis analysis(std::string("student"),*schema,
std::unique_ptr<AES_KEY>(getKey(std::string("113341234"))),
SECURITY_RATING::SENSITIVE);
return schema;
}
static void
addToReturn(ReturnMeta *const rm, int pos, const OLK &constr,
bool has_salt, const std::string &name) {
const bool test = static_cast<unsigned int>(pos) == rm->rfmeta.size();
TEST_TextMessageError(test, "ReturnMeta has badly ordered"
" ReturnFields!");
const int salt_pos = has_salt ? pos + 1 : -1;
std::pair<int, ReturnField>
pair(pos, ReturnField(false, name, constr, salt_pos));
rm->rfmeta.insert(pair);
}
static void
addSaltToReturn(ReturnMeta *const rm, int pos) {
const bool test = static_cast<unsigned int>(pos) == rm->rfmeta.size();
TEST_TextMessageError(test, "ReturnMeta has badly ordered"
" ReturnFields!");
std::pair<int, ReturnField>
pair(pos, ReturnField(true, "", OLK::invalidOLK(), -1));
rm->rfmeta.insert(pair);
}
static Item *
decrypt_item_layers(const Item &i, const FieldMeta *const fm, onion o,
uint64_t IV) {
assert(!RiboldMYSQL::is_null(i));
const Item *dec = &i;
Item *out_i = NULL;
//we have fieldMeta, but only use part of it. we select the onion via the o in olk we constructed.
const OnionMeta *const om = fm->getOnionMeta(o);
assert(om);
//its easy to use onionmeta, just get layers, and use dectypt() to decrypt the results.
const auto &enc_layers = om->getLayers();
for (auto it = enc_layers.rbegin(); it != enc_layers.rend(); ++it) {
out_i = (*it)->decrypt(*dec, IV);
assert(out_i);
dec = out_i;
LOG(cdb_v) << "dec okay";
}
assert(out_i && out_i != &i);
return out_i;
}
/*
structure of return field.
map<int,returnField>, int is the index of names
returnField, represent a field, if the field is not salt, then fieldCalled is the plaintex name
/*1. store data as column files, and restore data as plaintext insert query
* 2. plaintext insert query should be able to recover directly
* 3. should be able to used exsisting data to reduce the computation overhead(to be implemented)
*/
static
ResType decryptResults(const ResType &dbres, const ReturnMeta &rmeta) {
//num of rows
const unsigned int rows = dbres.rows.size();
//num of names, to be decrypted
const unsigned int cols = dbres.names.size();
std::vector<std::string> dec_names;
#include <stdlib.h>
#include <stdio.h>
#include <string>
#include <map>
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <algorithm>
#include "wrapper/reuse.hh"
#include "wrapper/common.hh"
using std::cout;
using std::cin;
using std::endl;
using std::vector;
using std::string;
using std::to_string;
static std::string embeddedDir="/t/cryt/shadow";
char * globalEsp=NULL;
int num_of_pipe = 4;
//global map, for each client, we have one WrapperState which contains ProxyState.
static std::map<std::string, WrapperState*> clients;
//This connection mimics the behaviour of MySQL-Proxy
Connect *globalConn;
/*for each field, convert the format to FieldMeta_Wrapper*/
for (auto it = dbres.names.begin();it != dbres.names.end(); it++){
const unsigned int index = it - dbres.names.begin();
//fetch rfmeta based on index
const ReturnField &rf = rmeta.rfmeta.at(index);
if (!rf.getIsSalt()) {
//need to return this field
//filed name here is plaintext
dec_names.push_back(rf.fieldCalled());
}
}
const unsigned int real_cols = dec_names.size();
std::vector<std::vector<Item *> > dec_rows(rows);
//real cols depends on plain text names.
for (unsigned int i = 0; i < rows; i++) {
dec_rows[i] = std::vector<Item *>(real_cols);
}
//
unsigned int col_index = 0;
for (unsigned int c = 0; c < cols; c++) {
const ReturnField &rf = rmeta.rfmeta.at(c);
if (rf.getIsSalt()) {
continue;
}
//the key is in fieldMeta
FieldMeta *const fm = rf.getOLK().key;
for (unsigned int r = 0; r < rows; r++) {
//
if (!fm || dbres.rows[r][c]->is_null()) {
dec_rows[r][col_index] = dbres.rows[r][c];
} else {
uint64_t salt = 0;
const int salt_pos = rf.getSaltPosition();
//read salt from remote datab for descrypting.
if (salt_pos >= 0) {
Item_int *const salt_item =
static_cast<Item_int *>(dbres.rows[r][salt_pos]);
assert_s(!salt_item->null_value, "salt item is null");
salt = salt_item->value;
}
//specify fieldMeta, onion, and salt should be able to decrpyt
//peel onion
dec_rows[r][col_index] =
decrypt_item_layers(*dbres.rows[r][c],fm,rf.getOLK().o,salt);
}
}
col_index++;
}
std::vector<enum_field_types> types;
for(auto item:dec_rows[0]){
types.push_back(item->field_type());
}
//resType is used befor and after descrypting.
return ResType(dbres.ok, dbres.affected_rows, dbres.insert_id,
std::move(dec_names),
std::vector<enum_field_types>(types),//different from previous version
std::move(dec_rows));
}
static void init(){
std::string client="192.168.1.1:1234";
......@@ -284,57 +52,52 @@ static void init(){
globalConn = new Connect(ci.server, ci.user, ci.passwd, ci.port);
}
struct batch{
vector<string> field_names;
vector<int> field_types;
vector<int> field_lengths;
};
static
batch get_batch(metadata_files & input,std::vector<FieldMeta_Wrapper> &tfms){
vector<vector<int>> selected_field_types = input.get_selected_field_types();
vector<vector<int>> selected_field_lengths = input.get_selected_field_lengths();
vector<vector<string>> selected_field_names = input.get_selected_field_names();
vector<int> dec_onion_index = input.get_dec_onion_index();
vector<string> has_salt = input.get_has_salt();
static void transform_to_rawMySQLReturnValue(rawMySQLReturnValue & str,ResType & item ){
for(auto row : item.rows){
std::vector<string> temp;
for(auto item : row){
temp.push_back(ItemToString(*item));
}
str.rowValues.push_back(temp);
}
str.fieldTypes = item.types;
}
vector<string> field_names;
vector<int> field_types;
vector<int> field_lengths;
static void construct_insert(rawMySQLReturnValue & str,std::string table,std::vector<string> &res){
std::string head = string("INSERT INTO `")+table+"` VALUES ";
for(auto i=0u;i<tfms.size();i++){
int index = dec_onion_index[i];
string dec_field_name = tfms[i].fields[index];
auto f = find(selected_field_names[i].begin(),selected_field_names[i].end(),dec_field_name);
assert(f!=selected_field_names[i].end());
int j = f - selected_field_names[i].begin();
if(has_salt[i]==string("true")){
field_names.push_back(selected_field_names[i][j]);
field_types.push_back(selected_field_types[i][j]);
field_lengths.push_back(selected_field_lengths[i][j]);
int cnt = 0;
string cur=head;
for(unsigned int i=0u; i<str.rowValues.size();i++){
++cnt;
cur+="(";
for(unsigned int j=0u;j<str.rowValues[i].size();j++){
if(IS_NUM(str.fieldTypes[j])) {
cur+=str.rowValues[i][j]+=",";
field_names.push_back(selected_field_names[i].back());
field_types.push_back(selected_field_types[i].back());
field_lengths.push_back(selected_field_lengths[i].back());
}else{
int len = str.rowValues[i][j].size();
mysql_real_escape_string(globalConn->get_conn(),globalEsp,
str.rowValues[i][j].c_str(),len);
cur+=string("\"")+=string(globalEsp)+="\",";
}
}
cur.back()=')';
cur+=",";
if(cnt == num_of_pipe){
cnt = 0;
cur.back()=';';
res.push_back(cur);
cur=head;
}
assert(1==2);
}
if(cnt!=0){
cur.back()=';';
res.push_back(cur);
}
batch bt;
bt.field_names = field_names;
bt.field_types = field_types;
bt.field_lengths = field_lengths;
return bt;
}
static
std::shared_ptr<ReturnMeta> getReturnMeta(std::vector<FieldMeta*> fms, std::vector<transField> &tfds){
std::shared_ptr<ReturnMeta> getReturnMeta(std::vector<FieldMeta*> fms,
std::vector<FieldMeta_Wrapper> &tfds){
assert(fms.size()==tfds.size());
std::shared_ptr<ReturnMeta> myReturnMeta = std::make_shared<ReturnMeta>();
int pos=0;
......@@ -374,61 +137,57 @@ static void load_string(string filename, vector<string> &res,unsigned long lengt
close(fd);
}
template<class T>
vector<T> flat_vec(vector<vector<T>> &input){
vector<T> res;
for(auto item:input){
for(auto i:item){
res.push_back(i);
static
void
construct_insert(rawMySQLReturnValue & str,std::string table,std::vector<std::string> &res){
std::string head = string("INSERT INTO `")+table+"` VALUES ";
int cnt = 0;
string cur=head;
for(unsigned int i=0u; i<str.rowValues.size();i++){
++cnt;
cur+="(";
for(unsigned int j=0u;j<str.rowValues[i].size();j++){
if(IS_NUM(str.fieldTypes[j])) {
cur+=str.rowValues[i][j]+=",";
}else{
int len = str.rowValues[i][j].size();
mysql_real_escape_string(globalConn->get_conn(),globalEsp,
str.rowValues[i][j].c_str(),len);
cur+=string("\"")+=string(globalEsp)+="\",";
}
}
return res;
cur.back()=')';
cur+=",";
if(cnt == num_of_pipe){
cnt = 0;
cur.back()=';';
res.push_back(cur);
cur=head;
}
}
if(cnt!=0){
cur.back()=';';
res.push_back(cur);
}
}
struct batch{
vector<string> field_names;
vector<int> field_types;
vector<int> field_lengths;
};
static
batch get_batch(metadata_files & input,std::vector<transField> &tfms){
vector<vector<int>> selected_field_types = input.get_selected_field_types();
vector<vector<int>> selected_field_lengths = input.get_selected_field_lengths();
vector<vector<string>> selected_field_names = input.get_selected_field_names();
vector<int> dec_onion_index = input.get_dec_onion_index();
vector<string> has_salt = input.get_has_salt();
vector<string> field_names;
vector<int> field_types;
vector<int> field_lengths;
for(auto i=0u;i<tfms.size();i++){
int index = dec_onion_index[i];
string dec_field_name = tfms[i].fields[index];
auto f = find(selected_field_names[i].begin(),selected_field_names[i].end(),dec_field_name);
assert(f!=selected_field_names[i].end());
int j = f - selected_field_names[i].begin();
if(has_salt[i]==string("true")){
field_names.push_back(selected_field_names[i][j]);
field_types.push_back(selected_field_types[i][j]);
field_lengths.push_back(selected_field_lengths[i][j]);
field_names.push_back(selected_field_names[i].back());
field_types.push_back(selected_field_types[i].back());
field_lengths.push_back(selected_field_lengths[i].back());
}else{
assert(1==2);
template<class T>
vector<T> flat_vec(vector<vector<T>> &input){
vector<T> res;
for(auto item:input){
for(auto i:item){
res.push_back(i);
}
}
batch bt;
bt.field_names = field_names;
bt.field_types = field_types;
bt.field_lengths = field_lengths;
return bt;
return res;
}
#include <algorithm>
static vector<vector<string>> load_table_fields(metadata_files & input,std::vector<transField> &tfms) {
/*load fields in plain string*/
static vector<vector<string>> load_table_fields(metadata_files & input,
std::vector<FieldMeta_Wrapper> &tfms) {
string db = input.get_db();
string table = input.get_table();
vector<vector<string>> res;
......@@ -462,10 +221,10 @@ static vector<vector<string>> load_table_fields(metadata_files & input,std::vect
}
static ResType load_files(std::string db="tdb", std::string table="student"){
std::unique_ptr<SchemaInfo> schema = myLoadSchemaInfo();
std::unique_ptr<SchemaInfo> schema = myLoadSchemaInfo(embeddedDir);
//get all the fields in the tables.
std::vector<FieldMeta*> fms = getFieldMeta(*schema,db,table);
auto res = getTransField(fms);
auto res = FieldMeta_to_Wrapper(fms);
std::vector<enum_field_types> types;//Added
metadata_files res_meta = load_meta(db,table);
std::shared_ptr<ReturnMeta> rm = getReturnMeta(fms,res);
......@@ -475,11 +234,9 @@ static ResType load_files(std::string db="tdb", std::string table="student"){
//load fields in the stored file
vector<vector<string>> res_field = load_table_fields(res_meta,res);
resraw.rowValues = res_field;
// auto field_names = flat_vec(res_meta.selected_field_names);
// auto field_types = flat_vec(res_meta.selected_field_types);
// auto field_lengths = flat_vec(res_meta.selected_field_lengths);
auto bt = get_batch(res_meta,res);
vector<string> field_names = bt.field_names;
vector<int> field_types = bt.field_types;
......
#pragma once
/*1. store data as column files, and restore data as plaintext insert query
* 2. plaintext insert query should be able to recover directly
* 3. should be able to used exsisting data to reduce the computation overhead(to be implemented)
*/
#include <stdlib.h>
#include <stdio.h>
#include <string>
#include <map>
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include "wrapper/reuse.hh"
using std::cout;
using std::cin;
using std::endl;
using std::vector;
using std::string;
using std::to_string;
static std::string embeddedDir="/t/cryt/shadow";
char * globalEsp=NULL;
int num_of_pipe = 4;
//global map, for each client, we have one WrapperState which contains ProxyState.
static std::map<std::string, WrapperState*> clients;
//This connection mimics the behaviour of MySQL-Proxy
Connect *globalConn;
......@@ -32,24 +32,249 @@ void rawMySQLReturnValue::show(){
cout<<endl;
}
/*no default argument allowed???*/
void FieldMeta_Wrapper::show(){
for(auto i=0U;i<fields.size();i++){
//cout<<fields[i]<<" : "<<gmp2[onions[i]]<<"\t";
}
cout<<endl;
if(hasSalt){
cout<<"has salt"<<endl;
}else cout<<"do not have salt"<<endl;
}
/*Item_null *
Item_null *
make_null(const std::string &name){
char *const n = current_thd->strdup(name.c_str());
return new Item_null(n);
}*/
}
/*
std::vector<Item *>
itemNullVector(unsigned int count)
{
std::vector<Item *> out;
for (unsigned int i = 0; i < count; ++i) {
out.push_back(make_null(""));
out.push_back(make_null());
}
return out;
}
ResType MygetResTypeFromLuaTable(bool isNULL,rawMySQLReturnValue *inRow,int in_last_insert_id){
std::vector<std::string> names;
std::vector<enum_field_types> types;
std::vector<std::vector<Item *>> rows;
//return NULL restype
if(isNULL){
return ResType(true,0,0,std::move(names),
std::move(types),std::move(rows));
} else {
for(auto inNames:inRow->fieldNames){
names.push_back(inNames);
}
for(auto inTypes:inRow->fieldTypes){
types.push_back(static_cast<enum_field_types>(inTypes));
}
for(auto inRows:inRow->rowValues) {
std::vector<Item *> curTempRow = itemNullVector(types.size());
for(int i=0;i< (int)(inRows.size());i++){
curTempRow[i] = (MySQLFieldTypeToItem(types[i],inRows[i]) );
}
rows.push_back(curTempRow);
}
return ResType(true,0,in_last_insert_id,std::move(names),std::move(types),std::move(rows));
}
}
void
addToReturn(ReturnMeta *const rm, int pos, const OLK &constr,
bool has_salt, const std::string &name) {
const bool test = static_cast<unsigned int>(pos) == rm->rfmeta.size();
TEST_TextMessageError(test, "ReturnMeta has badly ordered"
" ReturnFields!");
const int salt_pos = has_salt ? pos + 1 : -1;
std::pair<int, ReturnField>
pair(pos, ReturnField(false, name, constr, salt_pos));
rm->rfmeta.insert(pair);
}
void
addSaltToReturn(ReturnMeta *const rm, int pos) {
const bool test = static_cast<unsigned int>(pos) == rm->rfmeta.size();
TEST_TextMessageError(test, "ReturnMeta has badly ordered"
" ReturnFields!");
std::pair<int, ReturnField>
pair(pos, ReturnField(true, "", OLK::invalidOLK(), -1));
rm->rfmeta.insert(pair);
}
std::vector<FieldMeta *> getFieldMeta(SchemaInfo &schema,std::string db, std::string table){
const std::unique_ptr<AES_KEY> &TK = std::unique_ptr<AES_KEY>(getKey(std::string("113341234")));
Analysis analysis(db,schema,TK,
SECURITY_RATING::SENSITIVE);
if(analysis.databaseMetaExists(db)){
const DatabaseMeta & dbm = analysis.getDatabaseMeta(db);
TableMeta & tbm = *dbm.getChild(IdentityMetaKey(table));
return tbm.orderedFieldMetas();
}else{
std::cout<<"data base not exists"<<std::endl;
return std::vector<FieldMeta *>();
}
}
std::unique_ptr<SchemaInfo> myLoadSchemaInfo(std::string embeddedDir) {
std::unique_ptr<Connect> e_conn(Connect::getEmbedded(embeddedDir));
std::unique_ptr<SchemaInfo> schema(new SchemaInfo());
std::function<DBMeta *(DBMeta *const)> loadChildren =
[&loadChildren, &e_conn](DBMeta *const parent) {
auto kids = parent->fetchChildren(e_conn);
for (auto it : kids) {
loadChildren(it);
}
return parent;
};
//load all metadata and then store it in schema
loadChildren(schema.get());
Analysis analysis(std::string("student"),*schema,
std::unique_ptr<AES_KEY>(getKey(std::string("113341234"))),
SECURITY_RATING::SENSITIVE);
return schema;
}
Item *
decrypt_item_layers(const Item &i, const FieldMeta *const fm, onion o,
uint64_t IV) {
assert(!RiboldMYSQL::is_null(i));
const Item *dec = &i;
Item *out_i = NULL;
//we have fieldMeta, but only use part of it. we select the onion via the o in olk we constructed.
const OnionMeta *const om = fm->getOnionMeta(o);
assert(om);
//its easy to use onionmeta, just get layers, and use dectypt() to decrypt the results.
const auto &enc_layers = om->getLayers();
for (auto it = enc_layers.rbegin(); it != enc_layers.rend(); ++it) {
out_i = (*it)->decrypt(*dec, IV);
assert(out_i);
dec = out_i;
LOG(cdb_v) << "dec okay";
}
assert(out_i && out_i != &i);
return out_i;
}
/*
structure of return field.
map<int,returnField>, int is the index of names
returnField, represent a field, if the field is not salt, then fieldCalled is the plaintex name
*/
ResType decryptResults(const ResType &dbres, const ReturnMeta &rmeta) {
//num of rows
const unsigned int rows = dbres.rows.size();
//num of names, to be decrypted
const unsigned int cols = dbres.names.size();
std::vector<std::string> dec_names;
for (auto it = dbres.names.begin();it != dbres.names.end(); it++){
const unsigned int index = it - dbres.names.begin();
//fetch rfmeta based on index
const ReturnField &rf = rmeta.rfmeta.at(index);
if (!rf.getIsSalt()) {
//need to return this field
//filed name here is plaintext
dec_names.push_back(rf.fieldCalled());
}
}
const unsigned int real_cols = dec_names.size();
std::vector<std::vector<Item *> > dec_rows(rows);
//real cols depends on plain text names.
for (unsigned int i = 0; i < rows; i++) {
dec_rows[i] = std::vector<Item *>(real_cols);
}
//
unsigned int col_index = 0;
for (unsigned int c = 0; c < cols; c++) {
const ReturnField &rf = rmeta.rfmeta.at(c);
if (rf.getIsSalt()) {
continue;
}
//the key is in fieldMeta
FieldMeta *const fm = rf.getOLK().key;
for (unsigned int r = 0; r < rows; r++) {
//
if (!fm || dbres.rows[r][c]->is_null()) {
dec_rows[r][col_index] = dbres.rows[r][c];
} else {
uint64_t salt = 0;
const int salt_pos = rf.getSaltPosition();
//read salt from remote datab for descrypting.
if (salt_pos >= 0) {
Item_int *const salt_item =
static_cast<Item_int *>(dbres.rows[r][salt_pos]);
assert_s(!salt_item->null_value, "salt item is null");
salt = salt_item->value;
}
//specify fieldMeta, onion, and salt should be able to decrpyt
//peel onion
dec_rows[r][col_index] =
decrypt_item_layers(*dbres.rows[r][c],fm,rf.getOLK().o,salt);
}
}
col_index++;
}
std::vector<enum_field_types> types;
for(auto item:dec_rows[0]){
types.push_back(item->field_type());
}
//resType is used befor and after descrypting.
return ResType(dbres.ok, dbres.affected_rows, dbres.insert_id,
std::move(dec_names),
std::vector<enum_field_types>(types),//different from previous version
std::move(dec_rows));
}
std::vector<FieldMeta_Wrapper> FieldMeta_to_Wrapper(std::vector<FieldMeta *> pfms){
std::vector<FieldMeta_Wrapper> res;
//for every field
for(auto pfm:pfms){
FieldMeta_Wrapper tf;
tf.originalFm = pfm;
for(std::pair<const OnionMetaKey *, OnionMeta *> &ompair:pfm->orderedOnionMetas()){
tf.numOfOnions++;
tf.fields.push_back((ompair.second)->getAnonOnionName());
tf.onions.push_back(ompair.first->getValue());
tf.originalOm.push_back(ompair.second);
}
if(pfm->getHasSalt()){
tf.hasSalt=true;
tf.fields.push_back(pfm->getSaltName());
}
res.push_back(tf);
}
return res;
}
void transform_to_rawMySQLReturnValue(rawMySQLReturnValue & str,ResType & item ){
for(auto row : item.rows){
std::vector<std::string> temp;
for(auto item : row){
temp.push_back(ItemToString(*item));
}
str.rowValues.push_back(temp);
}
str.fieldTypes = item.types;
}
......@@ -40,12 +40,48 @@ struct rawMySQLReturnValue {
void show();
};
//Item_null*
//make_null(const std::string &name);
//representation of one field.
struct FieldMeta_Wrapper{
bool hasSalt;
FieldMeta *originalFm;
std::vector<int> choosenOnions;
//used to construct return meta
int onionIndex = 0;
int numOfOnions=0;
//onions
std::vector<std::string> fields;
std::vector<onion> onions;
std::vector<OnionMeta*>originalOm;
void show();
};
/*Functions*/
Item_null*
make_null(const std::string &name="");
std::vector<Item *>
itemNullVector(unsigned int count);
ResType MygetResTypeFromLuaTable(bool isNULL,rawMySQLReturnValue *inRow = NULL,int in_last_insert_id = 0);
void
addToReturn(ReturnMeta *const rm, int pos, const OLK &constr, bool has_salt, const std::string &name);
void
addSaltToReturn(ReturnMeta *const rm, int pos);
std::vector<FieldMeta *> getFieldMeta(SchemaInfo &schema,
std::string db = "tdb",
std::string table="student1");
std::unique_ptr<SchemaInfo> myLoadSchemaInfo(std::string embeddedDir="shadow");
Item *
decrypt_item_layers(const Item &i, const FieldMeta *const fm, onion o,
uint64_t IV);
//std::vector<Item *>
//itemNullVector(unsigned int count);
ResType decryptResults(const ResType &dbres, const ReturnMeta &rmeta);
//ResType MygetResTypeFromLuaTable(bool isNULL,rawMySQLReturnValue *inRow = NULL,int in_last_insert_id = 0);
std::vector<FieldMeta_Wrapper> FieldMeta_to_Wrapper(std::vector<FieldMeta *> pfms);
void transform_to_rawMySQLReturnValue(rawMySQLReturnValue & str,ResType & item );
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment