Skip to content

Commit a43206d

Browse files
committed
新增:abe接口代码
1 parent e518634 commit a43206d

File tree

11 files changed

+377
-104
lines changed

11 files changed

+377
-104
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
.vs
44
out
55
build
6+
.vscode

devapi/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ add_library(devapi STATIC
3737
result.cc
3838
document.cc
3939
crud.cc
40+
abe_extern.cc
4041
${HEADERS}
4142
)
4243
add_subdirectory(abe)

devapi/abe/abe_crypto.cc

Lines changed: 37 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
#include <cassert>
33
#include <iostream>
44
#include <unistd.h>
5+
#include <openabe/openabe.h>
6+
#include <openabe/zsymcrypto.h>
57
#include <openssl/sha.h>
68
#include <openssl/rsa.h>
79
#include <openssl/err.h>
@@ -10,34 +12,36 @@
1012
#include "mysqlx/abe/abe_crypto.h"
1113
#include "mysqlx/abe/base64.h"
1214

15+
namespace mysqlx{
16+
namespace abe{
1317

14-
bool abe_crypto::encrypt(string pt, string policy, string &ct){
18+
bool abe_crypto::encrypt(std::string pt, std::string policy, std::string &ct){
1519

16-
InitializeOpenABE();
17-
OpenABECryptoContext cpabe("CP-ABE");
20+
oabe::InitializeOpenABE();
21+
oabe::OpenABECryptoContext cpabe("CP-ABE");
1822
cpabe.importPublicParams(mpk);
1923
cpabe.encrypt(policy.c_str(), pt, ct);
20-
ShutdownOpenABE();
24+
oabe::ShutdownOpenABE();
2125

2226
// std::cout<<"encrypt succefully!"<<std::endl;
2327
return true;
2428
}
2529

26-
bool abe_crypto::decrypt(string ct, string &pt){
30+
bool abe_crypto::decrypt(std::string ct, std::string &pt){
2731

2832
if(!check_abe_key()){
2933
return false;
3034
}
3135

32-
InitializeOpenABE();
33-
OpenABECryptoContext cpabe("CP-ABE");
36+
oabe::InitializeOpenABE();
37+
oabe::OpenABECryptoContext cpabe("CP-ABE");
3438
cpabe.importPublicParams(mpk);
3539
cpabe.importUserKey(user.user_id.c_str(), user.user_key);
3640
if(!cpabe.decrypt(user.user_id.c_str(), ct, pt)){
3741
pt = "can't decrypt.";
3842
}
3943
// std::cout << "Recovered message: " << pt << std::endl;
40-
ShutdownOpenABE();
44+
oabe::ShutdownOpenABE();
4145

4246
return true;
4347
}
@@ -51,9 +55,9 @@ bool abe_crypto::check_abe_key(){
5155
}
5256

5357

54-
bool abe_crypto::init(string mpk_path, string key_path,
55-
string kms_cert_path, string db_cert_path,
56-
string rsa_sk_path){
58+
bool abe_crypto::init(std::string mpk_path, std::string key_path,
59+
std::string kms_cert_path, std::string db_cert_path,
60+
std::string rsa_sk_path){
5761
if(!(import_mpk(mpk_path)
5862
&& import_db_cert(db_cert_path) && import_kms_cert(kms_cert_path)
5963
&& import_sk(rsa_sk_path))){
@@ -65,7 +69,7 @@ bool abe_crypto::init(string mpk_path, string key_path,
6569
return true;
6670
}
6771

68-
bool abe_crypto::import_mpk(string mpk_path){
72+
bool abe_crypto::import_mpk(std::string mpk_path){
6973
//读入mpk
7074
std::ifstream ifs_mpk(mpk_path, std::ios::in);
7175
if(!ifs_mpk){
@@ -77,7 +81,7 @@ bool abe_crypto::import_mpk(string mpk_path){
7781
return true;
7882
}
7983

80-
bool abe_crypto::import_user_key(string key_path){
84+
bool abe_crypto::import_user_key(std::string key_path){
8185
//读入abe_user_key
8286
std::ifstream ifs_key(key_path, std::ios::in);
8387
if(!ifs_key){
@@ -89,29 +93,27 @@ bool abe_crypto::import_user_key(string key_path){
8993
return true;
9094
}
9195

92-
bool abe_crypto::save_user_key(string key_path, string key_str_b64){
93-
string pt;
96+
bool abe_crypto::save_user_key(std::string key_path, std::string key_str_b64){
97+
std::string pt;
9498

9599
//key_str为base64编码
96100
size_t key_str_b64_length = key_str_b64.length();
97101
char * key_str = (char*)malloc(base64_utils::b64_dec_len(key_str_b64_length));
98102
size_t key_str_length = base64_utils::b64_decode(key_str_b64.c_str(), key_str_b64_length, (char*)key_str);
99103
// base64_utils::b64_decode(key_str_b64.c_str(), key_str_b64_length, (char*)key_str);
100104

101-
string ct(key_str,key_str_length);
105+
std::string ct(key_str,key_str_length);
102106
if(!rsa_decrypt(ct, pt)){
103107
free(key_str);
104108
ABE_ERROR("failed to decrypt abe user key");
105109
return false;
106110
}
107111
free(key_str);
108112

109-
if(user.user_key != ""){
110-
string decide = "";
111-
std::cout << "You already have abe key, do you want to update it?(Y/n)";
112-
if(!std::getline(std::cin, decide) || (decide != "y" && decide != "Y" && decide != "")){
113-
return false;
114-
}
113+
if(pt == ""){
114+
//todo: 后续可以考虑增加一个参数决定每次启动是否更新abe_key
115+
//或者提供一个函数让程序员自行决定是否更新
116+
return false;
115117
}
116118
//写入abe_user_key
117119
std::ofstream ofs_key(key_path, std::ios::out);
@@ -125,7 +127,7 @@ bool abe_crypto::save_user_key(string key_path, string key_str_b64){
125127
return true;
126128
}
127129

128-
bool abe_crypto::import_sk(string rsa_sk_path){
130+
bool abe_crypto::import_sk(std::string rsa_sk_path){
129131
// 导入rsa密钥文件并读取密钥
130132
FILE *hPriKeyFile = fopen(rsa_sk_path.c_str(), "rb");
131133
if (hPriKeyFile == NULL)
@@ -147,7 +149,7 @@ bool abe_crypto::import_sk(string rsa_sk_path){
147149
return true;
148150
}
149151

150-
RSA * abe_crypto::import_pk(const string cert_path, string &err_msg){
152+
RSA * abe_crypto::import_pk(const std::string cert_path, std::string &err_msg){
151153
RSA * pk;
152154
// 导入证书文件并读取公钥
153155
FILE *hPubKeyFile = fopen(cert_path.c_str(), "rb");
@@ -183,8 +185,8 @@ RSA * abe_crypto::import_pk(const string cert_path, string &err_msg){
183185
return pk;
184186
}
185187

186-
bool abe_crypto::import_db_cert(string db_cert_path){
187-
string err_msg;
188+
bool abe_crypto::import_db_cert(std::string db_cert_path){
189+
std::string err_msg;
188190
RSA *pk = import_pk(db_cert_path, err_msg);
189191
if(pk == NULL){
190192
err_msg += ":" + db_cert_path;
@@ -195,8 +197,8 @@ bool abe_crypto::import_db_cert(string db_cert_path){
195197
return true;
196198
}
197199

198-
bool abe_crypto::import_kms_cert(string kms_cert_path){
199-
string err_msg;
200+
bool abe_crypto::import_kms_cert(std::string kms_cert_path){
201+
std::string err_msg;
200202
RSA *pk = import_pk(kms_cert_path, err_msg);
201203
if(pk == NULL){
202204
err_msg += ":" + kms_cert_path;
@@ -233,7 +235,7 @@ bool abe_crypto::verify_sig(RSA *pk, unsigned char * msg, size_t msg_length, uns
233235

234236
}
235237

236-
bool abe_crypto::verify_db_sig(const string msg, const string sig_b64){
238+
bool abe_crypto::verify_db_sig(const std::string msg, const std::string sig_b64){
237239
//sig是base64编码,需要先解码
238240
size_t sig_b64_length = sig_b64.length();
239241
unsigned char * sig = (unsigned char*)malloc(base64_utils::b64_dec_len(sig_b64_length));
@@ -249,7 +251,7 @@ bool abe_crypto::verify_db_sig(const string msg, const string sig_b64){
249251
return true;
250252
}
251253

252-
bool abe_crypto::verify_kms_sig(const string msg_b64, const string sig_b64){
254+
bool abe_crypto::verify_kms_sig(const std::string msg_b64, const std::string sig_b64){
253255

254256
//msg和sig都是base64编码,需要先解码
255257
size_t msg_b64_length = msg_b64.length();
@@ -274,7 +276,7 @@ bool abe_crypto::verify_kms_sig(const string msg_b64, const string sig_b64){
274276
}
275277

276278
//注意ct初始化时必须指定长度,否则ct.length会因为0x00而截断
277-
bool abe_crypto::rsa_decrypt(const string ct, string &pt){
279+
bool abe_crypto::rsa_decrypt(const std::string ct, std::string &pt){
278280
int nLen = RSA_size(sk);
279281
char *pDecode = new char[nLen + 1];
280282
bool flag = true;
@@ -316,4 +318,7 @@ bool abe_crypto::rsa_decrypt(const string ct, string &pt){
316318
delete[] pDecode;
317319
CRYPTO_cleanup_all_ex_data();
318320
return flag;
319-
}
321+
}
322+
323+
}//namespace mysqlx::abe
324+
}//namespace mysqlx

devapi/abe/base64.cc

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
#include "mysqlx/abe/base64.h"
22
#include <stddef.h>
33
#include <iostream>
4+
5+
namespace mysqlx{
6+
namespace abe{
7+
namespace base64_utils {
8+
49
typedef unsigned int uint32;
510
#define BASE64_ERROR(msg) std::cerr << "base64 error: " << (msg) << std::endl;
6-
unsigned base64_utils::b64_encode(const char* src, unsigned len, char* dst)
11+
12+
unsigned b64_encode(const char* src, unsigned len, char* dst)
713
{
814
char *p = NULL;
915
const char *s = NULL, *end = src + len;
@@ -39,7 +45,7 @@ unsigned base64_utils::b64_encode(const char* src, unsigned len, char* dst)
3945
return p - dst;
4046
}
4147

42-
unsigned base64_utils::b64_decode(const char* src, unsigned len, char* dst)
48+
unsigned b64_decode(const char* src, unsigned len, char* dst)
4349
{
4450
const char *srcend = src + len, *s = src;
4551
char* p = dst;
@@ -92,12 +98,16 @@ unsigned base64_utils::b64_decode(const char* src, unsigned len, char* dst)
9298
return p - dst;
9399
}
94100

95-
unsigned base64_utils::b64_enc_len(unsigned srclen)
101+
unsigned b64_enc_len(unsigned srclen)
96102
{
97103
return (srclen + 2) / 3 * 4;
98104
}
99105

100-
unsigned base64_utils::b64_dec_len(unsigned srclen)
106+
unsigned b64_dec_len(unsigned srclen)
101107
{
102108
return srclen / 4 * 3;
103-
}
109+
}
110+
111+
}
112+
}//namespace mysqlx::abe
113+
}//namespace mysqlx

devapi/abe/rewrite.cc

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#include <iostream>
44
#include <string>
55
#include <algorithm>
6-
using std::string;
6+
// using std::std::string;
77

88

99
/*因sql-parser库对mysql语句支持很不友好,且当前并未找到实用的sql解析库
@@ -14,8 +14,10 @@ using std::string;
1414
* 加密函数格式:abe_enc(<data>,<policy>)
1515
* 解密函数格式:abe_dec(<field name>)
1616
*/
17+
namespace mysqlx{
18+
namespace abe{
1719

18-
CommandType simple_parse(const string sql){
20+
CommandType simple_parse(const std::string sql){
1921
std::smatch result;
2022
for(auto it: PATTERNS_ALL.mp){
2123
if(std::regex_match(sql, result, it.second)){
@@ -26,10 +28,10 @@ CommandType simple_parse(const string sql){
2628
}
2729

2830

29-
bool rewrite_plan::insert_handler(string &real_sql, const string &raw_sql){
31+
bool rewrite_plan::insert_handler(std::string &real_sql, const std::string &raw_sql){
3032
std::regex pattern = PATTERNS_ALL.ABE_ENC_SQL_PATTERN;
3133
std::regex pattern_enc = PATTERNS_ALL.ABE_ENC_PATTERN;
32-
string new_sql = raw_sql;
34+
std::string new_sql = raw_sql;
3335
std::smatch result;
3436
while (std::regex_match(new_sql, result, pattern, std::regex_constants::format_first_only)){
3537
is_enc = true;//需要加密
@@ -38,7 +40,7 @@ bool rewrite_plan::insert_handler(string &real_sql, const string &raw_sql){
3840
temp.data = result[1];
3941
temp.policy = result[2];
4042

41-
string cipher;
43+
std::string cipher;
4244
crypto->encrypt(temp.data, temp.policy, cipher);
4345

4446
temp.enc_data = "'" + cipher + "'";
@@ -52,11 +54,11 @@ bool rewrite_plan::insert_handler(string &real_sql, const string &raw_sql){
5254
return true;
5355
}
5456

55-
bool rewrite_plan::select_handler(string &real_sql, const string &raw_sql){
57+
bool rewrite_plan::select_handler(std::string &real_sql, const std::string &raw_sql){
5658
std::regex pattern = PATTERNS_ALL.ABE_DEC_SQL_PATTERN;
5759
std::regex pattern_dec = PATTERNS_ALL.ABE_DEC_PATTERN;
5860
std::smatch result;
59-
string new_sql = raw_sql;
61+
std::string new_sql = raw_sql;
6062
while (std::regex_match(new_sql, result, pattern, std::regex_constants::format_first_only)){
6163
is_dec = true;//需要解密
6264

@@ -109,10 +111,13 @@ bool rewrite_plan::parse_and_rewrite(){
109111
return true;
110112
}
111113

112-
std::vector<string> rewrite_plan::field_name_list() const {
113-
std::vector<string> list;
114+
std::vector<std::string> rewrite_plan::get_field_name_list() const {
115+
std::vector<std::string> list;
114116
for(auto item : dec_plan){
115117
list.push_back(item.field_name);
116118
}
117119
return list;
118-
}
120+
}
121+
122+
}//namespace mysqlx::abe
123+
}//namespace mysqlx

0 commit comments

Comments
 (0)