@@ -4304,36 +4304,31 @@ static bool php_openssl_pkey_init_legacy_ec(EC_KEY *eckey, zval *data, bool *is_
4304
4304
goto clean_exit ;
4305
4305
}
4306
4306
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" ))){
4311
4307
OPENSSL_PKEY_SET_BN (data , d );
4312
4308
OPENSSL_PKEY_SET_BN (data , x );
4313
4309
OPENSSL_PKEY_SET_BN (data , y );
4314
4310
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
+ }
4320
4316
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 ;
4331
4326
}
4327
+ }
4332
4328
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 ;
4337
4332
}
4338
4333
}
4339
4334
@@ -4373,7 +4368,7 @@ static EVP_PKEY *php_openssl_pkey_init_ec(zval *data, bool *is_private) {
4373
4368
EC_POINT * point_q = NULL ;
4374
4369
unsigned char * point_q_buf = NULL ;
4375
4370
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 );
4377
4372
BN_CTX * bctx = BN_CTX_new ();
4378
4373
OSSL_PARAM * params = NULL ;
4379
4374
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) {
4389
4384
php_error_docref (NULL , E_WARNING , "Unknown curve name" );
4390
4385
goto cleanup ;
4391
4386
}
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
4393
4393
group = EC_GROUP_new_by_curve_name (nid );
4394
4394
if (!group ) {
4395
4395
goto cleanup ;
@@ -4456,40 +4456,42 @@ static EVP_PKEY *php_openssl_pkey_init_ec(zval *data, bool *is_private) {
4456
4456
} else {
4457
4457
EC_GROUP_set_generator (group , point_g , order , BN_value_one ());
4458
4458
}
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
4459
4466
}
4460
4467
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 );
4468
4471
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 );
4471
4474
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 ;
4482
4478
}
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
+ }
4483
4486
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 ;
4492
4492
}
4493
+
4494
+ OSSL_PARAM_BLD_push_octet_string (bld , OSSL_PKEY_PARAM_PUB_KEY , point_q_buf , point_q_buf_len );
4493
4495
}
4494
4496
4495
4497
params = OSSL_PARAM_BLD_to_param (bld );
@@ -4504,7 +4506,7 @@ static EVP_PKEY *php_openssl_pkey_init_ec(zval *data, bool *is_private) {
4504
4506
}
4505
4507
4506
4508
ctx = EVP_PKEY_CTX_new (param_key , NULL );
4507
- }
4509
+ }
4508
4510
4509
4511
if (EVP_PKEY_check (ctx ) || EVP_PKEY_public_check_quick (ctx )) {
4510
4512
* is_private = d != NULL ;
@@ -4884,7 +4886,13 @@ PHP_FUNCTION(openssl_pkey_get_details)
4884
4886
*/
4885
4887
#if PHP_OPENSSL_API_VERSION >= 0x30000
4886
4888
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 ) {
4888
4896
case EVP_PKEY_RSA :
4889
4897
ktype = OPENSSL_KEYTYPE_RSA ;
4890
4898
array_init (& ary );
0 commit comments