mirror of
https://github.com/sogou/workflow.git
synced 2026-10-29 03:27:04 +08:00
Add MySQL RSA encrypt.
This commit is contained in:
@@ -293,10 +293,14 @@ CommMessageIn *ComplexMySQLTask::message_in()
|
||||
|
||||
case ST_AUTH_REQUEST:
|
||||
case ST_AUTH_SWITCH_REQUEST:
|
||||
case ST_CLEAR_PASSWORD_REQUEST:
|
||||
resp = new MySQLAuthResponse;
|
||||
break;
|
||||
|
||||
case ST_CLEAR_PASSWORD_REQUEST:
|
||||
case ST_RSA_AUTH_REQUEST:
|
||||
resp = new MySQLResponse;
|
||||
break;
|
||||
|
||||
case ST_SHA256_PUBLIC_KEY_REQUEST:
|
||||
case ST_CSHA2_PUBLIC_KEY_REQUEST:
|
||||
resp = new MySQLPublicKeyResponse;
|
||||
@@ -437,6 +441,7 @@ int ComplexMySQLTask::keep_alive_timeout()
|
||||
case ST_AUTH_REQUEST:
|
||||
case ST_AUTH_SWITCH_REQUEST:
|
||||
case ST_CLEAR_PASSWORD_REQUEST:
|
||||
case ST_RSA_AUTH_REQUEST:
|
||||
if (resp->is_ok_packet())
|
||||
{
|
||||
if (!res_charset_.empty())
|
||||
@@ -447,7 +452,9 @@ int ComplexMySQLTask::keep_alive_timeout()
|
||||
break;
|
||||
}
|
||||
|
||||
if (resp->is_error_packet() || conn->state == ST_CLEAR_PASSWORD_REQUEST)
|
||||
if (resp->is_error_packet() ||
|
||||
conn->state == ST_CLEAR_PASSWORD_REQUEST ||
|
||||
conn->state == ST_RSA_AUTH_REQUEST)
|
||||
{
|
||||
this->resp = std::move(*resp);
|
||||
state_ = WFT_STATE_TASK_ERROR;
|
||||
@@ -474,8 +481,6 @@ int ComplexMySQLTask::keep_alive_timeout()
|
||||
conn->state = ST_RSA_AUTH_REQUEST;
|
||||
break;
|
||||
|
||||
case ST_RSA_AUTH_REQUEST:
|
||||
break;
|
||||
|
||||
case ST_CHARSET_REQUEST:
|
||||
if (!resp->is_ok_packet())
|
||||
|
||||
@@ -535,8 +535,63 @@ int MySQLPublicKeyResponse::encode(struct iovec vectors[], int max)
|
||||
return MySQLMessage::encode(vectors, max);
|
||||
}
|
||||
|
||||
int MySQLRSAAuthRequest::rsa_encrypt(void *ctx)
|
||||
{
|
||||
EVP_PKEY_CTX *pkey_ctx = (EVP_PKEY_CTX *)ctx;
|
||||
std::string pass = password_;
|
||||
unsigned char *p = (unsigned char *)pass.c_str();
|
||||
unsigned char out[256];
|
||||
size_t outlen = 256;
|
||||
|
||||
for (size_t i = 0; i < pass.size(); i++)
|
||||
p[i] ^= seed_[i % 20];
|
||||
|
||||
if (EVP_PKEY_encrypt_init(pkey_ctx) > 0 &&
|
||||
EVP_PKEY_CTX_set_rsa_padding(pkey_ctx, RSA_PKCS1_OAEP_PADDING) > 0 &&
|
||||
EVP_PKEY_encrypt(pkey_ctx, out, &outlen, p, pass.size()) > 0)
|
||||
{
|
||||
buf_.assign((char *)out, 256);
|
||||
return 0;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
int MySQLRSAAuthRequest::encode(struct iovec vectors[], int max)
|
||||
{
|
||||
RSA *rsa;
|
||||
BIO *bio;
|
||||
|
||||
bio = BIO_new_mem_buf(public_key_.c_str(), public_key_.size());
|
||||
if (!bio)
|
||||
return -1;
|
||||
|
||||
rsa = PEM_read_bio_RSA_PUBKEY(bio, NULL, NULL, NULL);
|
||||
BIO_free(bio);
|
||||
|
||||
EVP_PKEY_CTX *ctx;
|
||||
EVP_PKEY *pkey;
|
||||
int ret = -1;
|
||||
|
||||
pkey = EVP_PKEY_new();
|
||||
if (pkey)
|
||||
{
|
||||
EVP_PKEY_assign_RSA(pkey, rsa);
|
||||
ctx = EVP_PKEY_CTX_new(pkey, NULL);
|
||||
if (ctx)
|
||||
{
|
||||
ret = rsa_encrypt(ctx);
|
||||
EVP_PKEY_CTX_free(ctx);
|
||||
}
|
||||
|
||||
EVP_PKEY_free(pkey);
|
||||
}
|
||||
else
|
||||
RSA_free(rsa);
|
||||
|
||||
if (ret < 0)
|
||||
return -1;
|
||||
|
||||
return MySQLMessage::encode(vectors, max);
|
||||
}
|
||||
|
||||
|
||||
@@ -307,6 +307,8 @@ private:
|
||||
return -2;
|
||||
}
|
||||
|
||||
int rsa_encrypt(void *ctx);
|
||||
|
||||
std::string password_;
|
||||
std::string public_key_;
|
||||
unsigned char seed_[20];
|
||||
|
||||
Reference in New Issue
Block a user