Skip to content

Commit 853548c

Browse files
committed
修复:多线程下不能使用ABE加解密的bug
1 parent 50a88b1 commit 853548c

File tree

3 files changed

+29
-8
lines changed

3 files changed

+29
-8
lines changed

devapi/abe/abe_crypto.cc

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <unistd.h>
55
#include <openabe/openabe.h>
66
#include <openabe/zsymcrypto.h>
7+
#include <mutex>
78
#include <openssl/sha.h>
89
#include <openssl/rsa.h>
910
#include <openssl/err.h>
@@ -17,12 +18,12 @@ MYSQLX_ABI_BEGIN(2,0)
1718
namespace abe{
1819

1920
void abe_crypto::encrypt(std::string pt, std::string policy, std::string &ct){
20-
21-
oabe::InitializeOpenABE();
21+
oabe::OpenABEStateContext thread_context;
22+
thread_context.initializeThread();
2223
oabe::OpenABECryptoContext cpabe("CP-ABE");
2324
cpabe.importPublicParams(mpk);
2425
cpabe.encrypt(policy.c_str(), pt, ct);
25-
oabe::ShutdownOpenABE();
26+
thread_context.shutdownThread();
2627
}
2728

2829
void abe_crypto::decrypt(const std::string ct, std::string &pt){
@@ -31,15 +32,16 @@ void abe_crypto::decrypt(const std::string ct, std::string &pt){
3132
ABE_ERROR("no abe key!");
3233
}
3334

34-
oabe::InitializeOpenABE();
35+
oabe::OpenABEStateContext thread_context;
36+
thread_context.initializeThread();
3537
oabe::OpenABECryptoContext cpabe("CP-ABE");
3638
cpabe.importPublicParams(mpk);
3739
cpabe.importUserKey(user.user_id.c_str(), user.user_key);
3840
if(!cpabe.decrypt(user.user_id.c_str(), ct, pt)){
39-
oabe::ShutdownOpenABE();
41+
thread_context.shutdownThread();
4042
ABE_ERROR("abe: can't decrypt.");
4143
}
42-
oabe::ShutdownOpenABE();
44+
thread_context.shutdownThread();
4345
}
4446

4547
bool abe_crypto::check_abe_key(){
@@ -297,6 +299,15 @@ bool abe_crypto::rsa_decrypt(const std::string ct, std::string &pt){
297299
return flag;
298300
}
299301

302+
void _initialize_abe(){
303+
oabe::InitializeOpenABE();
304+
}
305+
306+
void _shutdown_abe(){
307+
oabe::ShutdownOpenABE();
308+
}
309+
310+
300311
}//namespace mysqlx::abe
301312

302313
MYSQLX_ABI_END(2,0)

include/mysqlx/abe/abe_crypto.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,9 @@ class PUBLIC_API abe_crypto{
9090
RSA * import_pk(const std::string cert_path, std::string &err_msg);
9191
};
9292

93+
void PUBLIC_API _initialize_abe();
94+
void PUBLIC_API _shutdown_abe();
95+
9396
}//namespace mysqlx::abe
9497
MYSQLX_ABI_END(2,0)
9598
}//namespace mysqlx

include/mysqlx/abe_extern.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,15 @@ class PUBLIC_API abe_env{
8181
};
8282

8383

84-
void abe_encrypt(abe::abe_crypto &abe, std::string pt, std::string policy, std::string &ct );
85-
void abe_decrypt(abe::abe_crypto &abe, std::string ct, std::string &pt);
84+
void PUBLIC_API abe_encrypt(abe::abe_crypto &abe, std::string pt, std::string policy, std::string &ct );
85+
void PUBLIC_API abe_decrypt(abe::abe_crypto &abe, std::string ct, std::string &pt);
86+
87+
void PUBLIC_API initialize_abe(){
88+
abe::_initialize_abe();
89+
}
90+
void PUBLIC_API shutdown_abe(){
91+
abe::_shutdown_abe();
92+
}
8693

8794
MYSQLX_ABI_END(2,0)
8895
}

0 commit comments

Comments
 (0)