Skip to content

Commit ae488e3

Browse files
authored
Merge pull request #161 from atoomic/koan.atoomic/fix-issue-152
perf: cache is_private_key flag to avoid per-call BIGNUM alloc on 3.x
2 parents f3b02be + c56ad2a commit ae488e3

1 file changed

Lines changed: 16 additions & 10 deletions

File tree

RSA.xs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ typedef struct
5858
EVP_PKEY* rsa;
5959
int padding;
6060
int hashMode;
61+
int is_private_key; /* cached once at construction; avoids per-call BIGNUM alloc on 3.x */
6162
} rsaData;
6263

6364
/* Key names for the rsa hash structure */
@@ -100,24 +101,28 @@ void croakSsl(char* p_file, int p_line)
100101

101102
char _is_private(rsaData* p_rsa)
102103
{
103-
char ret = 0;
104+
return p_rsa->is_private_key;
105+
}
106+
107+
static int _detect_private_key(EVP_PKEY* p_rsa)
108+
{
104109
#if OLD_CRUFTY_SSL_VERSION
105-
const BIGNUM* d;
106-
d = p_rsa->rsa->d;
107-
ret = (d != NULL);
110+
return (p_rsa->d != NULL);
108111
#else
109112
#if OPENSSL_VERSION_NUMBER >= 0x30000000L
110113
BIGNUM* d = NULL;
111-
EVP_PKEY_get_bn_param(p_rsa->rsa, OSSL_PKEY_PARAM_RSA_D, &d);
112-
ret = (d != NULL);
113-
BN_clear_free(d);
114+
EVP_PKEY_get_bn_param(p_rsa, OSSL_PKEY_PARAM_RSA_D, &d);
115+
if (d) {
116+
BN_clear_free(d);
117+
return 1;
118+
}
119+
return 0;
114120
#else
115121
const BIGNUM* d = NULL;
116-
RSA_get0_key(p_rsa->rsa, NULL, NULL, &d);
117-
ret = (d != NULL);
122+
RSA_get0_key(p_rsa, NULL, NULL, &d);
123+
return (d != NULL);
118124
#endif
119125
#endif
120-
return ret;
121126
}
122127

123128
SV* make_rsa_obj(SV* p_proto, EVP_PKEY* p_rsa)
@@ -132,6 +137,7 @@ SV* make_rsa_obj(SV* p_proto, EVP_PKEY* p_rsa)
132137
rsa->hashMode = NID_sha1;
133138
#endif
134139
rsa->padding = RSA_PKCS1_OAEP_PADDING;
140+
rsa->is_private_key = _detect_private_key(p_rsa);
135141
return sv_bless(
136142
newRV_noinc(newSViv((IV) rsa)),
137143
(SvROK(p_proto) ? SvSTASH(SvRV(p_proto)) : gv_stashsv(p_proto, 1)));

0 commit comments

Comments
 (0)