Skip to content

openssl ext sm2 compatibility #9422

Open

Description

@xywf221

Description

The following code:

<?php
$public_key = openssl_pkey_get_public(file_get_contents('sm2pubkey.pem'));
$d1 = openssl_pkey_get_details($public_key);
var_dump($d1);

Resulted in this output:

  ["dh"]=>
  array(1) {
    ["p"]=> 
  }
  ["type"]=>
  int(2)

But I expected this output instead:

  ["ec"]=>
  array(5) {
    ["curve_name"]=>
    string(9) "sm2"
    ["curve_oid"]=>
    ["x"]=> 
    ["y"]=> 
    ["d"]=> 
  }
  ["type"]=> int(3)

my openssl ext info

OpenSSL support => enabled
OpenSSL Library Version => OpenSSL 3.0.2 15 Mar 2022
OpenSSL Header Version => OpenSSL 3.0.2 15 Mar 2022
Openssl default config => /usr/lib/ssl/openssl.cnf

Directive => Local Value => Master Value
openssl.cafile => no value => no value
openssl.capath => no value => no value

the sm2pubkey.pem generate command
openssl ecparam -name SM2 -genkey -noout -out sm2-key.pem
openssl ec -in sm2-key.pem -pubout -out sm2pubkey.pem

PHP Version

8.1.2

Operating System

Ubuntu 22.04.1 LTS

Activity

  1. cmb69 commented on Aug 25, 2022

    @cmb69
    Member

    Indeed, there is no support for EVP_PKEY_SM2 yet. Looks more like a feature request than a bug. @bukka, thoughts?

  2. bukka commented on Aug 28, 2022

    @bukka
    Member

    I think the actual SM2 support would be more a feature as it has got some difference compare to standard EC (as documented in https://www.openssl.org/docs/manmaster/man7/EVP_PKEY-SM2.html ) . There's potentially one bug though as it seems not right to return DH so that might need a separate look and could be treated as a bug fix potentially (it should probably return false in such case).

  3. bukka commented on Dec 31, 2022

    @bukka
    Member

    I have done some debugging of this as I was getting different result with OpenSSL 1.1.1 (getting the actual SM2 with some missing parameters) and OpenSSL 3.0 where I actually got a different result and PKEY type ended up to be -1. That triggered incorrect assertion that I needed to fix by #10194 . After some digging I came across this issue that seems to be the reason for the behavior change: openssl/openssl#14411 .

    What's strange that you are getting dh in the result. I might need to check it on prod build to see if I'm able to reproduce.

  4. xywf221 commented on Dec 31, 2022

    @xywf221
    Author

    Thank you for your continued attention to this issue @bukka

  5. Eno-CN commented on Jun 20, 2023

    @Eno-CN
    Contributor

    @xywf221 You could use openssl genpkey -algorithm EC -pkeyopt ec_paramgen_curve:SM2 -out sm2-key.pem and openssl pkey -in sm2-key.pem -pubout -out sm2pubkey.pem instead.
    There would be a wrong on OpenSSL 3.x. Details see openssl/openssl#20973

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions