2
2
#include < cassert>
3
3
#include < iostream>
4
4
#include < unistd.h>
5
+ #include < openabe/openabe.h>
6
+ #include < openabe/zsymcrypto.h>
5
7
#include < openssl/sha.h>
6
8
#include < openssl/rsa.h>
7
9
#include < openssl/err.h>
10
12
#include " mysqlx/abe/abe_crypto.h"
11
13
#include " mysqlx/abe/base64.h"
12
14
15
+ namespace mysqlx {
16
+ namespace abe {
13
17
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){
15
19
16
- InitializeOpenABE ();
17
- OpenABECryptoContext cpabe (" CP-ABE" );
20
+ oabe:: InitializeOpenABE ();
21
+ oabe:: OpenABECryptoContext cpabe (" CP-ABE" );
18
22
cpabe.importPublicParams (mpk);
19
23
cpabe.encrypt (policy.c_str (), pt, ct);
20
- ShutdownOpenABE ();
24
+ oabe:: ShutdownOpenABE ();
21
25
22
26
// std::cout<<"encrypt succefully!"<<std::endl;
23
27
return true ;
24
28
}
25
29
26
- bool abe_crypto::decrypt (string ct, string &pt){
30
+ bool abe_crypto::decrypt (std:: string ct, std:: string &pt){
27
31
28
32
if (!check_abe_key ()){
29
33
return false ;
30
34
}
31
35
32
- InitializeOpenABE ();
33
- OpenABECryptoContext cpabe (" CP-ABE" );
36
+ oabe:: InitializeOpenABE ();
37
+ oabe:: OpenABECryptoContext cpabe (" CP-ABE" );
34
38
cpabe.importPublicParams (mpk);
35
39
cpabe.importUserKey (user.user_id .c_str (), user.user_key );
36
40
if (!cpabe.decrypt (user.user_id .c_str (), ct, pt)){
37
41
pt = " can't decrypt." ;
38
42
}
39
43
// std::cout << "Recovered message: " << pt << std::endl;
40
- ShutdownOpenABE ();
44
+ oabe:: ShutdownOpenABE ();
41
45
42
46
return true ;
43
47
}
@@ -51,9 +55,9 @@ bool abe_crypto::check_abe_key(){
51
55
}
52
56
53
57
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){
57
61
if (!(import_mpk (mpk_path)
58
62
&& import_db_cert (db_cert_path) && import_kms_cert (kms_cert_path)
59
63
&& import_sk (rsa_sk_path))){
@@ -65,7 +69,7 @@ bool abe_crypto::init(string mpk_path, string key_path,
65
69
return true ;
66
70
}
67
71
68
- bool abe_crypto::import_mpk (string mpk_path){
72
+ bool abe_crypto::import_mpk (std:: string mpk_path){
69
73
// 读入mpk
70
74
std::ifstream ifs_mpk (mpk_path, std::ios::in);
71
75
if (!ifs_mpk){
@@ -77,7 +81,7 @@ bool abe_crypto::import_mpk(string mpk_path){
77
81
return true ;
78
82
}
79
83
80
- bool abe_crypto::import_user_key (string key_path){
84
+ bool abe_crypto::import_user_key (std:: string key_path){
81
85
// 读入abe_user_key
82
86
std::ifstream ifs_key (key_path, std::ios::in);
83
87
if (!ifs_key){
@@ -89,29 +93,27 @@ bool abe_crypto::import_user_key(string key_path){
89
93
return true ;
90
94
}
91
95
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;
94
98
95
99
// key_str为base64编码
96
100
size_t key_str_b64_length = key_str_b64.length ();
97
101
char * key_str = (char *)malloc (base64_utils::b64_dec_len (key_str_b64_length));
98
102
size_t key_str_length = base64_utils::b64_decode (key_str_b64.c_str (), key_str_b64_length, (char *)key_str);
99
103
// base64_utils::b64_decode(key_str_b64.c_str(), key_str_b64_length, (char*)key_str);
100
104
101
- string ct (key_str,key_str_length);
105
+ std:: string ct (key_str,key_str_length);
102
106
if (!rsa_decrypt (ct, pt)){
103
107
free (key_str);
104
108
ABE_ERROR (" failed to decrypt abe user key" );
105
109
return false ;
106
110
}
107
111
free (key_str);
108
112
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 ;
115
117
}
116
118
// 写入abe_user_key
117
119
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){
125
127
return true ;
126
128
}
127
129
128
- bool abe_crypto::import_sk (string rsa_sk_path){
130
+ bool abe_crypto::import_sk (std:: string rsa_sk_path){
129
131
// 导入rsa密钥文件并读取密钥
130
132
FILE *hPriKeyFile = fopen (rsa_sk_path.c_str (), " rb" );
131
133
if (hPriKeyFile == NULL )
@@ -147,7 +149,7 @@ bool abe_crypto::import_sk(string rsa_sk_path){
147
149
return true ;
148
150
}
149
151
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){
151
153
RSA * pk;
152
154
// 导入证书文件并读取公钥
153
155
FILE *hPubKeyFile = fopen (cert_path.c_str (), " rb" );
@@ -183,8 +185,8 @@ RSA * abe_crypto::import_pk(const string cert_path, string &err_msg){
183
185
return pk;
184
186
}
185
187
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;
188
190
RSA *pk = import_pk (db_cert_path, err_msg);
189
191
if (pk == NULL ){
190
192
err_msg += " :" + db_cert_path;
@@ -195,8 +197,8 @@ bool abe_crypto::import_db_cert(string db_cert_path){
195
197
return true ;
196
198
}
197
199
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;
200
202
RSA *pk = import_pk (kms_cert_path, err_msg);
201
203
if (pk == NULL ){
202
204
err_msg += " :" + kms_cert_path;
@@ -233,7 +235,7 @@ bool abe_crypto::verify_sig(RSA *pk, unsigned char * msg, size_t msg_length, uns
233
235
234
236
}
235
237
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){
237
239
// sig是base64编码,需要先解码
238
240
size_t sig_b64_length = sig_b64.length ();
239
241
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){
249
251
return true ;
250
252
}
251
253
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){
253
255
254
256
// msg和sig都是base64编码,需要先解码
255
257
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){
274
276
}
275
277
276
278
// 注意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){
278
280
int nLen = RSA_size (sk);
279
281
char *pDecode = new char [nLen + 1 ];
280
282
bool flag = true ;
@@ -316,4 +318,7 @@ bool abe_crypto::rsa_decrypt(const string ct, string &pt){
316
318
delete[] pDecode;
317
319
CRYPTO_cleanup_all_ex_data ();
318
320
return flag;
319
- }
321
+ }
322
+
323
+ }// namespace mysqlx::abe
324
+ }// namespace mysqlx
0 commit comments