Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
P
Practical-Cryptdb
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Zhaozhen
Practical-Cryptdb
Commits
3f7f047b
Commit
3f7f047b
authored
Jan 25, 2018
by
yiwenshao
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
able to decrypt and reencrypt variable length string,haha
parent
51ff0337
Show whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
636 additions
and
18 deletions
+636
-18
final_load.cc
debug/final_load.cc
+299
-0
final_load.cc.back
debug/final_load.cc.back
+298
-0
final_store.cc
debug/final_store.cc
+13
-10
Makefrag
test_wrapper/Makefrag
+1
-1
main.cc
test_wrapper/main.cc
+21
-0
reuse.cc
wrapper/reuse.cc
+2
-2
reuse.hh
wrapper/reuse.hh
+2
-5
No files found.
debug/final_load.cc
0 → 100644
View file @
3f7f047b
/*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 <algorithm>
#include "wrapper/reuse.hh"
#include "wrapper/common.hh"
#include "wrapper/insert_lib.hh"
#include "util/constants.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*/
static
void
init
(){
std
::
string
client
=
"192.168.1.1:1234"
;
//one Wrapper per user.
clients
[
client
]
=
new
WrapperState
();
//Connect phase
ConnectionInfo
ci
(
"localhost"
,
"root"
,
"letmein"
,
3306
);
const
std
::
string
master_key
=
"113341234"
;
char
*
buffer
;
if
((
buffer
=
getcwd
(
NULL
,
0
))
==
NULL
){
perror
(
"getcwd error"
);
}
embeddedDir
=
std
::
string
(
buffer
)
+
"/shadow"
;
SharedProxyState
*
shared_ps
=
new
SharedProxyState
(
ci
,
embeddedDir
,
master_key
,
determineSecurityRating
());
assert
(
0
==
mysql_thread_init
());
//we init embedded database here.
clients
[
client
]
->
ps
=
std
::
unique_ptr
<
ProxyState
>
(
new
ProxyState
(
*
shared_ps
));
clients
[
client
]
->
ps
->
safeCreateEmbeddedTHD
();
//Connect end!!
globalConn
=
new
Connect
(
ci
.
server
,
ci
.
user
,
ci
.
passwd
,
ci
.
port
);
}
//========================================================================================//
fullBackUp
gfb
;
struct
batch
{
vector
<
string
>
field_names
;
vector
<
int
>
field_types
;
vector
<
int
>
field_lengths
;
};
batch
ggbt
;
/*should choose the right decryption onion*/
static
std
::
shared_ptr
<
ReturnMeta
>
getReturnMeta
(
std
::
vector
<
FieldMeta
*>
fms
,
std
::
vector
<
FieldMetaTrans
>
&
tfds
){
assert
(
fms
.
size
()
==
tfds
.
size
());
std
::
shared_ptr
<
ReturnMeta
>
myReturnMeta
=
std
::
make_shared
<
ReturnMeta
>
();
int
pos
=
0
;
//construct OLK
for
(
auto
i
=
0u
;
i
<
tfds
.
size
();
i
++
){
//the order is DET,OPE,ASHE,AGG. other onions are not decryptable!!
int
index
=
getDecryptionOnionIndex
(
tfds
[
i
]);
if
(
index
==-
1
)
assert
(
0
);
onion
o
=
tfds
[
i
].
getChoosenOnionO
()[
index
];
SECLEVEL
l
=
tfds
[
i
].
getOriginalFieldMeta
()
->
getOnionMeta
(
o
)
->
getSecLevel
();
FieldMeta
*
k
=
tfds
[
i
].
getOriginalFieldMeta
();
OLK
curOLK
(
o
,
l
,
k
);
bool
use_salt
=
false
;
if
(
needsSalt
(
curOLK
))
use_salt
=
true
;
addToReturn
(
myReturnMeta
.
get
(),
pos
++
,
curOLK
,
use_salt
,
k
->
getFieldName
());
if
(
use_salt
)
addSaltToReturn
(
myReturnMeta
.
get
(),
pos
++
);
ggbt
.
field_types
.
push_back
(
tfds
[
i
].
getChoosenFieldTypes
()[
index
]);
ggbt
.
field_names
.
push_back
(
tfds
[
i
].
getChoosenOnionName
()[
index
]);
ggbt
.
field_lengths
.
push_back
(
tfds
[
i
].
getChoosenFieldLengths
()[
index
]);
if
(
use_salt
){
ggbt
.
field_types
.
push_back
(
tfds
[
i
].
getSaltType
());
ggbt
.
field_names
.
push_back
(
tfds
[
i
].
getSaltName
());
ggbt
.
field_lengths
.
push_back
(
tfds
[
i
].
getSaltLength
());
}
}
return
myReturnMeta
;
}
/*init global full backup. */
static
void
initGfb
(
std
::
vector
<
FieldMetaTrans
>
&
res
,
std
::
string
db
,
std
::
string
table
){
vector
<
string
>
field_names
;
vector
<
int
>
field_types
;
vector
<
int
>
field_lengths
;
/*choosen onions should all be included in gfb. salt is also included
it's hard to decide whether a FieldMetaTrans has salt because the senmantic is different from that of FieldMeta.
*/
for
(
auto
&
item
:
res
){
for
(
auto
i
:
item
.
getChoosenOnionName
()){
field_names
.
push_back
(
i
);
}
for
(
auto
i
:
item
.
getChoosenFieldTypes
()){
field_types
.
push_back
(
i
);
}
for
(
auto
i
:
item
.
getChoosenFieldLengths
()){
field_lengths
.
push_back
(
i
);
}
if
(
item
.
getHasSalt
()){
field_names
.
push_back
(
item
.
getSaltName
());
field_types
.
push_back
(
item
.
getSaltType
());
field_lengths
.
push_back
(
item
.
getSaltLength
());
}
}
gfb
.
field_names
=
field_names
;
gfb
.
field_types
=
field_types
;
gfb
.
field_lengths
=
field_lengths
;
//then we should read the vector
std
::
string
prefix
=
std
::
string
(
"data/"
)
+
db
+
"/"
+
table
+
"/"
;
for
(
unsigned
int
i
=
0u
;
i
<
gfb
.
field_names
.
size
();
i
++
)
{
std
::
string
filename
=
prefix
+
gfb
.
field_names
[
i
];
std
::
vector
<
std
::
string
>
column
;
if
(
IS_NUM
(
gfb
.
field_types
[
i
])){
// load_num_file_count(filename,column,constGlobalConstants.loadCount);
loadFileNoEscape
(
filename
,
column
);
}
else
{
// load_string_file_count(filename,column,gfb.field_lengths[i],constGlobalConstants.loadCount);
loadFileEscape
(
filename
,
column
,
gfb
.
field_lengths
[
i
]);
}
gfb
.
annoOnionNameToFileVector
[
gfb
.
field_names
[
i
]]
=
std
::
move
(
column
);
}
//init another map
for
(
unsigned
int
i
=
0
;
i
<
gfb
.
field_names
.
size
();
i
++
){
gfb
.
annoOnionNameToType
[
gfb
.
field_names
[
i
]]
=
gfb
.
field_types
[
i
];
}
}
/*load file, decrypt, and then return data plain fields in the type ResType*/
static
ResType
load_files
(
std
::
string
db
,
std
::
string
table
){
std
::
unique_ptr
<
SchemaInfo
>
schema
=
myLoadSchemaInfo
(
embeddedDir
);
//get all the fields in the tables.
std
::
vector
<
FieldMeta
*>
fms
=
getFieldMeta
(
*
schema
,
db
,
table
);
TableMetaTrans
res_meta
=
loadTableMetaTrans
(
db
,
table
);
std
::
vector
<
FieldMetaTrans
>
res
=
res_meta
.
getFts
();
for
(
unsigned
int
i
=
0
;
i
<
fms
.
size
();
i
++
){
res
[
i
].
trans
(
fms
[
i
]);
}
//then we should load all the fields available
initGfb
(
res
,
db
,
table
);
std
::
shared_ptr
<
ReturnMeta
>
rm
=
getReturnMeta
(
fms
,
res
);
vector
<
string
>
field_names
=
ggbt
.
field_names
;
vector
<
int
>
field_types
=
ggbt
.
field_types
;
vector
<
int
>
field_lengths
=
ggbt
.
field_lengths
;
//why do we need this??
create_embedded_thd
(
0
);
rawMySQLReturnValue
resraw
;
vector
<
vector
<
string
>>
res_field
;
for
(
auto
item
:
field_names
){
res_field
.
push_back
(
gfb
.
annoOnionNameToFileVector
[
item
]);
}
//then transform it to ress_fields
unsigned
int
length
=
res_field
[
0
].
size
();
vector
<
vector
<
string
>>
ress_field
;
for
(
unsigned
int
i
=
0u
;
i
<
length
;
i
++
){
vector
<
string
>
row
;
for
(
unsigned
int
j
=
0u
;
j
<
res_field
.
size
();
j
++
){
row
.
push_back
(
res_field
[
j
][
i
]);
}
ress_field
.
push_back
(
row
);
}
resraw
.
rowValues
=
ress_field
;
resraw
.
fieldNames
=
field_names
;
for
(
unsigned
int
i
=
0
;
i
<
field_types
.
size
();
++
i
){
resraw
.
fieldTypes
.
push_back
(
static_cast
<
enum_field_types
>
(
field_types
[
i
]));
}
ResType
rawtorestype
=
rawMySQLReturnValue_to_ResType
(
false
,
&
resraw
);
auto
finalresults
=
decryptResults
(
rawtorestype
,
*
rm
);
return
finalresults
;
}
static
void
local_wrapper
(
const
Item
&
i
,
const
FieldMeta
&
fm
,
Analysis
&
a
,
List
<
Item
>
*
const
append_list
)
{
//append_list->push_back(&(const_cast<Item&>(i)));
//do not use the plain strategy
std
::
vector
<
Item
*>
l
;
const
uint64_t
salt
=
fm
.
getHasSalt
()
?
randomValue
()
:
0
;
uint64_t
IV
=
salt
;
for
(
auto
it
:
fm
.
orderedOnionMetas
())
{
const
onion
o
=
it
.
first
->
getValue
();
OnionMeta
*
const
om
=
it
.
second
;
std
::
string
annoOnionName
=
om
->
getAnonOnionName
();
if
(
gfb
.
annoOnionNameToFileVector
.
find
(
annoOnionName
)
!=
gfb
.
annoOnionNameToFileVector
.
end
()){
enum_field_types
type
=
static_cast
<
enum_field_types
>
(
gfb
.
annoOnionNameToType
[
annoOnionName
]);
std
::
vector
<
std
::
string
>
&
tempFileVector
=
gfb
.
annoOnionNameToFileVector
[
annoOnionName
];
std
::
string
in
=
tempFileVector
.
back
();
if
(
IS_NUM
(
type
)){
//std::string in("11");
unsigned
int
len
=
annoOnionName
.
size
();
if
(
len
>
4u
&&
annoOnionName
.
substr
(
len
-
4
)
==
"ASHE"
){
l
.
push_back
(
MySQLFieldTypeToItem
(
type
,
in
));
}
else
{
l
.
push_back
(
new
(
current_thd
->
mem_root
)
Item_int
(
static_cast
<
ulonglong
>
(
valFromStr
(
in
)))
);
}
}
else
{
//std::string in("hehe");
l
.
push_back
(
MySQLFieldTypeToItem
(
type
,
in
));
}
tempFileVector
.
pop_back
();
//l.push_back(&(const_cast<Item&>(i)));
}
else
{
l
.
push_back
(
my_encrypt_item_layers
(
i
,
o
,
*
om
,
a
,
IV
));
}
}
std
::
string
saltName
=
fm
.
getSaltName
();
if
(
fm
.
getHasSalt
())
{
if
(
gfb
.
annoOnionNameToFileVector
.
find
(
saltName
)
!=
gfb
.
annoOnionNameToFileVector
.
end
()){
std
::
vector
<
std
::
string
>
&
tempFileVector
=
gfb
.
annoOnionNameToFileVector
[
saltName
];
std
::
string
in
=
tempFileVector
.
back
();
// enum_field_types type = static_cast<enum_field_types>(gfb.annoOnionNameToType[saltName]);
// l.push_back(MySQLFieldTypeToItem(type,in));
l
.
push_back
(
new
(
current_thd
->
mem_root
)
Item_int
(
static_cast
<
ulonglong
>
(
valFromStr
(
in
)))
);
tempFileVector
.
pop_back
();
}
else
{
l
.
push_back
(
new
Item_int
(
static_cast
<
ulonglong
>
(
salt
)));
}
}
for
(
auto
it
:
l
)
{
append_list
->
push_back
(
it
);
}
}
int
main
(
int
argc
,
char
*
argv
[]){
init
();
create_embedded_thd
(
0
);
std
::
string
db
=
"tdb"
,
table
=
"student"
;
std
::
string
ip
=
"localhost"
;
if
(
argc
==
4
){
ip
=
std
::
string
(
argv
[
1
]);
db
=
std
::
string
(
argv
[
2
]);
table
=
std
::
string
(
argv
[
3
]);
}
std
::
unique_ptr
<
SchemaInfo
>
schema
=
myLoadSchemaInfo
(
embeddedDir
);
schema
.
get
();
const
std
::
unique_ptr
<
AES_KEY
>
&
TK
=
std
::
unique_ptr
<
AES_KEY
>
(
getKey
(
std
::
string
(
"113341234"
)));
Analysis
analysis
(
db
,
*
schema
,
TK
,
SECURITY_RATING
::
SENSITIVE
);
/*choose decryption onion, load and decrypt to plain text*/
ResType
res
=
load_files
(
db
,
table
);
std
::
string
annoTableName
=
analysis
.
getTableMeta
(
db
,
table
).
getAnonTableName
();
const
std
::
string
head
=
std
::
string
(
"INSERT INTO `"
)
+
db
+
"`.`"
+
annoTableName
+
"` "
;
/*reencryption to get the encrypted insert!!!*/
for
(
auto
&
row
:
res
.
rows
)
{
List
<
List_item
>
newList
;
List
<
Item
>
*
const
newList0
=
new
List
<
Item
>
();
for
(
auto
i
=
0u
;
i
<
res
.
names
.
size
();
i
++
){
std
::
string
field_name
=
res
.
names
[
i
];
FieldMeta
&
fm
=
analysis
.
getFieldMeta
(
db
,
table
,
field_name
);
local_wrapper
(
*
row
[
i
],
fm
,
analysis
,
newList0
);
}
newList
.
push_back
(
newList0
);
std
::
ostringstream
o
;
insertManyValues
(
o
,
newList
);
std
::
cout
<<
(
head
+
o
.
str
())
<<
std
::
endl
;
}
return
0
;
}
debug/final_load.cc.back
0 → 100644
View file @
3f7f047b
/*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 <algorithm>
#include "wrapper/reuse.hh"
#include "wrapper/common.hh"
#include "wrapper/insert_lib.hh"
#include "util/constants.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*/
static void init(){
std::string client="192.168.1.1:1234";
//one Wrapper per user.
clients[client] = new WrapperState();
//Connect phase
ConnectionInfo ci("localhost", "root", "letmein",3306);
const std::string master_key = "113341234";
char *buffer;
if((buffer = getcwd(NULL, 0)) == NULL){
perror("getcwd error");
}
embeddedDir = std::string(buffer)+"/shadow";
SharedProxyState *shared_ps =
new SharedProxyState(ci, embeddedDir , master_key,
determineSecurityRating());
assert(0 == mysql_thread_init());
//we init embedded database here.
clients[client]->ps = std::unique_ptr<ProxyState>(new ProxyState(*shared_ps));
clients[client]->ps->safeCreateEmbeddedTHD();
//Connect end!!
globalConn = new Connect(ci.server, ci.user, ci.passwd, ci.port);
}
//========================================================================================//
fullBackUp gfb;
struct batch{
vector<string> field_names;
vector<int> field_types;
vector<int> field_lengths;
};
batch ggbt;
/*should choose the right decryption onion*/
static
std::shared_ptr<ReturnMeta> getReturnMeta(std::vector<FieldMeta*> fms,
std::vector<FieldMetaTrans> &tfds){
assert(fms.size()==tfds.size());
std::shared_ptr<ReturnMeta> myReturnMeta = std::make_shared<ReturnMeta>();
int pos=0;
//construct OLK
for(auto i=0u;i<tfds.size();i++){
//the order is DET,OPE,ASHE,AGG. other onions are not decryptable!!
int index = getDecryptionOnionIndex(tfds[i]);
if(index==-1) assert(0);
onion o = tfds[i].getChoosenOnionO()[index];
SECLEVEL l = tfds[i].getOriginalFieldMeta()->getOnionMeta(o)->getSecLevel();
FieldMeta *k = tfds[i].getOriginalFieldMeta();
OLK curOLK(o,l,k);
bool use_salt = false;
if(needsSalt(curOLK))
use_salt = true;
addToReturn(myReturnMeta.get(),pos++,curOLK,use_salt,k->getFieldName());
if(use_salt)
addSaltToReturn(myReturnMeta.get(),pos++);
ggbt.field_types.push_back(tfds[i].getChoosenFieldTypes()[index]);
ggbt.field_names.push_back(tfds[i].getChoosenOnionName()[index]);
ggbt.field_lengths.push_back(tfds[i].getChoosenFieldLengths()[index]);
if(use_salt){
ggbt.field_types.push_back(tfds[i].getSaltType());
ggbt.field_names.push_back(tfds[i].getSaltName());
ggbt.field_lengths.push_back(tfds[i].getSaltLength());
}
}
return myReturnMeta;
}
/*init global full backup. */
static
void initGfb(std::vector<FieldMetaTrans> &res,std::string db,std::string table){
vector<string> field_names;
vector<int> field_types;
vector<int> field_lengths;
/*choosen onions should all be included in gfb. salt is also included
it's hard to decide whether a FieldMetaTrans has salt because the senmantic is different from that of FieldMeta.
*/
for(auto &item:res){
for(auto i:item.getChoosenOnionName()){
field_names.push_back(i);
}
for(auto i:item.getChoosenFieldTypes()){
field_types.push_back(i);
}
for(auto i:item.getChoosenFieldLengths()){
field_lengths.push_back(i);
}
if(item.getHasSalt()){
field_names.push_back(item.getSaltName());
field_types.push_back(item.getSaltType());
field_lengths.push_back(item.getSaltLength());
}
}
gfb.field_names = field_names;
gfb.field_types = field_types;
gfb.field_lengths = field_lengths;
//then we should read the vector
std::string prefix = std::string("data/")+db+"/"+table+"/";
for(unsigned int i=0u; i<gfb.field_names.size(); i++) {
std::string filename = prefix + gfb.field_names[i];
std::vector<std::string> column;
if(IS_NUM(gfb.field_types[i])){
load_num_file_count(filename,column,constGlobalConstants.loadCount);
}else{
load_string_file_count(filename,column,gfb.field_lengths[i],constGlobalConstants.loadCount);
}
gfb.annoOnionNameToFileVector[gfb.field_names[i]] = std::move(column);
}
//init another map
for(unsigned int i=0;i<gfb.field_names.size();i++){
gfb.annoOnionNameToType[gfb.field_names[i]] = gfb.field_types[i];
}
}
/*load file, decrypt, and then return data plain fields in the type ResType*/
static ResType load_files(std::string db, std::string table){
std::unique_ptr<SchemaInfo> schema = myLoadSchemaInfo(embeddedDir);
//get all the fields in the tables.
std::vector<FieldMeta*> fms = getFieldMeta(*schema,db,table);
TableMetaTrans res_meta = loadTableMetaTrans(db,table);
std::vector<FieldMetaTrans> res = res_meta.getFts();
for(unsigned int i=0;i<fms.size();i++){
res[i].trans(fms[i]);
}
//then we should load all the fields available
initGfb(res,db,table);
std::shared_ptr<ReturnMeta> rm = getReturnMeta(fms,res);
vector<string> field_names = ggbt.field_names;
vector<int> field_types = ggbt.field_types;
vector<int> field_lengths = ggbt.field_lengths;
//why do we need this??
create_embedded_thd(0);
rawMySQLReturnValue resraw;
// vector<vector<string>> resss_field = loadTableFieldsForDecryption(db,
// table,field_names, field_types, field_lengths);
vector<vector<string>> res_field;
for(auto item:field_names){
res_field.push_back(gfb.annoOnionNameToFileVector[item]);
}
//then transform it to ress_fields
unsigned int length = res_field[0].size();
vector<vector<string>> ress_field;
for(unsigned int i=0u;i<length;i++){
vector<string> row;
for(unsigned int j=0u;j<res_field.size();j++){
row.push_back(res_field[j][i]);
}
ress_field.push_back(row);
}
resraw.rowValues = ress_field;
resraw.fieldNames = field_names;
for(unsigned int i=0;i<field_types.size();++i){
resraw.fieldTypes.push_back(static_cast<enum_field_types>(field_types[i]));
}
ResType rawtorestype = rawMySQLReturnValue_to_ResType(false, &resraw);
auto finalresults = decryptResults(rawtorestype,*rm);
return finalresults;
}
static
void local_wrapper(const Item &i, const FieldMeta &fm, Analysis &a,
List<Item> *const append_list) {
//append_list->push_back(&(const_cast<Item&>(i)));
//do not use the plain strategy
std::vector<Item *> l;
const uint64_t salt = fm.getHasSalt() ? randomValue() : 0;
uint64_t IV = salt;
for (auto it : fm.orderedOnionMetas()) {
const onion o = it.first->getValue();
OnionMeta * const om = it.second;
std::string annoOnionName = om->getAnonOnionName();
if(gfb.annoOnionNameToFileVector.find(annoOnionName)!=gfb.annoOnionNameToFileVector.end()){
enum_field_types type = static_cast<enum_field_types>(gfb.annoOnionNameToType[annoOnionName]);
std::vector<std::string> &tempFileVector = gfb.annoOnionNameToFileVector[annoOnionName];
std::string in = tempFileVector.back();
if(IS_NUM(type)){
//std::string in("11");
unsigned int len = annoOnionName.size();
if(len>4u&&annoOnionName.substr(len-4)=="ASHE"){
l.push_back(MySQLFieldTypeToItem(type,in));
}else{
l.push_back( new (current_thd->mem_root)
Item_int(static_cast<ulonglong>(valFromStr(in))) );
}
}else{
//std::string in("hehe");
l.push_back(MySQLFieldTypeToItem(type,in));
}
tempFileVector.pop_back();
//l.push_back(&(const_cast<Item&>(i)));
}else{
l.push_back(my_encrypt_item_layers(i, o, *om, a, IV));
}
}
std::string saltName = fm.getSaltName();
if (fm.getHasSalt()) {
if(gfb.annoOnionNameToFileVector.find(saltName)!=gfb.annoOnionNameToFileVector.end()){
std::vector<std::string> &tempFileVector = gfb.annoOnionNameToFileVector[saltName];
std::string in = tempFileVector.back();
// enum_field_types type = static_cast<enum_field_types>(gfb.annoOnionNameToType[saltName]);
// l.push_back(MySQLFieldTypeToItem(type,in));
l.push_back( new (current_thd->mem_root)
Item_int(static_cast<ulonglong>(valFromStr(in)))
);
tempFileVector.pop_back();
}else{
l.push_back(new Item_int(static_cast<ulonglong>(salt)));
}
}
for (auto it : l) {
append_list->push_back(it);
}
}
int
main(int argc, char* argv[]){
init();
create_embedded_thd(0);
std::string db="tdb",table="student";
std::string ip="localhost";
if(argc==4){
ip = std::string(argv[1]);
db = std::string(argv[2]);
table = std::string(argv[3]);
}
std::unique_ptr<SchemaInfo> schema = myLoadSchemaInfo(embeddedDir);
schema.get();
const std::unique_ptr<AES_KEY> &TK = std::unique_ptr<AES_KEY>(getKey(std::string("113341234")));
Analysis analysis(db, *schema, TK, SECURITY_RATING::SENSITIVE);
/*choose decryption onion, load and decrypt to plain text*/
ResType res = load_files(db,table);
std::string annoTableName = analysis.getTableMeta(db,table).getAnonTableName();
const std::string head = std::string("INSERT INTO `")+db+"`.`"+annoTableName+"` ";
/*reencryption to get the encrypted insert!!!*/
for(auto &row:res.rows) {
List<List_item> newList;
List<Item> *const newList0 = new List<Item>();
for(auto i=0u;i<res.names.size();i++){
std::string field_name = res.names[i];
FieldMeta & fm = analysis.getFieldMeta(db,table,field_name);
local_wrapper(*row[i],fm,analysis,newList0);
}
newList.push_back(newList0);
std::ostringstream o;
insertManyValues(o,newList);
std::cout<<(head+o.str())<<std::endl;
}
return 0;
}
debug/final_store.cc
View file @
3f7f047b
...
...
@@ -57,29 +57,32 @@ std::string getTestQuery(SchemaInfo &schema, std::vector<FieldMetaTrans> &tfds,
return
res
;
}
static
void
write_meta
(
rawMySQLReturnValue
&
resraw
,
std
::
vector
<
FieldMetaTrans
>
&
res
,
string
db
,
string
table
){
static
void
write_meta
(
std
::
vector
<
FieldMetaTrans
>
&
res
,
string
db
,
string
table
){
TableMetaTrans
mf
(
db
,
table
,
res
);
mf
.
set_db_table
(
db
,
table
);
mf
.
serialize
();
}
static
void
write_raw_data_to_files
(
rawMySQLReturnValue
&
resraw
,
std
::
vector
<
FieldMetaTrans
>
&
res
,
string
db
,
string
table
){
void
write_raw_data_to_files
(
MySQLColumnData
&
resraw
,
std
::
vector
<
FieldMetaTrans
>
&
res
,
string
db
,
string
table
){
//write metafiles
write_meta
(
res
raw
,
res
,
db
,
table
);
write_meta
(
res
,
db
,
table
);
//write datafiles
write_row_data
(
resraw
,
db
,
table
);
std
::
string
prefix
=
std
::
string
(
"data/"
)
+
db
+
"/"
+
table
+
"/"
;
std
::
vector
<
std
::
string
>
filenames
;
for
(
auto
item
:
resraw
.
fieldNames
){
item
=
prefix
+
item
;
filenames
.
push_back
(
item
);
}
int
len
=
resraw
.
fieldNames
.
size
();
for
(
int
i
=
0
;
i
<
len
;
i
++
){
if
(
IS_NUM
(
resraw
.
fieldTypes
[
i
])){
writeColumndataNum
(
resraw
.
columnData
[
i
],
filenames
[
i
]);
}
else
{
writeColumndataEscapeString
(
resraw
.
columnData
[
i
],
filenames
[
i
],
resraw
.
maxLengths
[
i
]);
}
}
}
static
void
store
(
std
::
string
db
,
std
::
string
table
){
...
...
@@ -101,11 +104,11 @@ static void store(std::string db, std::string table){
//generate the backup query and then fetch the tuples
std
::
string
backup_query
=
getTestQuery
(
*
schema
,
res
,
db
,
table
);
rawMySQLReturnValue
resraw
=
executeAndGetResultRemote
(
globalConn
,
backup_query
);
MySQLColumnData
resraw
=
executeAndGetColumnData
(
globalConn
,
backup_query
);
//then we should set the type and length of FieldMetaTrans
auto
types
=
resraw
.
fieldTypes
;
auto
lengths
=
resraw
.
l
engths
;
auto
lengths
=
resraw
.
maxL
engths
;
int
base_types
=
0
;
int
base_lengths
=
0
;
for
(
auto
&
item
:
res
){
...
...
test_wrapper/Makefrag
View file @
3f7f047b
...
...
@@ -2,6 +2,6 @@ OBJDIRS += test_wrapper
##note that xx=*.cc will not expand. wildcard *.cc will include files from other directories.
##%.o will include testall
TESTALL_OBJS := $(patsubst %.cc,$(OBJDIR)/%.o,$(wildcard test_wrapper/*.cc))
TESTALL_EXES := $(patsubst test_wrapper/%.cc,test_wrapper_exe/%,$(wildcard test_wrapper/*.cc))
TESTALL_EXES := $(patsubst test_wrapper/%.cc,
mtl/
test_wrapper_exe/%,$(wildcard test_wrapper/*.cc))
all: $(TESTALL_OBJS) $(TESTALL_EXES)
test_wrapper/main.cc
View file @
3f7f047b
#include "wrapper/reuse.hh"
#include <string>
#include <iostream>
#include <vector>
using
std
::
string
;
using
std
::
vector
;
int
main
()
{
vector
<
string
>
inputstr
{
string
(
"a
\n\n\0
"
,
4
),
"b"
,
"c"
};
writeColumndataEscapeString
(
inputstr
,
"datastr"
,
10
);
vector
<
string
>
resstr
;
loadFileEscape
(
"datastr"
,
resstr
,
10
);
vector
<
string
>
inputint
{
"123"
,
"234"
,
"345"
,
"456"
,
"567"
,
"678"
};
writeColumndataNum
(
inputint
,
"dataint"
);
vector
<
string
>
resint
;
loadFileNoEscape
(
"dataint"
,
resint
);
return
0
;
}
wrapper/reuse.cc
View file @
3f7f047b
...
...
@@ -477,7 +477,7 @@ write_row_data(rawMySQLReturnValue& resraw,std::string db,std::string table,std:
/* Write a column of data of the type string in mysql. one line per record.
string should be escaped before being written into the file */
void
write
Row
dataEscapeString
(
const
std
::
vector
<
std
::
string
>
&
column
,
write
Column
dataEscapeString
(
const
std
::
vector
<
std
::
string
>
&
column
,
std
::
string
columnFilename
,
unsigned
int
maxLength
)
{
FILE
*
dataFileHandler
=
fopen
(
columnFilename
.
c_str
(),
"w"
);
...
...
@@ -496,7 +496,7 @@ writeRowdataEscapeString(const std::vector<std::string> &column,
one record per line
*/
void
write
Row
dataNum
(
const
std
::
vector
<
std
::
string
>
&
column
,
write
Column
dataNum
(
const
std
::
vector
<
std
::
string
>
&
column
,
std
::
string
columnFilename
)
{
FILE
*
dataFileHandler
=
fopen
(
columnFilename
.
c_str
(),
"w"
);
const
std
::
string
token
=
"
\n
"
;
...
...
wrapper/reuse.hh
View file @
3f7f047b
...
...
@@ -231,13 +231,13 @@ loadFileNoEscape(std::string filename,
void
write
Row
dataEscapeString
(
const
std
::
vector
<
std
::
string
>
&
column
,
write
Column
dataEscapeString
(
const
std
::
vector
<
std
::
string
>
&
column
,
std
::
string
columnFilename
,
unsigned
int
maxLength
);
void
write
Row
dataNum
(
const
std
::
vector
<
std
::
string
>
&
column
,
write
Column
dataNum
(
const
std
::
vector
<
std
::
string
>
&
column
,
std
::
string
columnFilename
);
...
...
@@ -249,6 +249,3 @@ void
loadFileNoEscapeLimitCount
(
std
::
string
filename
,
std
::
vector
<
std
::
string
>
&
res
,
int
limit
);
//Connect * initEmbeddedAndRemoteConnection(std::string ip,int port);
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment