Skip to content

Commit 6278e28

Browse files
committed
Add the support for sm2 keypairs import
1 parent 2f787df commit 6278e28

File tree

1 file changed

+62
-54
lines changed

1 file changed

+62
-54
lines changed

ext/openssl/openssl.c

Lines changed: 62 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -4304,36 +4304,31 @@ static bool php_openssl_pkey_init_legacy_ec(EC_KEY *eckey, zval *data, bool *is_
43044304
goto clean_exit;
43054305
}
43064306

4307-
/* custom params not supported with SM2, SKIP */
4308-
if (!curve_name_zv ||
4309-
(Z_TYPE_P(curve_name_zv) == IS_STRING &&
4310-
OBJ_sn2nid(Z_STRVAL_P(curve_name_zv)) != OBJ_sn2nid("SM2"))){
43114307
OPENSSL_PKEY_SET_BN(data, d);
43124308
OPENSSL_PKEY_SET_BN(data, x);
43134309
OPENSSL_PKEY_SET_BN(data, y);
43144310

4315-
if (d) {
4316-
*is_private = true;
4317-
if (!EC_KEY_set_private_key(eckey, d)) {
4318-
goto clean_exit;
4319-
}
4311+
if (d) {
4312+
*is_private = true;
4313+
if (!EC_KEY_set_private_key(eckey, d)) {
4314+
goto clean_exit;
4315+
}
43204316

4321-
point_q = EC_POINT_new(group);
4322-
if (!point_q || !EC_POINT_mul(group, point_q, d, NULL, NULL, bctx)) {
4323-
goto clean_exit;
4324-
}
4325-
} else if (x && y) {
4326-
/* OpenSSL does not allow setting EC_PUB_X/EC_PUB_Y, so convert to encoded format. */
4327-
point_q = EC_POINT_new(group);
4328-
if (!point_q || !EC_POINT_set_affine_coordinates(group, point_q, x, y, bctx)) {
4329-
goto clean_exit;
4330-
}
4317+
point_q = EC_POINT_new(group);
4318+
if (!point_q || !EC_POINT_mul(group, point_q, d, NULL, NULL, bctx)) {
4319+
goto clean_exit;
4320+
}
4321+
} else if (x && y) {
4322+
/* OpenSSL does not allow setting EC_PUB_X/EC_PUB_Y, so convert to encoded format. */
4323+
point_q = EC_POINT_new(group);
4324+
if (!point_q || !EC_POINT_set_affine_coordinates(group, point_q, x, y, bctx)) {
4325+
goto clean_exit;
43314326
}
4327+
}
43324328

4333-
if (point_q != NULL) {
4334-
if (!EC_KEY_set_public_key(eckey, point_q)) {
4335-
goto clean_exit;
4336-
}
4329+
if (point_q != NULL) {
4330+
if (!EC_KEY_set_public_key(eckey, point_q)) {
4331+
goto clean_exit;
43374332
}
43384333
}
43394334

@@ -4373,7 +4368,7 @@ static EVP_PKEY *php_openssl_pkey_init_ec(zval *data, bool *is_private) {
43734368
EC_POINT *point_q = NULL;
43744369
unsigned char *point_q_buf = NULL;
43754370
EVP_PKEY *param_key = NULL, *pkey = NULL;
4376-
EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new_from_name(NULL, "EC", NULL);
4371+
EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL);
43774372
BN_CTX *bctx = BN_CTX_new();
43784373
OSSL_PARAM *params = NULL;
43794374
OSSL_PARAM_BLD *bld = OSSL_PARAM_BLD_new();
@@ -4389,7 +4384,12 @@ static EVP_PKEY *php_openssl_pkey_init_ec(zval *data, bool *is_private) {
43894384
php_error_docref(NULL, E_WARNING, "Unknown curve name");
43904385
goto cleanup;
43914386
}
4392-
4387+
#ifndef OPENSSL_NO_SM2
4388+
if (nid == NID_sm2) {
4389+
EVP_PKEY_CTX_free(ctx);
4390+
ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_SM2, NULL);
4391+
}
4392+
#endif
43934393
group = EC_GROUP_new_by_curve_name(nid);
43944394
if (!group) {
43954395
goto cleanup;
@@ -4456,40 +4456,42 @@ static EVP_PKEY *php_openssl_pkey_init_ec(zval *data, bool *is_private) {
44564456
} else {
44574457
EC_GROUP_set_generator(group, point_g, order, BN_value_one());
44584458
}
4459+
#ifndef OPENSSL_NO_SM2
4460+
if (EC_GROUP_check_named_curve(group, 0, bctx) == NID_sm2) {
4461+
EVP_PKEY_CTX_free(ctx);
4462+
ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_SM2, NULL);
4463+
OSSL_PARAM_BLD_push_utf8_string(bld, OSSL_PKEY_PARAM_GROUP_NAME, "SM2", 3);
4464+
}
4465+
#endif
44594466
}
44604467

4461-
/* custom params not supported with SM2, SKIP */
4462-
if (!curve_name_zv ||
4463-
(Z_TYPE_P(curve_name_zv) == IS_STRING &&
4464-
OPENSSL_strcasecmp(Z_STRVAL_P(curve_name_zv), "SM2") != 0)){
4465-
OPENSSL_PKEY_SET_BN(data, d);
4466-
OPENSSL_PKEY_SET_BN(data, x);
4467-
OPENSSL_PKEY_SET_BN(data, y);
4468+
OPENSSL_PKEY_SET_BN(data, d);
4469+
OPENSSL_PKEY_SET_BN(data, x);
4470+
OPENSSL_PKEY_SET_BN(data, y);
44684471

4469-
if (d) {
4470-
OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_PRIV_KEY, d);
4472+
if (d) {
4473+
OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_PRIV_KEY, d);
44714474

4472-
point_q = EC_POINT_new(group);
4473-
if (!point_q || !EC_POINT_mul(group, point_q, d, NULL, NULL, bctx)) {
4474-
goto cleanup;
4475-
}
4476-
} else if (x && y) {
4477-
/* OpenSSL does not allow setting EC_PUB_X/EC_PUB_Y, so convert to encoded format. */
4478-
point_q = EC_POINT_new(group);
4479-
if (!point_q || !EC_POINT_set_affine_coordinates(group, point_q, x, y, bctx)) {
4480-
goto cleanup;
4481-
}
4475+
point_q = EC_POINT_new(group);
4476+
if (!point_q || !EC_POINT_mul(group, point_q, d, NULL, NULL, bctx)) {
4477+
goto cleanup;
44824478
}
4479+
} else if (x && y) {
4480+
/* OpenSSL does not allow setting EC_PUB_X/EC_PUB_Y, so convert to encoded format. */
4481+
point_q = EC_POINT_new(group);
4482+
if (!point_q || !EC_POINT_set_affine_coordinates(group, point_q, x, y, bctx)) {
4483+
goto cleanup;
4484+
}
4485+
}
44834486

4484-
if (point_q) {
4485-
size_t point_q_buf_len =
4486-
EC_POINT_point2buf(group, point_q, POINT_CONVERSION_COMPRESSED, &point_q_buf, bctx);
4487-
if (!point_q_buf_len) {
4488-
goto cleanup;
4489-
}
4490-
4491-
OSSL_PARAM_BLD_push_octet_string(bld, OSSL_PKEY_PARAM_PUB_KEY, point_q_buf, point_q_buf_len);
4487+
if (point_q) {
4488+
size_t point_q_buf_len =
4489+
EC_POINT_point2buf(group, point_q, POINT_CONVERSION_COMPRESSED, &point_q_buf, bctx);
4490+
if (!point_q_buf_len) {
4491+
goto cleanup;
44924492
}
4493+
4494+
OSSL_PARAM_BLD_push_octet_string(bld, OSSL_PKEY_PARAM_PUB_KEY, point_q_buf, point_q_buf_len);
44934495
}
44944496

44954497
params = OSSL_PARAM_BLD_to_param(bld);
@@ -4504,7 +4506,7 @@ static EVP_PKEY *php_openssl_pkey_init_ec(zval *data, bool *is_private) {
45044506
}
45054507

45064508
ctx = EVP_PKEY_CTX_new(param_key, NULL);
4507-
}
4509+
}
45084510

45094511
if (EVP_PKEY_check(ctx) || EVP_PKEY_public_check_quick(ctx)) {
45104512
*is_private = d != NULL;
@@ -4884,7 +4886,13 @@ PHP_FUNCTION(openssl_pkey_get_details)
48844886
*/
48854887
#if PHP_OPENSSL_API_VERSION >= 0x30000
48864888
zval ary;
4887-
switch (EVP_PKEY_base_id(pkey)) {
4889+
int id = EVP_PKEY_base_id(pkey);
4890+
#ifdef HAVE_EVP_PKEY_EC
4891+
if (EVP_PKEY_is_a(pkey, "SM2")) {
4892+
id = EVP_PKEY_EC;
4893+
}
4894+
#endif
4895+
switch (id) {
48884896
case EVP_PKEY_RSA:
48894897
ktype = OPENSSL_KEYTYPE_RSA;
48904898
array_init(&ary);

0 commit comments

Comments
 (0)