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
a6614fec
Commit
a6614fec
authored
Jan 17, 2018
by
yiwenshao
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
special bug in makefile
parent
0966083b
Show whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
449 additions
and
411 deletions
+449
-411
.load.cc.swp
debug/.load.cc.swp
+0
-0
load.cc
debug/load.cc
+333
-3
load.hh
debug/load.hh
+2
-407
Makefrag
wrapper/Makefrag
+1
-1
common.hh
wrapper/common.hh
+7
-0
reuse.cc
wrapper/reuse.cc
+55
-0
reuse.hh
wrapper/reuse.hh
+51
-0
No files found.
debug/.load.cc.swp
0 → 100644
View file @
a6614fec
File added
debug/load.cc
View file @
a6614fec
#include "debug/load.hh"
#include "wrapper/common.hh"
//get returnMeta
//for each filed, we have a fieldmeta. we can chosse one onion under that field to construct a return meta.
//in fact, a returnmeta can contain many fields.
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
*/
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
;
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"
;
//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
);
}
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
;
}
static
void
construct_insert
(
rawMySQLReturnValue
&
str
,
std
::
string
table
,
std
::
vector
<
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
)
+=
"
\"
,"
;
}
}
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
);
}
}
static
std
::
shared_ptr
<
ReturnMeta
>
getReturnMeta
(
std
::
vector
<
FieldMeta
*>
fms
,
std
::
vector
<
transField
>
&
tfds
){
assert
(
fms
.
size
()
==
tfds
.
size
());
...
...
debug/load.hh
View file @
a6614fec
...
...
@@ -12,8 +12,8 @@
#include <fstream>
#include <sstream>
#include <vector>
#include
<main/rewrite_main.hh>
#include <main/rewrite_util.hh>
#include
"wrapper/reuse.hh"
using
std
::
cout
;
using
std
::
cin
;
using
std
::
endl
;
...
...
@@ -21,39 +21,6 @@ using std::vector;
using
std
::
string
;
using
std
::
to_string
;
class
WrapperState
{
WrapperState
(
const
WrapperState
&
other
);
WrapperState
&
operator
=
(
const
WrapperState
&
rhs
);
KillZone
kill_zone
;
public
:
std
::
string
last_query
;
std
::
string
default_db
;
WrapperState
()
{}
~
WrapperState
()
{}
const
std
::
unique_ptr
<
QueryRewrite
>
&
getQueryRewrite
()
const
{
assert
(
this
->
qr
);
return
this
->
qr
;
}
void
setQueryRewrite
(
std
::
unique_ptr
<
QueryRewrite
>
&&
in_qr
)
{
this
->
qr
=
std
::
move
(
in_qr
);
}
void
selfKill
(
KillZone
::
Where
where
)
{
kill_zone
.
die
(
where
);
}
void
setKillZone
(
const
KillZone
&
kz
)
{
kill_zone
=
kz
;
}
std
::
unique_ptr
<
ProxyState
>
ps
;
std
::
vector
<
SchemaInfoRef
>
schema_info_refs
;
private
:
std
::
unique_ptr
<
QueryRewrite
>
qr
;
};
static
std
::
string
embeddedDir
=
"/t/cryt/shadow"
;
char
*
globalEsp
=
NULL
;
...
...
@@ -65,375 +32,3 @@ static std::map<std::string, WrapperState*> clients;
//This connection mimics the behaviour of MySQL-Proxy
Connect
*
globalConn
;
//Return values got by using directly the MySQL c Client
struct
rawMySQLReturnValue
{
std
::
vector
<
std
::
vector
<
std
::
string
>>
rowValues
;
/*data tuples*/
std
::
vector
<
std
::
string
>
fieldNames
;
std
::
vector
<
enum_field_types
>
fieldTypes
;
std
::
vector
<
int
>
lengths
;
std
::
vector
<
int
>
maxlengths
;
/*what's the difference between length and maxlength?*/
std
::
vector
<
int
>
choosen_onions
;
void
show
();
};
void
rawMySQLReturnValue
::
show
(){
cout
<<
"rowvalues:"
<<
endl
;
for
(
auto
item_vec
:
rowValues
){
for
(
auto
item
:
item_vec
){
cout
<<
item
.
size
()
<<
"
\t
"
;
}
cout
<<
endl
;
}
cout
<<
"types:"
<<
endl
;
for
(
auto
item
:
fieldTypes
){
cout
<<
IS_NUM
(
item
)
<<
"
\t
"
;
}
cout
<<
endl
;
cout
<<
"fieldNames:"
<<
endl
;
for
(
auto
item
:
fieldNames
){
cout
<<
item
<<
"
\t
"
;
}
cout
<<
endl
;
cout
<<
"lengths:"
<<
endl
;
for
(
auto
item
:
lengths
){
cout
<<
item
<<
"
\t
"
;
}
cout
<<
endl
;
cout
<<
"maxlengths:"
<<
endl
;
for
(
auto
item
:
maxlengths
){
cout
<<
item
<<
"
\t
"
;
}
cout
<<
endl
;
}
//helper function for transforming the rawMySQLReturnValue
static
Item_null
*
make_null
(
const
std
::
string
&
name
=
""
){
char
*
const
n
=
current_thd
->
strdup
(
name
.
c_str
());
return
new
Item_null
(
n
);
}
//helper function for transforming the rawMySQLReturnValue
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
;
}
//transform rawMySQLReturnValue to ResType
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
));
}
}
//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
*/
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
;
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"
;
//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
);
}
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
;
}
static
void
construct_insert
(
rawMySQLReturnValue
&
str
,
std
::
string
table
,
std
::
vector
<
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
)
+=
"
\"
,"
;
}
}
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
);
}
}
wrapper/Makefrag
View file @
a6614fec
OBJDIRS += wrapper
WRAPPER_SRCS := common.cc
WRAPPER_SRCS := common.cc
reuse.cc
all: $(OBJDIR)/libwrapper.so
...
...
wrapper/common.hh
View file @
a6614fec
...
...
@@ -59,3 +59,10 @@ public:
};
wrapper/reuse.cc
0 → 100644
View file @
a6614fec
#include "wrapper/reuse.hh"
using
std
::
cout
;
using
std
::
cin
;
using
std
::
endl
;
void
rawMySQLReturnValue
::
show
(){
cout
<<
"rowvalues:"
<<
endl
;
for
(
auto
item_vec
:
rowValues
){
for
(
auto
item
:
item_vec
){
cout
<<
item
.
size
()
<<
"
\t
"
;
}
cout
<<
endl
;
}
cout
<<
"types:"
<<
endl
;
for
(
auto
item
:
fieldTypes
){
cout
<<
IS_NUM
(
item
)
<<
"
\t
"
;
}
cout
<<
endl
;
cout
<<
"fieldNames:"
<<
endl
;
for
(
auto
item
:
fieldNames
){
cout
<<
item
<<
"
\t
"
;
}
cout
<<
endl
;
cout
<<
"lengths:"
<<
endl
;
for
(
auto
item
:
lengths
){
cout
<<
item
<<
"
\t
"
;
}
cout
<<
endl
;
cout
<<
"maxlengths:"
<<
endl
;
for
(
auto
item
:
maxlengths
){
cout
<<
item
<<
"
\t
"
;
}
cout
<<
endl
;
}
/*no default argument allowed???*/
/*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(""));
}
return out;
}
*/
wrapper/reuse.hh
0 → 100644
View file @
a6614fec
#pragma once
#include <main/rewrite_main.hh>
#include <main/rewrite_util.hh>
class
WrapperState
{
WrapperState
(
const
WrapperState
&
other
);
WrapperState
&
operator
=
(
const
WrapperState
&
rhs
);
KillZone
kill_zone
;
public
:
std
::
string
last_query
;
std
::
string
default_db
;
WrapperState
()
{}
~
WrapperState
()
{}
const
std
::
unique_ptr
<
QueryRewrite
>
&
getQueryRewrite
()
const
{
assert
(
this
->
qr
);
return
this
->
qr
;
}
void
setQueryRewrite
(
std
::
unique_ptr
<
QueryRewrite
>
&&
in_qr
)
{
this
->
qr
=
std
::
move
(
in_qr
);
}
void
selfKill
(
KillZone
::
Where
where
)
{
kill_zone
.
die
(
where
);
}
void
setKillZone
(
const
KillZone
&
kz
)
{
kill_zone
=
kz
;
}
std
::
unique_ptr
<
ProxyState
>
ps
;
std
::
vector
<
SchemaInfoRef
>
schema_info_refs
;
private
:
std
::
unique_ptr
<
QueryRewrite
>
qr
;
};
/*Raw return value from mysql*/
struct
rawMySQLReturnValue
{
std
::
vector
<
std
::
vector
<
std
::
string
>>
rowValues
;
/*data tuples*/
std
::
vector
<
std
::
string
>
fieldNames
;
std
::
vector
<
enum_field_types
>
fieldTypes
;
std
::
vector
<
int
>
lengths
;
std
::
vector
<
int
>
maxlengths
;
/*what's the difference between length and maxlength?*/
std
::
vector
<
int
>
choosen_onions
;
void
show
();
};
//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);
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