Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions KPHP/runtime/openssl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,13 @@ bool f$openssl_public_encrypt (const string &data, string &result, const string
result = string();
return false;
}

#if OPENSSL_VERSION_NUMBER < 0x10100000L
if (pkey->type != EVP_PKEY_RSA && pkey->type != EVP_PKEY_RSA2) {
#else
if (EVP_PKEY_id (pkey) != EVP_PKEY_RSA && EVP_PKEY_id (pkey) != EVP_PKEY_RSA2) {
#endif

if (!from_cache) {
EVP_PKEY_free (pkey);
}
Expand All @@ -332,8 +338,17 @@ bool f$openssl_public_encrypt (const string &data, string &result, const string
int key_size = EVP_PKEY_size (pkey);
php_assert (PHP_BUF_LEN >= key_size);




#if OPENSSL_VERSION_NUMBER < 0x10100000L
if (RSA_public_encrypt ((int)data.size(), reinterpret_cast <const unsigned char *> (data.c_str()),
reinterpret_cast <unsigned char *> (php_buf), pkey->pkey.rsa, RSA_PKCS1_PADDING) != key_size) {
#else
if (RSA_public_encrypt ((int)data.size(), reinterpret_cast <const unsigned char *> (data.c_str()),
reinterpret_cast <unsigned char *> (php_buf), EVP_PKEY_get0_RSA (pkey), RSA_PKCS1_PADDING) != key_size) {
#endif

if (!from_cache) {
EVP_PKEY_free (pkey);
}
Expand Down Expand Up @@ -372,7 +387,8 @@ bool f$openssl_private_decrypt (const string &data, string &result, const string
php_warning ("Parameter key is not a valid private key");
return false;
}
if (pkey->type != EVP_PKEY_RSA && pkey->type != EVP_PKEY_RSA2) {

if (EVP_PKEY_id (pkey) != EVP_PKEY_RSA && EVP_PKEY_id (pkey) != EVP_PKEY_RSA2) {
if (!from_cache) {
EVP_PKEY_free (pkey);
}
Expand All @@ -385,7 +401,7 @@ bool f$openssl_private_decrypt (const string &data, string &result, const string
php_assert (PHP_BUF_LEN >= key_size);

int len = RSA_private_decrypt ((int)data.size(), reinterpret_cast <const unsigned char *> (data.c_str()),
reinterpret_cast <unsigned char *> (php_buf), pkey->pkey.rsa, RSA_PKCS1_PADDING);
reinterpret_cast <unsigned char *> (php_buf), EVP_PKEY_get0_RSA (pkey), RSA_PKCS1_PADDING);
if (!from_cache) {
EVP_PKEY_free (pkey);
}
Expand Down
14 changes: 10 additions & 4 deletions image/image-data.c
Original file line number Diff line number Diff line change
Expand Up @@ -1077,10 +1077,16 @@ static void *forth_md5_hex (void **IP, struct forth_stack *st) {
}

char s[33];
if (1) {
st->top = old_stack_top;
return failf (st, "md5_hex: convert_to_hex failed");
}

int i;
for (i=0; i<16; i++)
sprintf(s+i*2, "%02x", out[i]);
s[32]=0;

// if (1) {
// st->top = old_stack_top;
// return failf (st, "md5_hex: convert_to_hex failed");
// }

free_stack (st, st->top + 1, old_stack_top);

Expand Down
29 changes: 28 additions & 1 deletion mc-proxy/mc-proxy-random-extension.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,14 @@ int random_merge_end_query (struct connection *c, const char *key, int key_len,
}
}

int c_len, f_len;

#if OPENSSL_VERSION_NUMBER < 0x10100000L
// for openssl 1.0.2
EVP_CIPHER_CTX e;
EVP_CIPHER_CTX_init (&e);

EVP_EncryptInit_ex (&e, EVP_aes_256_cbc(), NULL, R, R + 32);
int c_len, f_len;

if (!EVP_EncryptUpdate (&e, A, &c_len, R + 64, max_bytes - 64)) {
vkprintf (1, "EVP_EncryptUpdate fail.\n");
Expand All @@ -83,7 +87,30 @@ int random_merge_end_query (struct connection *c, const char *key, int key_len,
EVP_CIPHER_CTX_cleanup (&e);
return 0;
}

EVP_CIPHER_CTX_cleanup (&e);
#else
// for openssl 1.1.0
EVP_CIPHER_CTX *e;
e = EVP_CIPHER_CTX_new ();

EVP_EncryptInit_ex (e, EVP_aes_256_cbc(), NULL, R, R + 32);

if (!EVP_EncryptUpdate (e, A, &c_len, R + 64, max_bytes - 64)) {
vkprintf (1, "EVP_EncryptUpdate fail.\n");
EVP_CIPHER_CTX_free (e);
return 0;
}

if (!EVP_EncryptFinal_ex (e, A + c_len, &f_len)) {
vkprintf (1, "EVP_EncryptFinal_ex fail.\n");
EVP_CIPHER_CTX_free (e);
return 0;
}

EVP_CIPHER_CTX_free (e);
#endif

f_len += c_len;

int res = f_len < data[0].res_bytes ? f_len : data[0].res_bytes;
Expand Down
39 changes: 36 additions & 3 deletions net/net-crypto-rsa.c
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,30 @@ int rsa_encrypt (const char *const private_key_name, void *input, int ilen, void
goto clean;
}
memcpy (b + 4 + rsa_size, iv, 32);


int c_len, f_len;

#if OPENSSL_VERSION_NUMBER < 0x10100000L
// for openssl 1.0.2
EVP_CIPHER_CTX e;
EVP_CIPHER_CTX_init (&e);

EVP_EncryptInit_ex (&e, EVP_aes_256_cbc(), NULL, key, iv);
int c_len, f_len;
EVP_EncryptUpdate (&e, b + 4 + rsa_size + 32, &c_len, input, ilen);
EVP_EncryptFinal_ex (&e, b + 4 + rsa_size + 32 + c_len, &f_len);
EVP_CIPHER_CTX_cleanup (&e);
#else
// for openssl 1.1.0
EVP_CIPHER_CTX *e;
e = EVP_CIPHER_CTX_new ();

EVP_EncryptInit_ex (e, EVP_aes_256_cbc(), NULL, key, iv);
EVP_EncryptUpdate (e, b + 4 + rsa_size + 32, &c_len, input, ilen);
EVP_EncryptFinal_ex (e, b + 4 + rsa_size + 32 + c_len, &f_len);
EVP_CIPHER_CTX_free (e);
#endif

int r = 4 + rsa_size + 32 + c_len + f_len;
vkprintf (3, "c_len = %d, f_len = %d\n", c_len, f_len);
assert (r <= *olen);
Expand Down Expand Up @@ -208,15 +225,31 @@ int rsa_decrypt (const char *const key_name, int public_key, void *input, int il
}
*output = a;
int p_len = 0;

int f_len = 0;

#if OPENSSL_VERSION_NUMBER < 0x10100000L
// for openssl 1.0.2
EVP_CIPHER_CTX e;
EVP_CIPHER_CTX_init (&e);

EVP_DecryptInit_ex (&e, EVP_aes_256_cbc(), NULL, key, iv);
EVP_DecryptUpdate (&e, a, &p_len, input + 4 + rsa_size + 32, aes_cipher_len);
int f_len = 0;
EVP_DecryptFinal_ex (&e, input + 4 + rsa_size + 32 + p_len, &f_len);
EVP_CIPHER_CTX_cleanup (&e);
#else
// for openssl 1.1.0
EVP_CIPHER_CTX *e;
e = EVP_CIPHER_CTX_new ();

EVP_DecryptInit_ex (e, EVP_aes_256_cbc(), NULL, key, iv);
EVP_DecryptUpdate (e, a, &p_len, input + 4 + rsa_size + 32, aes_cipher_len);
EVP_DecryptFinal_ex (e, input + 4 + rsa_size + 32 + p_len, &f_len);
EVP_CIPHER_CTX_free (e);
#endif

vkprintf (3, "p_len = %d, f_len = %d\n", p_len, f_len);
*olen = p_len + f_len;
EVP_CIPHER_CTX_cleanup (&e);

clean:
if (f != NULL) {
Expand Down